Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

generic.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 self.response.
  16. method_name = "do_%s" % self.request.POST['action'].lower()
  17. if hasattr(self, method_name):
  18. getattr(self, method_name)(model)
  19. return self.response
  20. else:
  21. messages.error(request, _("Invalid form submission"))
  22. return super(PostActionMixin, self).post(request, *args, **kwargs)
  23. class BulkEditMixin(object):
  24. """
  25. Mixin for views that have a bulk editing facility. This is normally in the
  26. form of tabular data where each row has a checkbox. The UI allows a number
  27. of rows to be selected and then some 'action' to be performed on them.
  28. """
  29. action_param = 'action'
  30. # Permitted methods that can be used to act on the selected objects
  31. actions = None
  32. current_view = None
  33. checkbox_object_name = None
  34. def get_checkbox_object_name(self):
  35. if self.checkbox_object_name:
  36. return self.checkbox_object_name
  37. object_list = self.get_queryset()
  38. if hasattr(object_list, 'model'):
  39. return smart_str(object_list.model._meta.object_name.lower())
  40. else:
  41. return None
  42. def get_queryset(self):
  43. pass
  44. def get_error_url(self, request):
  45. return request.META['HTTP_REFERER']
  46. def get_success_url(self, request):
  47. return request.META['HTTP_REFERER']
  48. def post(self, request, *args, **kwargs):
  49. # Dynamic dispatch pattern - we forward POST requests onto a method
  50. # designated by the 'action' parameter. The action has to be in a
  51. # whitelist to avoid security issues.
  52. action = request.POST.get(self.action_param, '').lower()
  53. if not self.actions or action not in self.actions:
  54. messages.error(self.request, _("Invalid action"))
  55. return HttpResponseRedirect(self.get_error_url(request))
  56. ids = request.POST.getlist(
  57. 'selected_%s' % self.get_checkbox_object_name())
  58. ids = map(int, ids)
  59. if not ids:
  60. messages.error(
  61. self.request, _("You need to select some %ss") % self.get_checkbox_object_name())
  62. return HttpResponseRedirect(self.get_error_url(request))
  63. objects = self.get_objects(ids)
  64. return getattr(self, action)(request, objects)
  65. def get_objects(self, ids):
  66. object_dict = self.get_object_dict(ids)
  67. # Rearrange back into the original order
  68. return [object_dict[id] for id in ids]
  69. def get_object_dict(self, ids):
  70. return self.model.objects.in_bulk(ids)