Test coverage report: user_media.forms

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