Test coverage report: user_media.forms

# user_media.forms: 46 total statements, 100.0% covered

Generated: Wed 2013-11-27 14:17 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. ``
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. `fields = ('image',)`
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 clean_image(self):`
74. `"""`
75. `It seems like in Django 1.5 something has changed.`
76. ``
77. `When Django tries to validate the form, it checks if the generated`
78. `filename fit into the max_length. But at this point, self.instance.user`
79. `is not yet set so our filename generation function cannot create`
80. `the new file path because it needs the user id. Setting`
81. `self.instance.user at this point seems to work as a workaround.`
82. ``
83. `"""`
84. `self.instance.user = self.user`
85. `data = self.cleaned_data.get('image')`
86. `return data`
87. ``
88. `def save(self, *args, **kwargs):`
89. `self.instance.user = self.user`
90. `self.instance.content_type = self.content_type`
91. `self.instance.object_id = self.object_id`
92. `return super(UserMediaImageForm, self).save(*args, **kwargs)`
93. ``
94. ``
95. `class UserMediaImageSingleUploadForm(forms.ModelForm):`
96. `"""Form to save a single image upload."""`
97. `def __init__(self, image_field, *args, **kwargs):`
98. `super(UserMediaImageSingleUploadForm, self).__init__(*args, **kwargs)`
99. `self.fields[image_field] = forms.ImageField()`