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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from django.utils.encoding import smart_str
  2. from django.core.urlresolvers import reverse
  3. from django.views.generic import TemplateView
  4. class BulkEditMixin():
  5. actions = None
  6. current_view = None
  7. checkbox_object_name = None
  8. def get_checkbox_object_name(self):
  9. object_list = self.get_queryset()
  10. if self.checkbox_object_name:
  11. return self.checkbox_object_name
  12. elif hasattr(object_list, 'model'):
  13. return smart_str(object_list.model._meta.object_name.lower())
  14. else:
  15. return None
  16. def post(self, request, *args, **kwargs):
  17. action = request.POST.get('action', '').lower()
  18. if action not in self.actions:
  19. messages.error(self.request, "Invalid action")
  20. return HttpResponseRedirect(reverse(self.current_view))
  21. ids = request.POST.getlist('selected_%s' % self.get_checkbox_object_name())
  22. if not ids:
  23. messages.error(self.request, "You need to select some %s" % self.get_checkbox_object_name())
  24. return HttpResponseRedirect(reverse(self.current_view))
  25. raw_objects = self.model.objects.in_bulk(ids)
  26. objects = (raw_objects[int(id)] for id in ids)
  27. return getattr(self, action)(request, objects)
  28. class IndexView(TemplateView):
  29. template_name = 'dashboard/index.html'
  30. class MenuItem(object):
  31. def __init__(self, description, view_name):
  32. self.description = description
  33. self.url = reverse(view_name)
  34. def get_menu_items(self):
  35. MenuItem = IndexView.MenuItem
  36. # This needs to be configurable per project and permission based
  37. return (
  38. MenuItem('See order statistics', 'dashboard:order-summary'),
  39. MenuItem('Manage orders', 'dashboard:order-list'),
  40. MenuItem('View reports', 'dashboard:reports-index'),
  41. MenuItem('User management', 'dashboard:users-index'),
  42. MenuItem('Content block management', 'dashboard:promotion-list'),
  43. MenuItem('Catalogue management', 'dashboard:catalogue-product-list'),
  44. )
  45. def get_context_data(self, **kwargs):
  46. context = super(IndexView, self).get_context_data(**kwargs)
  47. context['menu_items'] = self.get_menu_items()
  48. return context