diff --git a/users/forms.py b/users/forms.py index bc88a1f4..5669b155 100644 --- a/users/forms.py +++ b/users/forms.py @@ -299,6 +299,11 @@ class ResetPasswordForm(forms.Form): email = forms.EmailField(max_length=255) +class ResendConfirmationEmailForm(forms.Form): + """Formulaire de renvoie du mail de confirmation""" + pass + + class MassArchiveForm(forms.Form): """Formulaire d'archivage des users inactif. Prend en argument du formulaire la date de depart avant laquelle archiver les @@ -344,6 +349,7 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): self.fields["room"].label = _("Room") self.fields["room"].empty_label = _("No room") self.fields["school"].empty_label = _("Select a school") + self.initial["email"] = kwargs["instance"].email class Meta: model = Adherent @@ -390,6 +396,22 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): remove_user_room(room) return + def save(self, commit=True): + """On met à jour l'état de l'utilisateur en fonction de son mail""" + user = super(AdherentForm, self).save(commit=False) + + if user.email != self.initial["email"]: + # Send a confirmation email + if user.state in [User.STATE_ACTIVE, User.STATE_DISABLED, User.STATE_NOT_YET_ACTIVE, User.STATE_EMAIL_NOT_YET_CONFIRMED]: + user.state = User.STATE_EMAIL_NOT_YET_CONFIRMED + user.confirm_email_address_mail() + + # Always keep the oldest change date + if user.email_change_date is None: + user.email_change_date = timezone.now() + + return user + class AdherentCreationForm(AdherentForm): """Formulaire de création d'un user. diff --git a/users/management/commands/disable_emailnotyetconfirmed.py b/users/management/commands/disable_emailnotyetconfirmed.py index 404b5004..b2678c19 100644 --- a/users/management/commands/disable_emailnotyetconfirmed.py +++ b/users/management/commands/disable_emailnotyetconfirmed.py @@ -34,7 +34,8 @@ class Command(BaseCommand): days = OptionalUser.get_cached_value("disable_emailnotyetconfirmed") users_to_disable = ( User.objects.filter(state=User.STATE_EMAIL_NOT_YET_CONFIRMED) - .filter(registered__lte=timezone.now() - timedelta(days=days)) + .exclude(email_change_date__is_null=True) + .filter(email_change_date__lte=timezone.now() - timedelta(days=days)) .distinct() ) print("Disabling " + str(users_to_disable.count()) + " users.") diff --git a/users/models.py b/users/models.py index 12795a67..8671fba0 100755 --- a/users/models.py +++ b/users/models.py @@ -226,6 +226,7 @@ class User( shortcuts_enabled = models.BooleanField( verbose_name=_("enable shortcuts on Re2o website"), default=True ) + email_change_date = None USERNAME_FIELD = "pseudo" REQUIRED_FIELDS = ["surname", "email"] @@ -879,7 +880,10 @@ class User( def confirm_mail(self): """Marque l'email de l'utilisateur comme confirmé""" - # Let the "set_active" method handle + # Reset the email change date + self.email_change_date = None + + # Let the "set_active" method handle the rest self.state = self.STATE_NOT_YET_ACTIVE self.set_active() diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index 0bd25f75..cb8358a8 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -38,7 +38,36 @@ with this program; if not, write to the Free Software Foundation, Inc.,