Test coverage report: subscribe.views

# subscribe.views: 36 total statements, 79.3% covered

Generated: Thu 2013-02-28 14:37 SGT

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

Stats: 23 executed, 6 missed, 7 excluded, 23 ignored

1. `"""Views of the ``subscribe`` app."""`
2. `from django.core.exceptions import ObjectDoesNotExist`
3. `from django.contrib.auth.decorators import login_required`
4. `from django.contrib.contenttypes.models import ContentType`
5. `from django.http import Http404`
6. `from django.utils.decorators import method_decorator`
7. `from django.views.generic import FormView`
8. ``
9. `from .forms import SubscriptionCreateForm, SubscriptionDeleteForm`
10. ``
11. ``
12. `class SubscriptionCreateView(FormView):`
13. `"""View that subscribes a ``User`` to any thing."""`
14. `form_class = SubscriptionCreateForm`
15. `template_name = 'subscriptions/subscription_form.html'`
16. `success_url = '/'`
17. ``
18. `@method_decorator(login_required)`
19. `def dispatch(self, request, *args, **kwargs):`
20. `self.user = request.user`
21. ``
22. `try:`
23. `self.ctype = ContentType.objects.get(pk=kwargs.get('ctype_pk'))`
24. `except ContentType.DoesNotExist:`
25. `return Http404`
26. ``
27. `try:`
28. `self.content_object = self.ctype.get_object_for_this_type(`
29. `pk=kwargs.get('object_pk'))`
30. `except ObjectDoesNotExist:`
31. `return Http404`
32. ``
33. `return super(SubscriptionCreateView, self).dispatch(`
34. `request, *args, **kwargs)`
35. ``
36. `def form_valid(self, form):`
37. `form.save()`
38. `return super(SubscriptionCreateView, self).form_valid(form)`
39. ``
40. `def get_context_data(self, **kwargs):`
41. `ctx = super(SubscriptionCreateView, self).get_context_data(**kwargs)`
42. `ctx.update({`
43. `'content_object': self.content_object,`
44. `})`
45. `return ctx`
46. ``
47. `def get_form_kwargs(self):`
48. `kwargs = super(SubscriptionCreateView, self).get_form_kwargs()`
49. `kwargs.update({`
50. `'user': self.user,`
51. `'content_object': self.content_object,`
52. `})`
53. `return kwargs`
54. ``
55. ``
56. `class SubscriptionDeleteView(SubscriptionCreateView):`
57. `"""View that un-subscribes a ``User`` from any thing."""`
58. `form_class = SubscriptionDeleteForm`
59. `template_name = 'subscriptions/subscription_delete.html'`