Test coverage report: user_media.forms

# 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. ``
6. `from user_media.models import UserMediaImage`
7. ``
8. ``
9. `class UserMediaImageFormMixin(object):`
10. `"""`
11. `Adds an `forms.ImageField` with name `user_media_image` to the form.`
12. ``
13. `Overrides `save()` and makes sure that the uploaded image get's tied`
14. `to the content object instance.`
15. ``
16. `This is useful if you have a model form for your content object and you`
17. `want to support uploading the user media image right from that form.`
18. ``
19. `Please make sure that your content object has a property called `user` that`
20. `returns the user to which the content object belongs to.`
21. ``
22. `Currently it only supports one image per content object. On each subsequent`
23. `upload, all other images of that content object will be deleted before the`
24. `new image will be saved.`
25. ``
26. `"""`
27. ``
28. `image_label = _('Image')`
29. `require_user_media_image = False`
30. ``
31. `def __init__(self, *args, **kwargs):`
32. `super(UserMediaImageFormMixin, self).__init__(*args, **kwargs)`
33. `self.fields['user_media_image'] = forms.ImageField(`
34. `required=self.require_user_media_image,`
35. `label=self.image_label,`
36. `)`
37. ``
38. `def _delete_images(self, instance):`
39. `"""Deletes all user media images of the given instance."""`
40. `UserMediaImage.objects.filter(`
41. `content_type=ContentType.objects.get_for_model(instance),`
42. `object_id=instance.pk,`
43. `user=instance.user,`
44. `).delete()`
45. ``
46. `def save(self, *args, **kwargs):`
47. `instance = super(UserMediaImageFormMixin, self).save(*args, **kwargs)`
48. `umedia_image = self.cleaned_data['user_media_image']`
49. `if umedia_image:`
50. `self._delete_images(instance)`
51. `image = UserMediaImage()`
52. `image.user = instance.user`
53. `image.content_type = ContentType.objects.get_for_model(`
54. `instance)`
55. `image.object_id = instance.pk`
56. `image.image = umedia_image`
57. `image.save()`
58. `return instance`
59. ``
60. ``
61. `class UserMediaImageForm(forms.ModelForm):`
62. `"""Form that allows to create or update an `UserMediaImage` object."""`
63. `class Meta:`
64. `model = UserMediaImage`
65. `exclude = ('user', 'content_type', 'object_id')`
66. ``
67. `def __init__(self, user, content_type, object_id, *args, **kwargs):`
68. `self.user = user`
69. `self.content_type = content_type`
70. `self.object_id = object_id`
71. `super(UserMediaImageForm, self).__init__(*args, **kwargs)`
72. ``
73. `def save(self, *args, **kwargs):`
74. `self.instance.user = self.user`
75. `self.instance.content_type = self.content_type`
76. `self.instance.object_id = self.object_id`
77. `return super(UserMediaImageForm, self).save(*args, **kwargs)`