You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from django.conf import settings
  2. from django.http import HttpResponse, Http404, HttpResponseRedirect
  3. from django.template import Context, loader, RequestContext
  4. from django.shortcuts import render
  5. from django.core.urlresolvers import reverse
  6. from django.contrib import messages
  7. def class_based_view(class_obj):
  8. u"""
  9. Simple function that takes a view class and returns a function
  10. that instantiates a new instance each time it is called.
  11. This is used urls.py files when using class-based views.
  12. """
  13. def _instantiate_view_class(request, *args, **kwargs):
  14. return class_obj()(request, *args, **kwargs)
  15. return _instantiate_view_class
  16. class ModelView(object):
  17. u"""
  18. A generic view for models which can recieve GET and POST requests
  19. The __init__ method of subclasses should set the default response
  20. variable.
  21. """
  22. template_file = None
  23. response = None
  24. def __call__(self, request, *args, **kwargs):
  25. self.request = request
  26. self.args = args
  27. self.kwargs = kwargs
  28. method_name = "handle_%s" % request.method.upper()
  29. model = self.get_model()
  30. getattr(self, method_name)(model)
  31. return self.response
  32. def handle_GET(self, model):
  33. u"""Default implementation of model view is to do nothing."""
  34. pass
  35. def handle_POST(self, model):
  36. u"""
  37. Handle a POST request to this resource.
  38. This will forward on request to a method of form "do_%s" where the
  39. second part needs to be specified as an "action" name within the
  40. request.
  41. If you don't want to handle POSTs this way, just override this method
  42. """
  43. if 'action' in self.request.POST:
  44. getattr(self, "do_%s" % self.request.POST['action'].lower())(model)
  45. def get_model(self):
  46. u"""Responsible for loading the model that is being acted on"""
  47. return None
  48. def home(request):
  49. u"""Oscar home page"""
  50. return render(request, 'home.html', locals())