Skip to content
Snippets Groups Projects
Commit acf7ecc4 authored by ynerant's avatar ynerant
Browse files

Use phone number validator

parent 6c9cf738
No related branches found
No related tags found
3 merge requests!104Beta,!98Morefront,!91Documents
Pipeline #8340 passed with warnings with stages
in 5 minutes and 6 seconds
......@@ -11,10 +11,10 @@ from django.db import models
from django.db.models import Q
from django.template import loader
from django.urls import reverse, reverse_lazy
from django.utils import timezone
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField
from permission.models import Role
from registration.tokens import email_validation_token
......@@ -34,7 +34,7 @@ class Profile(models.Model):
on_delete=models.CASCADE,
)
phone_number = models.CharField(
phone_number = PhoneNumberField(
verbose_name=_('phone number'),
max_length=50,
blank=True,
......
......@@ -69,7 +69,9 @@ class UserUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
form.fields['email'].required = True
form.fields['email'].help_text = _("This address must be valid.")
context['profile_form'] = self.profile_form(instance=context['user_object'].profile)
context['profile_form'] = self.profile_form(instance=context['user_object'].profile,
data=self.request.POST if self.request.POST else None)
return context
def form_valid(self, form):
......@@ -86,30 +88,33 @@ class UserUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
data=self.request.POST,
instance=self.object.profile,
)
if form.is_valid() and profile_form.is_valid():
new_username = form.data['username']
alias = Alias.objects.filter(name=new_username)
# Si le nouveau pseudo n'est pas un de nos alias,
# on supprime éventuellement un alias similaire pour le remplacer
if not alias.exists():
similar = Alias.objects.filter(
normalized_name=Alias.normalize(new_username))
if similar.exists():
similar.delete()
olduser = User.objects.get(pk=form.instance.pk)
user = form.save(commit=False)
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
user.save()
if olduser.email != user.email:
# If the user changed her/his email, then it is unvalidated and a confirmation link is sent.
user.profile.email_confirmed = False
user.profile.save()
user.profile.send_email_validation_link()
profile_form.full_clean()
if not profile_form.is_valid():
return super().form_invalid(form)
new_username = form.data['username']
alias = Alias.objects.filter(name=new_username)
# Si le nouveau pseudo n'est pas un de nos alias,
# on supprime éventuellement un alias similaire pour le remplacer
if not alias.exists():
similar = Alias.objects.filter(
normalized_name=Alias.normalize(new_username))
if similar.exists():
similar.delete()
olduser = User.objects.get(pk=form.instance.pk)
user = form.save(commit=False)
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
user.save()
if olduser.email != user.email:
# If the user changed her/his email, then it is unvalidated and a confirmation link is sent.
user.profile.email_confirmed = False
user.profile.save()
user.profile.send_email_validation_link()
return super().form_valid(form)
......
......@@ -8,6 +8,8 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField
from member.models import Club, Membership
from note.models import MembershipTransaction
from permission.models import Role
......@@ -223,7 +225,7 @@ class WEIRegistration(models.Model):
verbose_name=_("emergency contact name"),
)
emergency_contact_phone = models.CharField(
emergency_contact_phone = PhoneNumberField(
max_length=32,
verbose_name=_("emergency contact phone"),
)
......
This diff is collapsed.
This diff is collapsed.
......@@ -37,6 +37,7 @@ INSTALLED_APPS = [
# External apps
'mailer',
'phonenumber_field',
'polymorphic',
'crispy_forms',
'django_tables2',
......@@ -195,3 +196,7 @@ MEDIA_URL = '/media/'
# Profile Picture Settings
PIC_WIDTH = 200
PIC_RATIO = 1
PHONENUMBER_DB_FORMAT = 'NATIONAL'
PHONENUMBER_DEFAULT_REGION = 'FR'
......@@ -7,11 +7,13 @@ django-crispy-forms==1.7.2
django-extensions==2.1.9
django-filter==2.2.0
django-mailer==2.0.1
django-phonenumber-field==4.0.0
django-polymorphic==2.0.3
django-tables2==2.1.0
docutils==0.14
idna==2.8
oauthlib==3.1.0
phonenumbers==8.12.7
Pillow==7.1.2
python3-openid==3.1.0
pytz==2019.1
......
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