mirror of
https://github.com/ae-utbm/sith.git
synced 2026-09-21 19:54:23 +00:00
make existing users approve CGU after login
This commit is contained in:
+37
-19
@@ -30,9 +30,7 @@ from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.staticfiles.management.commands.collectstatic import (
|
||||
staticfiles_storage,
|
||||
)
|
||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import transaction
|
||||
from django.forms import (
|
||||
@@ -43,6 +41,7 @@ from django.forms import (
|
||||
Widget,
|
||||
)
|
||||
from django.urls import reverse
|
||||
from django.utils.functional import lazy
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -110,6 +109,34 @@ class FutureDateTimeField(forms.DateTimeField):
|
||||
return {"min": widget.format_value(now())}
|
||||
|
||||
|
||||
class CGUApprovalField(forms.BooleanField):
|
||||
cgu_file_id = settings.SITH_CGU_FILE_ID
|
||||
default_error_messages = {"required": _("You must approve the terms of service.")}
|
||||
__label = None
|
||||
|
||||
def __init__(self, *, label_suffix: str | None = "", **kwargs):
|
||||
# Because the core app of the sith is so huge,
|
||||
# and because we require a url from the latter,
|
||||
# putting the reverse into the __init__ will result in it
|
||||
# being evaluated at server startup time (even with reverse_lazy).
|
||||
# This will result in a circular import.
|
||||
# Thus, we must keep the label in its own property and force it to be lazy.
|
||||
kwargs["label"] = lazy(self.get_label, str)
|
||||
kwargs["required"] = True
|
||||
super().__init__(label_suffix=label_suffix, **kwargs)
|
||||
|
||||
def get_label(self):
|
||||
if not self.__label:
|
||||
self.__label = mark_safe(
|
||||
_(
|
||||
"I have read and I approve the "
|
||||
'<a href="%(url)s" target="_blank">Terms of Service</a>'
|
||||
)
|
||||
% {"url": reverse("core:page", kwargs={"page_name": self.cgu_file_id})}
|
||||
)
|
||||
return self.__label
|
||||
|
||||
|
||||
# Forms
|
||||
|
||||
|
||||
@@ -149,23 +176,14 @@ class RegisteringForm(UserCreationForm):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ("first_name", "last_name", "email", "cgu_approved")
|
||||
field_classes = {"email": AntiSpamEmailField}
|
||||
field_classes = {"email": AntiSpamEmailField, "cgu_approved": CGUApprovalField}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields["cgu_approved"].required = True
|
||||
self.fields["cgu_approved"].label_suffix = ""
|
||||
self.fields["cgu_approved"].label = mark_safe(
|
||||
_(
|
||||
"I have read and I approve the "
|
||||
'<a href="%(url)s" target="_blank">End User License Agreement</a>'
|
||||
)
|
||||
% {
|
||||
"url": reverse(
|
||||
"core:download", kwargs={"file_id": settings.SITH_CGU_FILE_ID}
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
class CGUApprovalForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["cgu_approved"]
|
||||
field_classes = {"cgu_approved": CGUApprovalField}
|
||||
|
||||
|
||||
class UserProfileForm(forms.ModelForm):
|
||||
|
||||
+33
-3
@@ -27,12 +27,12 @@ from datetime import timedelta
|
||||
# This file contains all the views that concern the user model
|
||||
from operator import itemgetter
|
||||
from smtplib import SMTPException
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import login, views
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
|
||||
from django.contrib.auth.forms import SetPasswordForm
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.core.exceptions import PermissionDenied
|
||||
@@ -60,6 +60,7 @@ from honeypot.decorators import check_honeypot
|
||||
from core.auth.mixins import CanEditMixin, CanEditPropMixin, CanViewMixin
|
||||
from core.models import Gift, Preferences, User
|
||||
from core.views.forms import (
|
||||
CGUApprovalForm,
|
||||
GiftForm,
|
||||
LoginForm,
|
||||
RegisteringForm,
|
||||
@@ -71,6 +72,7 @@ from core.views.forms import (
|
||||
from core.views.mixins import FragmentMixin, TabedViewMixin, UseFragmentsMixin
|
||||
from counter.models import Refilling, Selling
|
||||
from eboutic.models import Invoice
|
||||
from sith import settings
|
||||
from trombi.views import UserTrombiForm
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -82,9 +84,16 @@ class SithLoginView(views.LoginView):
|
||||
|
||||
template_name = "core/login.jinja"
|
||||
authentication_form = LoginForm
|
||||
form_class = PasswordChangeForm
|
||||
redirect_authenticated_user = True
|
||||
|
||||
def get_success_url(self) -> str:
|
||||
redirect_to = self.get_redirect_url()
|
||||
default_url = self.get_default_redirect_url()
|
||||
if not self.request.user.cgu_approved:
|
||||
query = {"next": redirect_to} if redirect_to else {}
|
||||
return reverse("core:approve_cgu", query=query)
|
||||
return redirect_to or default_url
|
||||
|
||||
|
||||
class SithPasswordChangeView(views.PasswordChangeView):
|
||||
"""Allows a user to change its password."""
|
||||
@@ -188,6 +197,27 @@ class UserCreationView(FormView):
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class CGUApprovalView(views.RedirectURLMixin, UpdateView):
|
||||
form_class = CGUApprovalForm
|
||||
next_page = settings.LOGIN_REDIRECT_URL
|
||||
template_name = "core/cgu_approve.jinja"
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if self.request.user.is_anonymous:
|
||||
return redirect("core:login")
|
||||
if self.request.user.cgu_approved:
|
||||
return redirect(self.get_success_url())
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_object(self, *args, **kwargs):
|
||||
return self.request.user
|
||||
|
||||
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
||||
return super().get_context_data(**kwargs) | {
|
||||
self.redirect_field_name: self.get_redirect_url()
|
||||
}
|
||||
|
||||
|
||||
class UserMeRedirect(LoginRequiredMixin, RedirectView):
|
||||
def get_redirect_url(self, *args, **kwargs):
|
||||
if remaining := kwargs.get("remaining_path"):
|
||||
|
||||
Reference in New Issue
Block a user