Exemplo n.º 1
0
 def clean_photo(self):
     cleaned_data = self.cleaned_data
     cleaned_data['id_photo'] = self.data['id_photo']
     photo = cleaned_data.get('photo')
     if isinstance(photo, long):
         try:
             cleaned_data['photo'] = Photo.objects.get(pk=photo)
         except Photo.DoesNotExist:
             raise forms.ValidationError(_('Photo does not exist'))
     elif isinstance(photo, InMemoryUploadedFile):
         new_photo = Photo(title=photo.name, image=photo, is_gallery_thumbnail=True)
         cleaned_data['photo'] = new_photo
         try:
             new_photo.save()
         except IntegrityError:
             new_photo.title = new_photo.get_avaliable_title()
             new_photo.save()
     return cleaned_data['photo']