Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. actions = None
  31. current_view = None
  32. checkbox_object_name = None
  33. def get_checkbox_object_name(self):
  34. object_list = self.get_queryset()
  35. if self.checkbox_object_name:
  36. return self.checkbox_object_name
  37. elif hasattr(object_list, 'model'):
  38. return smart_str(object_list.model._meta.object_name.lower())
  39. else:
  40. return None
  41. def get_error_url(self, request):
  42. return request.META['HTTP_REFERER']
  43. def get_success_url(self, request):
  44. return request.META['HTTP_REFERER']
  45. def post(self, request, *args, **kwargs):
  46. # Dynamic dispatch pattern - we forward POST requests onto a method
  47. # designated by the 'action' parameter. The action has to be in a
  48. # whitelist to avoid security issues.
  49. action = request.POST.get(self.action_param, '').lower()
  50. if not self.actions or action not in self.actions:
  51. messages.error(self.request, _("Invalid action"))
  52. return HttpResponseRedirect(self.get_error_url(request))
  53. ids = request.POST.getlist('selected_%s' % self.get_checkbox_object_name())
  54. if not ids:
  55. messages.error(self.request, _("You need to select some %ss") % self.get_checkbox_object_name())
  56. return HttpResponseRedirect(self.get_error_url(request))
  57. raw_objects = self.model.objects.in_bulk(ids)
  58. objects = [raw_objects[int(id)] for id in ids]
  59. return getattr(self, action)(request, objects)