Test coverage report: subscribe.views

# subscribe.views: 37 total statements, 76.7% covered

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

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

Stats: 23 executed, 7 missed, 7 excluded, 24 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 = 'subscribe/subscription_form.html'`
16. ``
17. `@method_decorator(login_required)`
18. `def dispatch(self, request, *args, **kwargs):`
19. `self.user = request.user`
20. ``
21. `try:`
22. `self.ctype = ContentType.objects.get(pk=kwargs.get('ctype_pk'))`
23. `except ContentType.DoesNotExist:`
24. `return Http404`
25. ``
26. `try:`
27. `self.content_object = self.ctype.get_object_for_this_type(`
28. `pk=kwargs.get('object_pk'))`
29. `except ObjectDoesNotExist:`
30. `return Http404`
31. ``
32. `return super(SubscriptionCreateView, self).dispatch(`
33. `request, *args, **kwargs)`
34. ``
35. `def form_valid(self, form):`
36. `self.object = form.save()`
37. `return super(SubscriptionCreateView, self).form_valid(form)`
38. ``
39. `def get_context_data(self, **kwargs):`
40. `ctx = super(SubscriptionCreateView, self).get_context_data(**kwargs)`
41. `ctx.update({`
42. `'content_object': self.content_object,`
43. `})`
44. `return ctx`
45. ``
46. `def get_form_kwargs(self):`
47. `kwargs = super(SubscriptionCreateView, self).get_form_kwargs()`
48. `kwargs.update({`
49. `'user': self.user,`
50. `'content_object': self.content_object,`
51. `})`
52. `return kwargs`
53. ``
54. `def get_success_url(self):`
55. `return self.content_object.get_absolute_url()`
56. ``
57. ``
58. `class SubscriptionDeleteView(SubscriptionCreateView):`
59. `"""View that un-subscribes a ``User`` from any thing."""`
60. `form_class = SubscriptionDeleteForm`
61. `template_name = 'subscribe/subscription_delete.html'`