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.

generic.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from django.utils.encoding import smart_str
  2. from django.contrib import messages
  3. from django.http import HttpResponseRedirect
  4. from django.utils.translation import ugettext_lazy as _
  5. class PostActionMixin(object):
  6. """
  7. Simple mixin to forward POST request that contain a key 'action'
  8. onto a method of form "do_{action}".
  9. This only works with DetailView
  10. """
  11. def post(self, request, *args, **kwargs):
  12. if 'action' in self.request.POST:
  13. model = self.get_object()
  14. # The do_* method is required to do what it needs to with the model
  15. # it is passed, and then to assign the HTTP response to
  16. # self.response.
  17. method_name = "do_%s" % self.request.POST['action'].lower()
  18. if hasattr(self, method_name):
  19. getattr(self, method_name)(model)
  20. return self.response
  21. else:
  22. messages.error(request, _("Invalid form submission"))
  23. return super(PostActionMixin, self).post(request, *args, **kwargs)
  24. class BulkEditMixin(object):
  25. """
  26. Mixin for views that have a bulk editing facility. This is normally in the
  27. form of tabular data where each row has a checkbox. The UI allows a number
  28. of rows to be selected and then some 'action' to be performed on them.
  29. """
  30. action_param = 'action'
  31. # Permitted methods that can be used to act on the selected objects
  32. actions = None
  33. current_view = None
  34. checkbox_object_name = None
  35. def get_checkbox_object_name(self):
  36. if self.checkbox_object_name:
  37. return self.checkbox_object_name
  38. object_list = self.get_queryset()
  39. if hasattr(object_list, 'model'):
  40. return smart_str(object_list.model._meta.object_name.lower())
  41. else:
  42. return None
  43. def get_queryset(self):
  44. pass
  45. def get_error_url(self, request):
  46. return request.META['HTTP_REFERER']
  47. def get_success_url(self, request):
  48. return request.META['HTTP_REFERER']
  49. def post(self, request, *args, **kwargs):
  50. # Dynamic dispatch pattern - we forward POST requests onto a method
  51. # designated by the 'action' parameter. The action has to be in a
  52. # whitelist to avoid security issues.
  53. action = request.POST.get(self.action_param, '').lower()
  54. if not self.actions or action not in self.actions:
  55. messages.error(self.request, _("Invalid action"))
  56. return HttpResponseRedirect(self.get_error_url(request))
  57. ids = request.POST.getlist(
  58. 'selected_%s' % self.get_checkbox_object_name())
  59. ids = map(int, ids)
  60. if not ids:
  61. messages.error(
  62. self.request,
  63. _("You need to select some %ss") % self.get_checkbox_object_name())
  64. return HttpResponseRedirect(self.get_error_url(request))
  65. objects = self.get_objects(ids)
  66. return getattr(self, action)(request, objects)
  67. def get_objects(self, ids):
  68. object_dict = self.get_object_dict(ids)
  69. # Rearrange back into the original order
  70. return [object_dict[id] for id in ids]
  71. def get_object_dict(self, ids):
  72. return self.model.objects.in_bulk(ids)