2012-04-26 03:44:16 +00:00
|
|
|
from django import forms
|
|
|
|
|
|
|
|
|
|
from .models import Pin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PinForm(forms.ModelForm):
|
|
|
|
|
url = forms.CharField(label='URL')
|
|
|
|
|
|
2012-04-27 02:48:49 +00:00
|
|
|
def clean_url(self):
|
|
|
|
|
data = self.cleaned_data['url']
|
2012-05-01 04:42:45 +00:00
|
|
|
|
|
|
|
|
# Test file type
|
|
|
|
|
image_file_types = ['png', 'gif', 'jpeg', 'jpg']
|
|
|
|
|
file_type = data.split('.')[-1]
|
|
|
|
|
if file_type.lower() not in image_file_types:
|
|
|
|
|
raise forms.ValidationError("Requested URL is not an image file. Only images are currently supported.")
|
|
|
|
|
|
|
|
|
|
# Check if pin already exists
|
2012-04-27 15:11:41 +00:00
|
|
|
try:
|
|
|
|
|
Pin.objects.get(url=data)
|
|
|
|
|
raise forms.ValidationError("URL has already been pinned!")
|
|
|
|
|
except Pin.DoesNotExist:
|
2012-05-01 04:42:45 +00:00
|
|
|
protocol = data.split(':')[0]
|
|
|
|
|
if protocol == 'http':
|
|
|
|
|
opp_data = data.replace('http://', 'https://')
|
|
|
|
|
elif protocol == 'https':
|
|
|
|
|
opp_data = data.replace('https://', 'http://')
|
|
|
|
|
else:
|
|
|
|
|
raise forms.ValidationError("Currently only support HTTP and HTTPS protocols, please be sure you include this in the URL.")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
Pin.objects.get(url=opp_data)
|
|
|
|
|
raise forms.ValidationError("URL has already been pinned!")
|
|
|
|
|
except Pin.DoesNotExist:
|
|
|
|
|
return data
|
2012-04-27 02:48:49 +00:00
|
|
|
|
2012-04-26 03:44:16 +00:00
|
|
|
class Meta:
|
|
|
|
|
model = Pin
|
|
|
|
|
exclude = ['image']
|