Added basic buro right handling while updating member

This commit is contained in:
Alseidon 2021-01-27 22:25:58 +01:00 committed by Martin Pépin
parent ba9aa06b4f
commit bf6d6d6430
No known key found for this signature in database
GPG key ID: E7520278B1774448
2 changed files with 12 additions and 0 deletions

View file

@ -1,6 +1,7 @@
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.utils.translation import gettext_lazy as _
from bds.models import BDSProfile
@ -8,6 +9,8 @@ User = get_user_model()
class UserForm(forms.ModelForm):
is_buro = forms.BooleanField(label=_("membre du Burô"), required=False)
class Meta:
model = User
fields = ["email", "first_name", "last_name"]

View file

@ -3,6 +3,7 @@ import csv
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.models import Permission
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.urls import reverse, reverse_lazy
@ -40,6 +41,9 @@ class UserUpdateView(StaffRequiredMixin, MultipleFormView):
"profile": ProfileForm,
}
def get_user_initial(self):
return {"is_buro": self.get_user_instance().has_perm("bds.is_team")}
def dispatch(self, request, *args, **kwargs):
self.user = get_object_or_404(User, pk=self.kwargs["pk"])
return super().dispatch(request, *args, **kwargs)
@ -56,6 +60,11 @@ class UserUpdateView(StaffRequiredMixin, MultipleFormView):
def form_valid(self, forms):
user = forms["user"].save()
profile = forms["profile"].save(commit=False)
perm = Permission.objects.get(content_type__app_label="bds", codename="is_team")
if forms["user"].cleaned_data["is_buro"]:
user.user_permissions.add(perm)
else:
user.user_permissions.remove(perm)
profile.user = user
profile.save()
messages.success(self.request, _("Profil mis à jour avec succès !"))