user_media.forms: 51 total statements, 100.0% covered

Generated: Tue 2014-07-15 16:34 CEST

Source file: /home/dkaufhold/projects/django-user-media/src/user_media/forms.py

Stats: 47 executed, 0 missed, 4 excluded, 58 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. The field name of the ImageField can be overridden by setting
  19. ``image_field_name`` on the form, that includes this mixin.
  20. """
  21. image_label = _('Image')
  22. require_user_media_image = False
  23. image_field_name = 'user_media_image'
  24. def __init__(self, *args, **kwargs):
  25. super(UserMediaImageFormMixin, self).__init__(*args, **kwargs)
  26. self.fields[self.image_field_name] = forms.ImageField(
  27. required=self.require_user_media_image,
  28. label=self.image_label,
  29. )
  30. def _delete_images(self, instance):
  31. """Deletes all user media images of the given instance."""
  32. UserMediaImage.objects.filter(
  33. content_type=ContentType.objects.get_for_model(instance),
  34. object_id=instance.pk,
  35. user=instance.user,
  36. ).delete()
  37. def save(self, *args, **kwargs):
  38. instance = super(UserMediaImageFormMixin, self).save(*args, **kwargs)
  39. umedia_image = self.cleaned_data[self.image_field_name]
  40. if umedia_image:
  41. self._delete_images(instance)
  42. image = UserMediaImage()
  43. image.user = instance.user
  44. image.content_type = ContentType.objects.get_for_model(
  45. instance)
  46. image.object_id = instance.pk
  47. image.image = umedia_image
  48. image.save()
  49. return instance
  50. class UserMediaImageForm(forms.ModelForm):
  51. """Form that allows to create or update an `UserMediaImage` object."""
  52. class Meta:
  53. model = UserMediaImage
  54. fields = ('image', )
  55. def __init__(self, user, content_type=None, object_id=None,
  56. *args, **kwargs):
  57. self.user = user
  58. self.content_type = content_type
  59. self.object_id = object_id
  60. if content_type is not None and object_id is not None:
  61. self.content_object = content_type.get_object_for_this_type(
  62. pk=self.object_id)
  63. super(UserMediaImageForm, self).__init__(*args, **kwargs)
  64. def clean_image(self):
  65. """
  66. It seems like in Django 1.5 something has changed.
  67. When Django tries to validate the form, it checks if the generated
  68. filename fit into the max_length. But at this point, self.instance.user
  69. is not yet set so our filename generation function cannot create
  70. the new file path because it needs the user id. Setting
  71. self.instance.user at this point seems to work as a workaround.
  72. """
  73. self.instance.user = self.user
  74. data = self.cleaned_data.get('image')
  75. return data
  76. def save(self, *args, **kwargs):
  77. self.instance.user = self.user
  78. if self.content_type is not None and self.object_id is not None:
  79. self.instance.content_type = self.content_type
  80. self.instance.object_id = self.object_id
  81. return super(UserMediaImageForm, self).save(*args, **kwargs)
  82. class UserMediaImageSingleUploadForm(forms.ModelForm):
  83. """Form to save a single image upload."""
  84. def __init__(self, image_field, *args, **kwargs):
  85. self._meta.model = type(kwargs['instance'])
  86. super(UserMediaImageSingleUploadForm, self).__init__(*args, **kwargs)
  87. self.fields[image_field] = forms.ImageField()