SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 2debd764 authored by George Katsikas's avatar George Katsikas :goat:
Browse files

propagate user mail change to profile if verified

fixes #306
parent 07510e78
No related branches found
No related tags found
No related merge requests found
...@@ -297,15 +297,9 @@ class AddProfileEmailForm(forms.ModelForm): ...@@ -297,15 +297,9 @@ class AddProfileEmailForm(forms.ModelForm):
"""Mark the email as still_valid but not primary.""" """Mark the email as still_valid but not primary."""
self.instance.profile = self.profile self.instance.profile = self.profile
if self.request:
is_editing_self = self.request.user.contributor.profile == self.profile
is_ed_admin = self.request.user.contributor.is_ed_admin
self.instance.verified = is_editing_self or is_ed_admin
self.instance.still_valid = True self.instance.still_valid = True
self.instance.primary = False self.instance.primary = False
self.instance.added_by = self.request.user.contributor self.instance.added_by = self.request.user.contributor if self.request else None
return super().save() return super().save()
......
...@@ -231,6 +231,9 @@ class ProfileEmail(models.Model): ...@@ -231,6 +231,9 @@ class ProfileEmail(models.Model):
) )
primary = models.BooleanField(default=False) primary = models.BooleanField(default=False)
if TYPE_CHECKING:
objects: models.Manager["ProfileEmail"]
class Meta: class Meta:
unique_together = ["profile", "email"] unique_together = ["profile", "email"]
ordering = ["-primary", "-still_valid", "email"] ordering = ["-primary", "-still_valid", "email"]
...@@ -261,6 +264,14 @@ class ProfileEmail(models.Model): ...@@ -261,6 +264,14 @@ class ProfileEmail(models.Model):
kwargs={"email_id": self.id, "token": self.verification_token}, kwargs={"email_id": self.id, "token": self.verification_token},
) )
def set_primary(self):
"""
Sets this email as the primary email for the Profile, unsetting others.
"""
self.profile.emails.update(primary=False)
self.primary = True
self.save()
def get_profiles(slug): def get_profiles(slug):
""" """
......
...@@ -360,21 +360,44 @@ class UpdateUserDataForm(forms.ModelForm): ...@@ -360,21 +360,44 @@ class UpdateUserDataForm(forms.ModelForm):
self.fields["last_name"].widget.attrs["readonly"] = True self.fields["last_name"].widget.attrs["readonly"] = True
def clean_email(self): def clean_email(self):
if email := self.cleaned_data.get("email"): # Guard against empty email address
other_users = User.objects.filter(email=email).exclude(pk=self.instance.pk) if not (email := self.cleaned_data.get("email")):
if other_users.exists(): raise ValidationError("The email address cannot be empty.")
self.add_error(
"email", other_users = User.objects.filter(email=email).exclude(pk=self.instance.pk)
"This email is already in use by another user. " if other_users.exists():
"If it belongs to you and you have forgotten your credentials, " raise ValidationError(
"use the email in place of your username and/or reset your password.", "This email is already in use by another user. "
) "If it belongs to you and you have forgotten your credentials, "
# other_profiles = Profile.objects.filter(emails__email=email).exclude( "use the email in place of your username and/or reset your password.",
# user=self.instance )
# )
# if other_profiles.exists(): profile_email, created = ProfileEmail.objects.get_or_create(
email=email, profile=self.instance.contributor.profile
)
# If just created, it needs to be verified
if created:
profile_email.send_verification_email()
raise ValidationError(
"This email is not yet verified. Please check your inbox for a verification email."
)
# Existing, but of another User
elif profile_email.profile.contributor != self.instance.contributor:
raise ValidationError(
"This email is already declared as belonging to another person. "
"Please contact tech support.",
)
# Existing, of this User, but not verified
elif not profile_email.verified:
profile_email.send_verification_email()
raise ValidationError(
"This email is not yet verified. Please check your inbox for a verification email."
)
return email or self.instance.email # Existing, of this User, and verified
profile_email.set_primary()
return email
def clean_last_name(self): def clean_last_name(self):
"""Make sure the `last_name` cannot be saved via this form.""" """Make sure the `last_name` cannot be saved via this form."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment