subscribe.views: 36 total statements, 79.3% covered

Generated: Thu 2013-02-28 14:58 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. from .forms import SubscriptionCreateForm, SubscriptionDeleteForm
  9. class SubscriptionCreateView(FormView):
  10. """View that subscribes a ``User`` to any thing."""
  11. form_class = SubscriptionCreateForm
  12. template_name = 'subscribe/subscription_form.html'
  13. success_url = '/'
  14. @method_decorator(login_required)
  15. def dispatch(self, request, *args, **kwargs):
  16. self.user = request.user
  17. try:
  18. self.ctype = ContentType.objects.get(pk=kwargs.get('ctype_pk'))
  19. except ContentType.DoesNotExist:
  20. return Http404
  21. try:
  22. self.content_object = self.ctype.get_object_for_this_type(
  23. pk=kwargs.get('object_pk'))
  24. except ObjectDoesNotExist:
  25. return Http404
  26. return super(SubscriptionCreateView, self).dispatch(
  27. request, *args, **kwargs)
  28. def form_valid(self, form):
  29. form.save()
  30. return super(SubscriptionCreateView, self).form_valid(form)
  31. def get_context_data(self, **kwargs):
  32. ctx = super(SubscriptionCreateView, self).get_context_data(**kwargs)
  33. ctx.update({
  34. 'content_object': self.content_object,
  35. })
  36. return ctx
  37. def get_form_kwargs(self):
  38. kwargs = super(SubscriptionCreateView, self).get_form_kwargs()
  39. kwargs.update({
  40. 'user': self.user,
  41. 'content_object': self.content_object,
  42. })
  43. return kwargs
  44. class SubscriptionDeleteView(SubscriptionCreateView):
  45. """View that un-subscribes a ``User`` from any thing."""
  46. form_class = SubscriptionDeleteForm
  47. template_name = 'subscribe/subscription_delete.html'