mirror of
https://github.com/ae-utbm/sith.git
synced 2026-09-26 14:14:20 +00:00
use DatetimeField to store cgu approval
Au lieu de dire si l'utilisateur a approuvé les CGUs ou non, on dit quand est-ce qu'il les a approuvées. Si la date de la dernière approbation est antérieure à la date de la dernière révision des CGUs, alors on demande à nouveau de les approuver.
This commit is contained in:
+26
-4
@@ -110,6 +110,12 @@ class FutureDateTimeField(forms.DateTimeField):
|
||||
|
||||
|
||||
class CGUApprovalField(forms.BooleanField):
|
||||
"""Form field with a checkbox to approve the CGUs.
|
||||
|
||||
The checkbox must be checked to be valid.
|
||||
If valid, then the value of the field is the current timestamp.
|
||||
"""
|
||||
|
||||
default_error_messages = {"required": _("You must approve the terms of service.")}
|
||||
__label = None
|
||||
|
||||
@@ -124,6 +130,9 @@ class CGUApprovalField(forms.BooleanField):
|
||||
kwargs["required"] = True
|
||||
super().__init__(label_suffix=label_suffix, **kwargs)
|
||||
|
||||
def to_python(self, value):
|
||||
return now() if super().to_python(value) else None
|
||||
|
||||
def get_label(self):
|
||||
if not self.__label:
|
||||
url = reverse("core:page", kwargs={"page_name": settings.SITH_CGU_PAGE})
|
||||
@@ -175,15 +184,28 @@ class RegisteringForm(UserCreationForm):
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ("first_name", "last_name", "email", "cgu_approved")
|
||||
field_classes = {"email": AntiSpamEmailField, "cgu_approved": CGUApprovalField}
|
||||
fields = ("first_name", "last_name", "email", "cgu_approved_at")
|
||||
field_classes = {
|
||||
"email": AntiSpamEmailField,
|
||||
"cgu_approved_at": CGUApprovalField,
|
||||
}
|
||||
|
||||
|
||||
class CGUApprovalForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["cgu_approved"]
|
||||
field_classes = {"cgu_approved": CGUApprovalField}
|
||||
fields = ["cgu_approved_at"]
|
||||
field_classes = {"cgu_approved_at": CGUApprovalField}
|
||||
|
||||
def __init__(self, *args, instance: User | None = None, **kwargs):
|
||||
if instance:
|
||||
# If this form is displayed,
|
||||
# then we want the user to explicitly check the button.
|
||||
# So we pretend cgu were never approved (even if they were).
|
||||
# If we didn't do that, the button would be initially checked,
|
||||
# even if the approval was done before the last CGU version.
|
||||
instance.cgu_approved_at = False
|
||||
super().__init__(*args, instance=instance, **kwargs)
|
||||
|
||||
|
||||
class UserProfileForm(forms.ModelForm):
|
||||
|
||||
Reference in New Issue
Block a user