1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django import forms
7 from django.conf import settings
8 from django.utils.translation import ugettext_lazy as _, ugettext
9 from cover.models import Image
10 from django.utils.safestring import mark_safe
11 from PIL import Image as PILImage
13 from cover.utils import get_import_data, FlickrError, URLOpener
16 class ImageAddForm(forms.ModelForm):
21 def __init__(self, *args, **kwargs):
22 super(ImageAddForm, self).__init__(*args, **kwargs)
23 self.fields['file'].required = False
25 def clean_download_url(self):
26 cl = self.cleaned_data['download_url'] or None
29 img = Image.objects.get(download_url=cl)
30 except Image.DoesNotExist:
33 raise forms.ValidationError(mark_safe(
34 ugettext('Image <a href="%(url)s">already in repository</a>.')
35 % {'url': img.get_absolute_url()}))
38 def clean_source_url(self):
39 source_url = self.cleaned_data['source_url'] or None
40 if source_url is not None:
41 same_source = Image.objects.filter(source_url=source_url)
43 raise forms.ValidationError(mark_safe(
44 ugettext('Image <a href="%(url)s">already in repository</a>.')
45 % {'url': same_source.first().get_absolute_url()}))
49 cleaned_data = super(ImageAddForm, self).clean()
50 download_url = cleaned_data.get('download_url', None)
51 uploaded_file = cleaned_data.get('file', None)
52 if not download_url and not uploaded_file:
53 raise forms.ValidationError(ugettext('No image specified'))
55 image_data = URLOpener().open(download_url).read()
56 width, height = PILImage.open(BytesIO(image_data)).size
58 width, height = PILImage.open(uploaded_file.file).size
59 min_width, min_height = settings.MIN_COVER_SIZE
60 if width < min_width or height < min_height:
61 raise forms.ValidationError(ugettext('Image too small: %sx%s, minimal dimensions %sx%s') %
62 (width, height, min_width, min_height))
66 class ImageEditForm(forms.ModelForm):
67 """Form used for editing a Book."""
70 exclude = ['download_url']
73 cleaned_data = super(ImageEditForm, self).clean()
74 uploaded_file = cleaned_data.get('file', None)
75 width, height = PILImage.open(uploaded_file.file).size
76 min_width, min_height = settings.MIN_COVER_SIZE
77 if width < min_width or height < min_height:
78 raise forms.ValidationError(ugettext('Image too small: %sx%s, minimal dimensions %sx%s') %
79 (width, height, min_width, min_height))
82 class ReadonlyImageEditForm(ImageEditForm):
83 """Form used for not editing an Image."""
85 def __init__(self, *args, **kwargs):
86 super(ReadonlyImageEditForm, self).__init__(*args, **kwargs)
87 for field in self.fields.values():
88 field.widget.attrs.update({"readonly": True})
90 def save(self, *args, **kwargs):
91 raise AssertionError("ReadonlyImageEditForm should not be saved.")
94 class ImportForm(forms.Form):
95 source_url = forms.URLField(label=_('WikiCommons, MNW or Flickr URL'))
97 def clean_source_url(self):
98 url = self.cleaned_data['source_url']
100 import_data = get_import_data(url)
101 except FlickrError as e:
102 raise forms.ValidationError(e)
103 for field_name in ('license_url', 'license_name', 'author', 'title', 'download_url'):
104 self.cleaned_data[field_name] = import_data[field_name]
105 return import_data['source_url']