mirror of
https://github.com/pinry/pinry.git
synced 2025-11-14 00:55:43 +01:00
Another major Pinry model rewrite
Generate thumbnail and standard image on request, and use http://github.com/mirumee/django-images for generating them. Also, remove the CreatePin page as pin creation is going to be done in JavaScript. Create UploadImage view for uploading images from computer.
This commit is contained in:
@@ -1,44 +1,9 @@
|
||||
from django import forms
|
||||
|
||||
from .models import Pin
|
||||
from django_images.models import Image
|
||||
|
||||
|
||||
class PinForm(forms.ModelForm):
|
||||
url = forms.CharField(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 ImageForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Pin
|
||||
fields = ['url', 'image', 'description', 'tags']
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(PinForm, self).clean()
|
||||
|
||||
url = cleaned_data.get('url')
|
||||
image = cleaned_data.get('image')
|
||||
|
||||
if url:
|
||||
image_file_types = ['png', 'gif', 'jpeg', 'jpg']
|
||||
if not url.split('.')[-1].lower() in image_file_types:
|
||||
raise forms.ValidationError(self._errors['not_image'])
|
||||
protocol = url.split(':')[0]
|
||||
if protocol not in ['http', 'https']:
|
||||
raise forms.ValidationError(self._errors['protocol'])
|
||||
try:
|
||||
Pin.objects.get(url=url)
|
||||
raise forms.ValidationError(self._errors['pinned'])
|
||||
except Pin.DoesNotExist:
|
||||
pass
|
||||
elif image:
|
||||
pass
|
||||
else:
|
||||
raise forms.ValidationError(self._errors['nothing'])
|
||||
|
||||
return cleaned_data
|
||||
model = Image
|
||||
fields = ('image',)
|
||||
|
||||
Reference in New Issue
Block a user