1
0

account.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from django.shortcuts import render
  2. from django.utils.translation import gettext as _
  3. from django.contrib.auth import get_user_model
  4. from django.urls import reverse
  5. from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
  6. from django.views.generic.edit import UpdateView, DeleteView
  7. from SharixAdmin.forms import UserDataForm
  8. #class AccountUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
  9. #model = get_user_model()
  10. #form_class = UserDataForm
  11. #template_name = "SharixAdmin/account_form.html"
  12. #def get_queryset(self):
  13. # Возвращаем текущего пользователя
  14. #return get_user_model().objects.filter(id=self.request.user.id)
  15. #def get_context_data(self, **kwargs):
  16. #context = super().get_context_data(**kwargs)
  17. #context.update({
  18. #'title': _('Аккаунт'),
  19. #'object': self.object,
  20. #"current_page": "account"
  21. #})
  22. #return context
  23. #
  24. #def test_func(self):
  25. #group_names = ('METASERVICE-ADMIN')
  26. #return self.request.user.groups.filter(name=group_names).exists() or self.request.user
  27. #def get_success_url(self):
  28. #return reverse('account')
  29. class AccountUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
  30. model = get_user_model()
  31. form_class = UserDataForm
  32. template_name = "SharixAdmin/account_form.html"
  33. def get_queryset(self):
  34. # Возвращаем текущего пользователя
  35. return get_user_model().objects.all()
  36. def get_object(self, queryset=None):
  37. # Получаем объект по pk или slug
  38. pk = self.kwargs.get('pk')
  39. if pk is not None:
  40. try:
  41. obj = self.model.objects.get(pk=pk)
  42. except self.model.DoesNotExist:
  43. raise Http404(_("No %(verbose_name)s found matching the query") % {'verbose_name': self.model._meta.verbose_name})
  44. return obj
  45. def test_func(self):
  46. # Проверяем, что пользователь имеет право редактировать свой аккаунт
  47. account = self.get_object()
  48. if account == self.request.user:
  49. return True
  50. else:
  51. return False
  52. def get_success_url(self):
  53. return reverse('account')
  54. class AccountDelete(UserPassesTestMixin, DeleteView):
  55. model = get_user_model()
  56. template_name = "SharixAdmin/account_delete.html"
  57. def get_context_data(self, **kwargs):
  58. context = super().get_context_data(**kwargs)
  59. context.update({
  60. 'title': 'Аккаунт',
  61. 'object': self.object,
  62. "current_page": "account"
  63. })
  64. return context
  65. def get_success_url(self):
  66. return reverse('account')
  67. def test_func(self) -> bool or None:
  68. group_names = ('METASERVICE-ADMIN')
  69. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user:
  70. return True
  71. return False
  72. def AccountView(request):
  73. context = {
  74. 'title':_('Личный кабинет'),
  75. 'current_page': 'account'
  76. }
  77. return render(request, "SharixAdmin/account.html", context)