user_media.forms: 46 total statements, 100.0% covered

Generated: Mon 2013-11-25 14:30 CET

Source file: /home/tobi/Projects/django-user-media/src/user_media/forms.py

Stats: 42 executed, 0 missed, 4 excluded, 53 ignored

  1. """Forms for the ``django-user-media`` app."""
  2. from django import forms
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.utils.translation import ugettext_lazy as _
  5. from user_media.models import UserMediaImage
  6. class UserMediaImageFormMixin(object):
  7. """
  8. Adds an `forms.ImageField` with name `user_media_image` to the form.
  9. Overrides `save()` and makes sure that the uploaded image get's tied
  10. to the content object instance.
  11. This is useful if you have a model form for your content object and you
  12. want to support uploading the user media image right from that form.
  13. Please make sure that your content object has a property called `user` that
  14. returns the user to which the content object belongs to.
  15. Currently it only supports one image per content object. On each subsequent
  16. upload, all other images of that content object will be deleted before the
  17. new image will be saved.
  18. """
  19. image_label = _('Image')
  20. require_user_media_image = False
  21. def __init__(self, *args, **kwargs):
  22. super(UserMediaImageFormMixin, self).__init__(*args, **kwargs)
  23. self.fields['user_media_image'] = forms.ImageField(
  24. required=self.require_user_media_image,
  25. label=self.image_label,
  26. )
  27. def _delete_images(self, instance):
  28. """Deletes all user media images of the given instance."""
  29. UserMediaImage.objects.filter(
  30. content_type=ContentType.objects.get_for_model(instance),
  31. object_id=instance.pk,
  32. user=instance.user,
  33. ).delete()
  34. def save(self, *args, **kwargs):
  35. instance = super(UserMediaImageFormMixin, self).save(*args, **kwargs)
  36. umedia_image = self.cleaned_data['user_media_image']
  37. if umedia_image:
  38. self._delete_images(instance)
  39. image = UserMediaImage()
  40. image.user = instance.user
  41. image.content_type = ContentType.objects.get_for_model(
  42. instance)
  43. image.object_id = instance.pk
  44. image.image = umedia_image
  45. image.save()
  46. return instance
  47. class UserMediaImageForm(forms.ModelForm):
  48. """Form that allows to create or update an `UserMediaImage` object."""
  49. class Meta:
  50. model = UserMediaImage
  51. fields = ('image',)
  52. def __init__(self, user, content_type, object_id, *args, **kwargs):
  53. self.user = user
  54. self.content_type = content_type
  55. self.object_id = object_id
  56. super(UserMediaImageForm, self).__init__(*args, **kwargs)
  57. def clean_image(self):
  58. """
  59. It seems like in Django 1.5 something has changed.
  60. When Django tries to validate the form, it checks if the generated
  61. filename fit into the max_length. But at this point, self.instance.user
  62. is not yet set so our filename generation function cannot create
  63. the new file path because it needs the user id. Setting
  64. self.instance.user at this point seems to work as a workaround.
  65. """
  66. self.instance.user = self.user
  67. data = self.cleaned_data.get('image')
  68. return data
  69. def save(self, *args, **kwargs):
  70. self.instance.user = self.user
  71. self.instance.content_type = self.content_type
  72. self.instance.object_id = self.object_id
  73. return super(UserMediaImageForm, self).save(*args, **kwargs)
  74. class UserMediaImageSingleUploadForm(forms.ModelForm):
  75. """Form to save a single image upload."""
  76. def __init__(self, image_field, *args, **kwargs):
  77. super(UserMediaImageSingleUploadForm, self).__init__(*args, **kwargs)
  78. self.fields[image_field] = forms.ImageField()