Fixe un bug de migration si on n'a pas de db initiale

This commit is contained in:
Tom Hubrecht 2022-01-07 23:58:40 +01:00
parent 0e0f29cba6
commit b2c55b824a

View file

@ -13,8 +13,6 @@ from .utils import generate_password
User = get_user_model() User = get_user_model()
election_perm = Permission.objects.get(codename="election_admin")
faq_perm = Permission.objects.get(codename="faq_admin")
# ############################################################################# # #############################################################################
# Mixin to restrict access to staff members # Mixin to restrict access to staff members
@ -92,10 +90,12 @@ class AccountListView(StaffMemberMixin, ListView):
ctx["cas_users"] = qs.filter(username__startswith="cas__") ctx["cas_users"] = qs.filter(username__startswith="cas__")
ctx["pwd_users"] = qs.filter(username__startswith="pwd__") ctx["pwd_users"] = qs.filter(username__startswith="pwd__")
ctx["e_manager"] = User.objects.with_perm( ctx["e_manager"] = User.objects.with_perm(
election_perm, backend="shared.auth.backends.PwdBackend" Permission.objects.get(codename="election_admin"),
backend="shared.auth.backends.PwdBackend",
) )
ctx["f_manager"] = User.objects.with_perm( ctx["f_manager"] = User.objects.with_perm(
faq_perm, backend="shared.auth.backends.PwdBackend" Permission.objects.get(codename="faq_admin"),
backend="shared.auth.backends.PwdBackend",
) )
return ctx return ctx
@ -141,12 +141,14 @@ class PermissionManagementView(StaffMemberMixin, SuccessMessageMixin, FormView):
user.is_staff = form.cleaned_data["full_admin"] user.is_staff = form.cleaned_data["full_admin"]
# Election admin # Election admin
election_perm = Permission.objects.get(codename="election_admin")
if form.cleaned_data["election_admin"]: if form.cleaned_data["election_admin"]:
election_perm.user_set.add(user) election_perm.user_set.add(user)
else: else:
election_perm.user_set.remove(user) election_perm.user_set.remove(user)
# FAQ admin # FAQ admin
faq_perm = Permission.objects.get(codename="faq_admin")
if form.cleaned_data["faq_admin"]: if form.cleaned_data["faq_admin"]:
faq_perm.user_set.add(user) faq_perm.user_set.add(user)
else: else:
@ -169,9 +171,11 @@ class AdminAccountsView(LoginRequiredMixin, TemplateView):
ctx["admin"] = User.objects.filter(is_staff=True) ctx["admin"] = User.objects.filter(is_staff=True)
ctx["e_manager"] = User.objects.with_perm( ctx["e_manager"] = User.objects.with_perm(
election_perm, backend="shared.auth.backends.PwdBackend" Permission.objects.get(codename="election_admin"),
backend="shared.auth.backends.PwdBackend",
) )
ctx["f_manager"] = User.objects.with_perm( ctx["f_manager"] = User.objects.with_perm(
faq_perm, backend="shared.auth.backends.PwdBackend" Permission.objects.get(codename="faq_admin"),
backend="shared.auth.backends.PwdBackend",
) )
return ctx return ctx