from django.shortcuts import render from django.utils.translation import gettext as _ from django.contrib.auth import get_user_model from django.urls import reverse from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin from django.views.generic.edit import UpdateView, DeleteView from SharixAdmin.forms import UserDataForm #class AccountUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView): #model = get_user_model() #form_class = UserDataForm #template_name = "SharixAdmin/account_form.html" #def get_queryset(self): # Возвращаем текущего пользователя #return get_user_model().objects.filter(id=self.request.user.id) #def get_context_data(self, **kwargs): #context = super().get_context_data(**kwargs) #context.update({ #'title': _('Аккаунт'), #'object': self.object, #"current_page": "account" #}) #return context # #def test_func(self): #group_names = ('METASERVICE-ADMIN') #return self.request.user.groups.filter(name=group_names).exists() or self.request.user #def get_success_url(self): #return reverse('account') class AccountUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView): model = get_user_model() form_class = UserDataForm template_name = "SharixAdmin/account_form.html" def get_queryset(self): # Возвращаем текущего пользователя return get_user_model().objects.all() def get_object(self, queryset=None): # Получаем объект по pk или slug pk = self.kwargs.get('pk') if pk is not None: try: obj = self.model.objects.get(pk=pk) except self.model.DoesNotExist: raise Http404(_("No %(verbose_name)s found matching the query") % {'verbose_name': self.model._meta.verbose_name}) return obj def test_func(self): # Проверяем, что пользователь имеет право редактировать свой аккаунт account = self.get_object() if account == self.request.user: return True else: return False def get_success_url(self): return reverse('account') class AccountDelete(UserPassesTestMixin, DeleteView): model = get_user_model() template_name = "SharixAdmin/account_delete.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'title': 'Аккаунт', 'object': self.object, "current_page": "account" }) return context def get_success_url(self): return reverse('account') def test_func(self) -> bool or None: group_names = ('METASERVICE-ADMIN') if bool(self.request.user.groups.filter(name=group_names)) or self.request.user: return True return False def AccountView(request): context = { 'title':_('Личный кабинет'), 'current_page': 'account' } return render(request, "SharixAdmin/account.html", context)