From ccd4b7f1e2d0ee2628f4a3113a6c4bbde92cf874 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 31 Oct 2022 17:38:55 +0100 Subject: [PATCH] Allow new users to create character using admin --- src/charasheet/urls.py | 7 ++++++- src/common/templates/common/base.html | 3 +++ src/common/views.py | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/charasheet/urls.py b/src/charasheet/urls.py index 799685d..77c6d7f 100644 --- a/src/charasheet/urls.py +++ b/src/charasheet/urls.py @@ -21,7 +21,7 @@ from django.urls import include, path from django_registration.backends.activation.views import RegistrationView from common.forms import RegistrationForm -from common.views import hello_world +from common.views import ActivationView, hello_world urlpatterns = [ path("logout/", logout, {"next_page": settings.LOGOUT_REDIRECT_URL}, name="logout"), @@ -30,6 +30,11 @@ urlpatterns = [ RegistrationView.as_view(form_class=RegistrationForm), name="django_registration_register", ), + path( + "accounts/activate//", + ActivationView.as_view(), + name="django_registration_activate", + ), path("accounts/", include("django_registration.backends.activation.urls")), path("accounts/", include("django.contrib.auth.urls")), path("admin/", admin.site.urls), diff --git a/src/common/templates/common/base.html b/src/common/templates/common/base.html index d21d417..5eec256 100644 --- a/src/common/templates/common/base.html +++ b/src/common/templates/common/base.html @@ -19,6 +19,9 @@
+ {% if user.is_authenticated %} + Déconnexion + {% endif %} {% block content %} {% include "common/hello-random.html" %} {% endblock %} diff --git a/src/common/views.py b/src/common/views.py index 8009758..4cfd429 100644 --- a/src/common/views.py +++ b/src/common/views.py @@ -1,7 +1,25 @@ +from django.contrib.auth.models import Permission from django.core.handlers.wsgi import WSGIRequest from django.http import HttpResponse from django.shortcuts import redirect +from django_registration.backends.activation.views import ( + ActivationView as BaseActivationView, +) def hello_world(request: WSGIRequest) -> HttpResponse: return redirect("character:list") + + +class ActivationView(BaseActivationView): + def activate(self, *args, **kwargs): + user = super().activate(*args, **kwargs) + perm = Permission.objects.get( + content_type__app_label="character", + content_type__model="character", + codename="add_character", + ) + user.user_permissions.add(perm) + user.is_staff = True + user.save() + return user