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.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from django.db.models import Q
  2. from django.contrib import messages
  3. from django.contrib.auth.models import User
  4. from django.http import HttpResponseRedirect
  5. from django.core.urlresolvers import reverse
  6. from django.views.generic import ListView, DetailView
  7. from oscar.apps.dashboard.users import forms
  8. from oscar.apps.dashboard.views import BulkEditMixin
  9. class IndexView(ListView, BulkEditMixin):
  10. template_name = 'dashboard/users/index.html'
  11. paginate_by = 25
  12. model = User
  13. actions = ('make_active', 'make_inactive', )
  14. current_view = 'dashboard:users-index'
  15. form_class = forms.UserSearchForm
  16. base_description = 'All users'
  17. description = ''
  18. def get_queryset(self):
  19. queryset = self.model.objects.all().order_by('-date_joined')
  20. self.description = self.base_description
  21. if 'username' not in self.request.GET:
  22. self.form = self.form_class()
  23. return queryset
  24. self.form = self.form_class(self.request.GET)
  25. if not self.form.is_valid():
  26. return queryset
  27. data = self.form.cleaned_data
  28. if data['username']:
  29. queryset = queryset.filter(username__startswith=data['username'])
  30. self.description += " with username matching '%s'" % data['username']
  31. if data['email']:
  32. queryset = queryset.filter(email__startswith=data['email'])
  33. self.description += " with email matching '%s'" % data['email']
  34. if data['name']:
  35. # If the value is two words, then assume they are first name and last name
  36. parts = data['name'].split()
  37. if len(parts) == 2:
  38. queryset = queryset.filter(Q(first_name__istartswith=parts[0]) |
  39. Q(last_name__istartswith=parts[1])).distinct()
  40. else:
  41. queryset = queryset.filter(Q(first_name__istartswith=data['name']) |
  42. Q(last_name__istartswith=data['name'])).distinct()
  43. self.description += " with name matching '%s'" % data['name']
  44. return queryset
  45. def get_context_data(self, **kwargs):
  46. context = super(IndexView, self).get_context_data(**kwargs)
  47. context['form'] = self.form
  48. context['queryset_description'] = self.description
  49. return context
  50. def make_inactive(self, request, users):
  51. return self._change_users_active_status(users, False)
  52. def make_active(self, request, users):
  53. return self._change_users_active_status(users, True)
  54. def _change_users_active_status(self, users, value):
  55. for user in users:
  56. if not user.is_superuser:
  57. user.is_active = value
  58. user.save()
  59. messages.info(self.request, 'Users\' status successfully changed')
  60. return HttpResponseRedirect(reverse(self.current_view))
  61. class UserDetailView(DetailView):
  62. template_name = 'dashboard/users/detail.html'
  63. model = User