|
@@ -7,6 +7,13 @@ class PinForm(forms.ModelForm): |
|
|
url = forms.CharField(required=False) |
|
|
url = forms.CharField(required=False) |
|
|
image = forms.ImageField(label='or Upload', required=False) |
|
|
image = forms.ImageField(label='or Upload', required=False) |
|
|
|
|
|
|
|
|
|
|
|
_errors = { |
|
|
|
|
|
'not_image': 'Requested URL is not an image file. Only images are currently supported.', |
|
|
|
|
|
'pinned': 'URL has already been pinned!', |
|
|
|
|
|
'protocol': 'Currently only support HTTP and HTTPS protocols, please be sure you include this in the URL.', |
|
|
|
|
|
'nothing': 'Need either a URL or Upload', |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
class Meta: |
|
|
class Meta: |
|
|
model = Pin |
|
|
model = Pin |
|
|
fields = ['url', 'image', 'description', 'tags'] |
|
|
fields = ['url', 'image', 'description', 'tags'] |
|
@@ -20,26 +27,18 @@ class PinForm(forms.ModelForm): |
|
|
if url: |
|
|
if url: |
|
|
image_file_types = ['png', 'gif', 'jpeg', 'jpg'] |
|
|
image_file_types = ['png', 'gif', 'jpeg', 'jpg'] |
|
|
if not url.split('.')[-1].lower() in image_file_types: |
|
|
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 |
|
|
|
|
|
|
|
|
raise forms.ValidationError(self._errors['not_image']) |
|
|
protocol = url.split(':')[0] |
|
|
protocol = url.split(':')[0] |
|
|
if protocol not in ['http', 'https']: |
|
|
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.") |
|
|
|
|
|
|
|
|
raise forms.ValidationError(self._errors['protocol']) |
|
|
try: |
|
|
try: |
|
|
Pin.objects.get(url=url) |
|
|
Pin.objects.get(url=url) |
|
|
raise forms.ValidationError("URL has already been pinned!") |
|
|
|
|
|
|
|
|
raise forms.ValidationError(self._errors['pinned']) |
|
|
except Pin.DoesNotExist: |
|
|
except Pin.DoesNotExist: |
|
|
pass |
|
|
pass |
|
|
elif image: |
|
|
elif image: |
|
|
pass |
|
|
pass |
|
|
else: |
|
|
else: |
|
|
raise forms.ValidationError("Need either a URL or Upload.") |
|
|
|
|
|
|
|
|
raise forms.ValidationError(self._errors['nothing']) |
|
|
|
|
|
|
|
|
return cleaned_data |
|
|
return cleaned_data |