custom_user : déprécier le champ de base User.phone (#79135) #84

Merged
pmarillonnet merged 2 commits from wip/79135-user-phone-model-field-deprecation into main 2023-07-17 10:17:36 +02:00
6 changed files with 33 additions and 41 deletions

View File

@ -472,7 +472,7 @@ class UserCsvImporter:
header.key = True
elif header.name not in SPECIAL_COLUMNS:
try:
if header.name in ['email', 'phone', 'first_name', 'last_name', 'username']:
if header.name in ['email', 'first_name', 'last_name', 'username']:
User._meta.get_field(header.name)
header.field = True
if header.name == 'email':

View File

@ -0,0 +1,16 @@
# Generated by Django 3.2.18 on 2023-06-28 13:55
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('custom_user', '0035_alter_user_username'),
]
operations = [
migrations.RemoveConstraint(
model_name='user',
name='constraint_at_least_one_identifier',
),
]

View File

@ -31,7 +31,6 @@ from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import MultipleObjectsReturned, ValidationError
from django.core.mail import send_mail
from django.db import models, transaction
from django.db.models import Q
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
@ -118,7 +117,7 @@ class Attributes:
)
update_fields = ['modified']
if name in ['first_name', 'last_name', 'phone']:
if name in ['first_name', 'last_name']:
if getattr(self.owner, name) != value:
setattr(self.owner, name, value)
update_fields.append(name)
@ -268,12 +267,6 @@ class User(AbstractBaseUser):
verbose_name = _('user')
verbose_name_plural = _('users')
ordering = ('last_name', 'first_name', 'email', 'username')
constraints = [
models.CheckConstraint(
check=Q(username__isnull=False) | Q(email__isnull=False) | Q(phone__isnull=False),
name='constraint_at_least_one_identifier',
)
]
def get_group_permissions(self, obj=None):
"""
@ -510,7 +503,7 @@ class User(AbstractBaseUser):
def save(self, *args, **kwargs):
update_fields = kwargs.get('update_fields')
super().save(*args, **kwargs)
if not update_fields or not set(update_fields).isdisjoint({'first_name', 'last_name', 'phone'}):
if not update_fields or not set(update_fields).isdisjoint({'first_name', 'last_name'}):
try:
self.attributes.first_name
except AttributeError:
@ -526,14 +519,6 @@ class User(AbstractBaseUser):
if self.attributes.last_name != self.last_name:
self.attributes.last_name = self.last_name
try:
self.attributes.phone
except AttributeError:
pass
else:
if self.attributes.phone != self.phone:
self.attributes.phone = self.phone
def can_change_password(self):
return True

View File

@ -3221,7 +3221,7 @@ def test_api_basic_authz_user_phone_number(app, settings, superuser, phone_activ
headers = basic_authorization_header(superuser)
app.get('/api/users/', headers=headers, status=200)
superuser.phone = '+33499985643'
superuser.attributes.phone = '+33499985643'
superuser.save()
# authn valid

View File

@ -201,7 +201,7 @@ x,x,x,x'''
CsvHeader(1, 'email', field=True, key=True, verified=True),
CsvHeader(2, 'first_name', field=True),
CsvHeader(3, 'last_name', field=True),
CsvHeader(4, 'phone', field=True),
CsvHeader(4, 'phone', field=False, attribute=True),
]
assert importer.has_errors
assert len(importer.rows) == 3
@ -253,7 +253,7 @@ x,x,x,x'''
CsvHeader(1, 'email', field=True, key=True, verified=True),
CsvHeader(2, 'first_name', field=True),
CsvHeader(3, 'last_name', field=True),
CsvHeader(4, 'phone', field=True),
CsvHeader(4, 'phone', field=False, attribute=True),
]
assert importer.has_errors
assert len(importer.rows) == 3
@ -435,7 +435,7 @@ app1,2,tnoel@entrouvert.com,Thomas,Noël,0606060606
CsvHeader(3, 'email', field=True, verified=True),
CsvHeader(4, 'first_name', field=True),
CsvHeader(5, 'last_name', field=True),
CsvHeader(6, 'phone', field=True),
CsvHeader(6, 'phone', field=False, attribute=True),
]
assert not importer.has_errors
assert len(importer.rows) == 2

View File

@ -89,17 +89,19 @@ def test_sync_first_name(db, settings):
assert user.attributes.first_name == 'John Paul'
def test_sync_phone(db, settings):
def test_sync_phone_deactivated(db, settings):
Attribute.objects.get_or_create(name='phone', defaults={'label': 'Phone', 'kind': 'phone_number'})
user = User(username='john.doe', email='john.doe2@example.net')
user.phone = '+33123456789' # deprecated model field
user.save()
user.phone = '+33123456789'
user.save()
assert user.attributes.phone == '+33123456789'
assert user.attributes.phone is None
user.attributes.phone = '+43876543210'
user.save()
user.refresh_from_db()
assert user.attributes.phone == '+43876543210'
assert user.phone == '+33123456789' # deprecated field left unused & unmodified
def test_save_does_not_reset_verified_attributes_first_name(db):
@ -186,35 +188,24 @@ def test_save_does_not_reset_verified_attributes_phone(db):
user.verified_attributes.phone = '+33123456789'
user = User.objects.get()
assert user.phone == '+33123456789'
assert user.phone is None # deprecated model field
assert user.is_verified.phone
assert user.verified_attributes.phone == '+33123456789'
assert user.attributes.phone == '+33123456789'
user.save()
user = User.objects.get()
assert user.phone == '+33123456789'
assert user.phone is None # deprecated model field
assert user.is_verified.phone
assert user.verified_attributes.phone == '+33123456789'
assert user.attributes.phone == '+33123456789'
user.phone = '+43876543210'
user.phone = '+43876543210' # deprecated field mistakenly modified in db
user.save()
assert not user.is_verified.phone
assert user.verified_attributes.phone is None
assert user.attributes.phone == '+43876543210'
user = User.objects.get()
assert user.phone == '+43876543210'
assert not user.is_verified.phone
assert user.verified_attributes.phone is None
assert user.attributes.phone == '+43876543210'
user.verified_attributes.phone = '+33123456789'
assert user.is_verified.phone
assert user.is_verified.phone # verication state unmodified
assert user.verified_attributes.phone == '+33123456789'
assert user.attributes.phone == '+33123456789'
user = User.objects.get()
assert user.phone == '+33123456789'
assert user.is_verified.phone
assert user.verified_attributes.phone == '+33123456789'
assert user.attributes.phone == '+33123456789'