+ def __init__(self, *args, **kwargs):
+ super(ImageAddForm, self).__init__(*args, **kwargs)
+ self.fields['file'].required = False
+
+ def clean_download_url(self):
+ cl = self.cleaned_data['download_url'] or None
+ if cl is not None:
+ try:
+ img = Image.objects.get(download_url=cl)
+ except Image.DoesNotExist:
+ pass
+ else:
+ raise forms.ValidationError(mark_safe(
+ ugettext('Image <a href="%(url)s">already in repository</a>.'
+ ) % {'url': img.get_absolute_url()}))
+ return cl
+
+ def clean(self):
+ cleaned_data = super(ImageAddForm, self).clean()
+ if not cleaned_data.get('download_url', None) and not cleaned_data.get('file', None):
+ raise forms.ValidationError('No image specified')
+ return cleaned_data
+