mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-25 22:22:26 +00:00
Move some code away from the forms
This commit is contained in:
parent
55cde72512
commit
f790dca202
3 changed files with 31 additions and 33 deletions
|
@ -333,7 +333,6 @@ 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.is_anon = kwargs["user"].is_anonymous()
|
||||
|
||||
class Meta:
|
||||
model = Adherent
|
||||
|
@ -382,23 +381,6 @@ 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=commit)
|
||||
|
||||
if not self.is_anon and self.initial["email"] and 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.email_state = User.EMAIL_STATE_PENDING
|
||||
self.should_send_confirmation_email = True
|
||||
|
||||
# Always keep the oldest change date
|
||||
if user.email_change_date is None:
|
||||
user.email_change_date = timezone.now()
|
||||
|
||||
user.save()
|
||||
return user
|
||||
|
||||
|
||||
class AdherentCreationForm(AdherentForm):
|
||||
"""Formulaire de création d'un user.
|
||||
|
@ -518,7 +500,6 @@ class AdherentCreationForm(AdherentForm):
|
|||
an email to init the password should be sent"""
|
||||
# Save the provided password in hashed format
|
||||
user = super(AdherentForm, self).save(commit=False)
|
||||
user.email_change_date = timezone.now()
|
||||
|
||||
is_set_password_allowed = OptionalUser.get_cached_value("allow_set_password_during_user_creation")
|
||||
send_email = not is_set_password_allowed or self.cleaned_data.get("init_password_by_mail")
|
||||
|
|
|
@ -800,6 +800,26 @@ class User(
|
|||
)
|
||||
return
|
||||
|
||||
def send_confirm_email_if_necessary(self, request):
|
||||
"""Update the user's email state
|
||||
Returns whether an email was sent"""
|
||||
# Only update the state if the email changed
|
||||
if self.__original_email == self.email:
|
||||
return False
|
||||
|
||||
# Archived users shouldn't get an email
|
||||
if self.state not in [self.STATE_ACTIVE, self.STATE_DISABLED, self.STATE_NOT_YET_ACTIVE]:
|
||||
return False
|
||||
|
||||
# Always keep the oldest change date
|
||||
if self.email_change_date is None:
|
||||
self.email_change_date = timezone.now()
|
||||
|
||||
self.email_state = self.EMAIL_STATE_PENDING
|
||||
self.confirm_email_address_mail(request)
|
||||
|
||||
return True
|
||||
|
||||
def confirm_email_before_date(self):
|
||||
if self.email_change_date is None or self.email_state == self.EMAIL_STATE_VERIFIED:
|
||||
return None
|
||||
|
@ -810,6 +830,7 @@ class User(
|
|||
def confirm_email_address_mail(self, request):
|
||||
"""Prend en argument un request, envoie un mail pour
|
||||
confirmer l'adresse"""
|
||||
# Create the request and send the email
|
||||
req = Request()
|
||||
req.type = Request.EMAIL
|
||||
req.user = self
|
||||
|
|
|
@ -124,11 +124,9 @@ def new_user(request):
|
|||
is_set_password_allowed = OptionalUser.get_cached_value("allow_set_password_during_user_creation")
|
||||
|
||||
if user.is_valid():
|
||||
user = user.save()
|
||||
|
||||
# Use "is False" so that if None, the email is sent
|
||||
if is_set_password_allowed and user.should_send_password_reset_email is False:
|
||||
user.confirm_email_address_mail(request)
|
||||
if is_set_password_allowed and user.should_send_password_reset_email:
|
||||
user.send_confirm_email_if_necessary(request)
|
||||
messages.success(
|
||||
request,
|
||||
_("The user %s was created, a confirmation email was sent.")
|
||||
|
@ -142,6 +140,7 @@ def new_user(request):
|
|||
% user.pseudo,
|
||||
)
|
||||
|
||||
user = user.save()
|
||||
return redirect(reverse("users:profil", kwargs={"userid": str(user.id)}))
|
||||
|
||||
# Anonymous users are allowed to create new accounts
|
||||
|
@ -223,13 +222,12 @@ def edit_info(request, user, userid):
|
|||
)
|
||||
if user_form.is_valid():
|
||||
if user_form.changed_data:
|
||||
if user.send_confirm_email_if_necessary(request):
|
||||
messages.success(request, _("Sent a new confirmation email."))
|
||||
|
||||
user = user_form.save()
|
||||
messages.success(request, _("The user was edited."))
|
||||
|
||||
if user_form.should_send_confirmation_email:
|
||||
user.confirm_email_address_mail(request)
|
||||
messages.success(request, _("Sent a new confirmation email."))
|
||||
|
||||
return redirect(reverse("users:profil", kwargs={"userid": str(userid)}))
|
||||
return form(
|
||||
{"userform": user_form, "action_name": _("Edit")},
|
||||
|
@ -544,14 +542,12 @@ def edit_email_settings(request, user_instance, **_kwargs):
|
|||
)
|
||||
if email_settings.is_valid():
|
||||
if email_settings.changed_data:
|
||||
if user_instance.send_confirm_email_if_necessary(request):
|
||||
messages.success(request, _("An email to confirm your address was sent."))
|
||||
|
||||
email_settings.save()
|
||||
messages.success(request, _("The email settings were edited."))
|
||||
|
||||
# Send confirmation email if necessary
|
||||
if email_settings.should_send_confirmation_email is True:
|
||||
user_instance.confirm_email_address_mail(request)
|
||||
messages.success(request, _("An email to confirm your address was sent."))
|
||||
|
||||
return redirect(
|
||||
reverse("users:profil", kwargs={"userid": str(user_instance.id)})
|
||||
)
|
||||
|
@ -1073,7 +1069,7 @@ def resend_confirmation_email(request, logged_user, userid):
|
|||
messages.error(request, _("The user doesn't exist."))
|
||||
|
||||
if request.method == "POST":
|
||||
user.confirm_email_address_mail(request)
|
||||
user.send_confirm_email_if_necessary(request)
|
||||
messages.success(request, _("An email to confirm your address was sent."))
|
||||
return redirect(reverse("users:profil", kwargs={"userid": userid}))
|
||||
|
||||
|
|
Loading…
Reference in a new issue