Test coverage report: subscribe.forms

# subscribe.forms: 27 total statements, 100.0% covered

Generated: Sat 2013-04-06 17:51 SGT

Source file: /Users/martin/Repos/django-subscribe/subscribe/forms.py

Stats: 24 executed, 0 missed, 3 excluded, 20 ignored

1. `"""Forms for the ``subscribe`` app."""`
2. `from django import forms`
3. `from django.contrib.contenttypes.models import ContentType`
4. ``
5. `from .models import Subscription`
6. ``
7. ``
8. `class SubscriptionCreateForm(forms.Form):`
9. `def __init__(self, user, content_object, *args, **kwargs):`
10. `self.user = user`
11. `self.content_object = content_object`
12. `self.ctype = ContentType.objects.get_for_model(self.content_object)`
13. `super(SubscriptionCreateForm, self).__init__(*args, **kwargs)`
14. ``
15. `def _get_method_kwargs(self):`
16. `"""`
17. `Helper method. Returns kwargs needed to filter the correct object.`
18. ``
19. `Can also be used to create the correct object.`
20. ``
21. `"""`
22. `method_kwargs = {`
23. `'user': self.user,`
24. `'content_type': self.ctype,`
25. `'object_id': self.content_object.pk,`
26. `}`
27. `return method_kwargs`
28. ``
29. `def save(self, *args, **kwargs):`
30. `"""Adds a subscription for the given user to the given object."""`
31. `method_kwargs = self._get_method_kwargs()`
32. `try:`
33. `subscription = Subscription.objects.get(**method_kwargs)`
34. `except Subscription.DoesNotExist:`
35. `subscription = Subscription.objects.create(**method_kwargs)`
36. `return subscription`
37. ``
38. ``
39. `class SubscriptionDeleteForm(SubscriptionCreateForm):`
40. `def save(self, *args, **kwargs):`
41. `"""Removes a subscription for the given user from the given object."""`
42. `method_kwargs = self._get_method_kwargs()`
43. `try:`
44. `subscription = Subscription.objects.get(**method_kwargs)`
45. `except Subscription.DoesNotExist:`
46. `return`
47. `subscription.delete()`