2012-04-26 03:44:16 +00:00
|
|
|
from django import forms
|
|
|
|
|
|
|
|
|
|
from .models import Pin
|
|
|
|
|
|
|
|
|
|
|
2013-02-24 16:56:03 +01:00
|
|
|
class PinForm(forms.ModelForm):
|
2013-02-24 17:20:20 +01:00
|
|
|
url = forms.CharField(required=False)
|
2012-07-24 21:23:27 +00:00
|
|
|
image = forms.ImageField(label='or Upload', required=False)
|
2013-02-24 16:56:03 +01:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Pin
|
2013-02-24 17:20:20 +01:00
|
|
|
fields = ['url', 'image', 'description', 'tags']
|
2012-05-01 04:42:45 +00:00
|
|
|
|
2012-07-03 02:24:34 +00:00
|
|
|
def clean(self):
|
|
|
|
|
cleaned_data = super(PinForm, self).clean()
|
|
|
|
|
|
|
|
|
|
url = cleaned_data.get('url')
|
|
|
|
|
image = cleaned_data.get('image')
|
2012-05-01 04:42:45 +00:00
|
|
|
|
2012-07-03 02:24:34 +00:00
|
|
|
if url:
|
2013-02-24 17:20:20 +01:00
|
|
|
image_file_types = ['png', 'gif', 'jpeg', 'jpg']
|
|
|
|
|
if not url.split('.')[-1].lower() in image_file_types:
|
|
|
|
|
raise forms.ValidationError("Requested URL is not an image file. "
|
|
|
|
|
"Only images are currently supported.")
|
|
|
|
|
try:
|
|
|
|
|
Pin.objects.get(url=url)
|
|
|
|
|
raise forms.ValidationError("URL has already been pinned!")
|
|
|
|
|
except Pin.DoesNotExist:
|
|
|
|
|
pass
|
|
|
|
|
protocol = url.split(':')[0]
|
|
|
|
|
if protocol not in ['http', 'https']:
|
|
|
|
|
raise forms.ValidationError("Currently only support HTTP and "
|
|
|
|
|
"HTTPS protocols, please be sure "
|
|
|
|
|
"you include this in the URL.")
|
2012-05-01 04:42:45 +00:00
|
|
|
try:
|
2012-07-03 02:24:34 +00:00
|
|
|
Pin.objects.get(url=url)
|
2012-05-01 04:42:45 +00:00
|
|
|
raise forms.ValidationError("URL has already been pinned!")
|
|
|
|
|
except Pin.DoesNotExist:
|
2013-02-24 17:20:20 +01:00
|
|
|
pass
|
2012-07-03 02:24:34 +00:00
|
|
|
elif image:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
raise forms.ValidationError("Need either a URL or Upload.")
|
|
|
|
|
|
|
|
|
|
return cleaned_data
|