-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connexion: Utilisation du modèle EmailAddress et refonte du parcours de modification d'adresse mail [GEN-2216] #5088
base: master
Are you sure you want to change the base?
Changes from all commits
804321d
f5f1311
4bd1bb3
2602f6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||
|
||||||
import httpx | ||||||
from allauth.account.adapter import get_adapter | ||||||
from allauth.account.models import EmailAddress | ||||||
from django.conf import settings | ||||||
from django.contrib import messages | ||||||
from django.contrib.auth import login | ||||||
|
@@ -14,7 +15,6 @@ | |||||
|
||||||
from itou.prescribers.models import PrescriberOrganization | ||||||
from itou.users.enums import KIND_EMPLOYER, KIND_PRESCRIBER, IdentityProvider, UserKind | ||||||
from itou.users.models import User | ||||||
from itou.utils import constants as global_constants | ||||||
from itou.utils.constants import ITOU_HELP_CENTER_URL | ||||||
from itou.utils.urls import add_url_params, get_absolute_url | ||||||
|
@@ -173,13 +173,15 @@ def inclusion_connect_activate_account(request): | |||||
return HttpResponseRedirect(params.get("previous_url", reverse("search:employers_home"))) | ||||||
|
||||||
user_kind = params.get("user_kind") | ||||||
user = User.objects.filter(email=email).first() | ||||||
email_address = EmailAddress.objects.filter(email__iexact=email).first() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Et a priori à chaque fois qu'on utilise |
||||||
|
||||||
if not user: | ||||||
if not email_address: | ||||||
params["register"] = True | ||||||
request.GET = params | ||||||
return inclusion_connect_authorize(request) | ||||||
|
||||||
user = email_address.user | ||||||
|
||||||
if user.kind != user_kind: | ||||||
_add_user_kind_error_message(request, user, request.GET.get("user_kind")) | ||||||
return HttpResponseRedirect(params.get("previous_url", reverse("search:employers_home"))) | ||||||
|
@@ -289,7 +291,7 @@ def inclusion_connect_callback(request): | |||||
try: | ||||||
user, _ = ic_user_data.create_or_update_user(is_login=ic_state.data.get("is_login")) | ||||||
except InvalidKindException: | ||||||
existing_user = User.objects.get(email=user_data["email"]) | ||||||
existing_user = EmailAddress.objects.get(email__iexact=user_data["email"]).user | ||||||
_add_user_kind_error_message(request, existing_user, user_kind) | ||||||
is_successful = False | ||||||
except MultipleSubSameEmailException as e: | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% autoescape off %} | ||
Nous avons bien enregistré votre demande d'inscription et vous remercions de votre confiance. | ||
|
||
Afin de finaliser votre inscription, cliquez sur le lien suivant : | ||
|
||
{{ activate_url }} | ||
|
||
Si vous n'êtes pas à l'origine de cette demande, merci de ne pas prendre en compte ce message. | ||
{% include "layout/base_email_signature.txt" %} | ||
{% endautoescape %} | ||
Comment on lines
+1
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fonctionnalité de django-allauth, ce gabarit sera utilisé lorsque |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from django.db import migrations | ||
from django.db.models import OuterRef, Subquery | ||
|
||
|
||
def create_email_addresses_for_users(apps, schema_editor): | ||
User = apps.get_model("users", "User") | ||
EmailAddress = apps.get_model("account", "EmailAddress") | ||
|
||
# Get all those values of User.email where there is no corresponding EmailAddresses instance | ||
users_missing_addresses = ( | ||
User.objects.prefetch_related("emailaddress_set") | ||
.annotate(email_exists=Subquery(EmailAddress.objects.filter(email=OuterRef("email")).values("id")[:1])) | ||
.filter(email_exists__isnull=True) | ||
.values("id", "email") | ||
) | ||
|
||
EmailAddress.objects.bulk_create( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Il y en a beaucoup ? Ça vaudrait ptet le coup de faire un migration |
||
EmailAddress(user_id=x["id"], email=x["email"], primary=True, verified=False) for x in users_missing_addresses | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
""" | ||
This migration was created at a time when not all User email addresses had an associated EmailAddress. | ||
It ensures that EmailAddress instances are created where they are not existing. | ||
Of course this means that the migration can be squashed later. | ||
""" | ||
|
||
dependencies = [ | ||
("users", "0014_alter_jobseekerprofile_birthdate__add_index"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
create_email_addresses_for_users, | ||
reverse_code=migrations.RunPython.noop, | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tu peux rajouter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bien vu merci ! |
||
] |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||||||||||||||
from collections import Counter | ||||||||||||||||||
|
||||||||||||||||||
from allauth.account.forms import default_token_generator | ||||||||||||||||||
from allauth.account.models import EmailAddress | ||||||||||||||||||
from allauth.account.utils import user_pk_to_url_str | ||||||||||||||||||
from citext import CIEmailField | ||||||||||||||||||
from django.conf import settings | ||||||||||||||||||
|
@@ -75,6 +76,11 @@ def autocomplete(self, search_string, current_user): | |||||||||||||||||
) | ||||||||||||||||||
return queryset[:10] | ||||||||||||||||||
|
||||||||||||||||||
def create_user(self, username, email=None, password=None, **extra_fields): | ||||||||||||||||||
if email is not None and EmailAddress.objects.filter(email__icontains=email).exists(): | ||||||||||||||||||
raise ValidationError(User.ERROR_EMAIL_ALREADY_EXISTS) | ||||||||||||||||||
return super().create_user(username, email, password, **extra_fields) | ||||||||||||||||||
|
||||||||||||||||||
def get_duplicated_pole_emploi_ids(self): | ||||||||||||||||||
""" | ||||||||||||||||||
Returns an array of `pole_emploi_id` used more than once: | ||||||||||||||||||
|
@@ -350,6 +356,13 @@ def save(self, *args, **kwargs): | |||||||||||||||||
|
||||||||||||||||||
self.set_old_values() | ||||||||||||||||||
|
||||||||||||||||||
# Ensure EmailAddress consistency | ||||||||||||||||||
if self.email is None: | ||||||||||||||||||
self.emailaddress_set.all().delete() | ||||||||||||||||||
elif self.email not in self.emailaddress_set.values_list("email", flat=True): | ||||||||||||||||||
self.emailaddress_set.all().delete() | ||||||||||||||||||
self.emailaddress_set.create(email=self.email, primary=True) | ||||||||||||||||||
|
||||||||||||||||||
def get_full_name(self): | ||||||||||||||||||
""" | ||||||||||||||||||
Return the first_name plus the last_name, with a space in between. | ||||||||||||||||||
|
@@ -706,7 +719,10 @@ def email_already_exists(cls, email, exclude_pk=None): | |||||||||||||||||
is case-insensitive. Consider [email protected] and [email protected] as | ||||||||||||||||||
the same email. | ||||||||||||||||||
""" | ||||||||||||||||||
queryset = cls.objects.filter(email__iexact=email) | ||||||||||||||||||
# Uses EmailAddress to capture emails in-use but thus far unverified. | ||||||||||||||||||
queryset = cls.objects.filter( | ||||||||||||||||||
id__in=EmailAddress.objects.filter(email__iexact=email).values_list("user_id", flat=True) | ||||||||||||||||||
) | ||||||||||||||||||
if exclude_pk: | ||||||||||||||||||
queryset = queryset.exclude(pk=exclude_pk) | ||||||||||||||||||
Comment on lines
+723
to
727
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
return queryset.exists() | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pourquoi ne pas garder celle de django-allauth ?