Allow new users to create character using admin

This commit is contained in:
Gabriel Augendre 2022-10-31 17:38:55 +01:00
parent cdb7d1c27e
commit ccd4b7f1e2
3 changed files with 27 additions and 1 deletions

View file

@ -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/<str:activation_key>/",
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),

View file

@ -19,6 +19,9 @@
</head>
<body>
<div class="container-fluid">
{% if user.is_authenticated %}
<a href="{% url "logout" %}">Déconnexion</a>
{% endif %}
{% block content %}
{% include "common/hello-random.html" %}
{% endblock %}

View file

@ -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