user_media.forms: 38 total statements, 100.0% covered

Generated: Thu 2013-03-14 13:13 SGT

Source file: /Users/martin/Repos/django-user-media/user_media/forms.py

Stats: 34 executed, 0 missed, 4 excluded, 39 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. exclude = ('user', 'content_type', 'object_id')
  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 save(self, *args, **kwargs):
  58. self.instance.user = self.user
  59. self.instance.content_type = self.content_type
  60. self.instance.object_id = self.object_id
  61. return super(UserMediaImageForm, self).save(*args, **kwargs)