Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions pythonpro/core/migrations/0011_normalizing_lead_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 3.0.3 on 2020-02-28 11:30
from itertools import count

from django.db import IntegrityError, migrations
from django.db.transaction import atomic


def normalize_email(apps, schema_editor):
'''
We can't import the Post model directly as it may be a newer
version than this migration expects. We use the historical version.
'''
User = apps.get_model('core', 'User')
print()
for msg in normalize_all_user_leads_emails(User):
print(msg)


def normalize_all_user_leads_emails(User):
"""
Migrate all lead email to lower case with prefix excluded
:param User:
:return:
"""
no_migrate_groups = {'member', 'client'}
for user in User.objects.order_by('id').all():
original_email = user.email
normalized_email = user.email.lower()
if normalized_email != original_email:
user_groups = {g.name for g in user.groups.all()}
if no_migrate_groups.isdisjoint(user_groups):
for exclusion_index in count(1):
user.email = f'excluded{exclusion_index}.{normalized_email}'
with atomic():
try:
user.save()
except IntegrityError:
pass
else:
break
else:
user_groups_str = ','.join(user_groups)
yield f'User groups {user_groups_str} user {user.id}, {user.email}'


class Migration(migrations.Migration):
dependencies = [
('core', '0010_normalizing_email'),
]

operations = [
migrations.RunPython(normalize_email, migrations.RunPython.noop)
]
2 changes: 1 addition & 1 deletion pythonpro/core/tests/test_email_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_no_msg_for_already_normalized_emails(django_user_model):
assert msg is None


def test_conflictiong_email_msg(django_user_model):
def test_conflicting_email_msg(django_user_model):
lower_email = 'normalized@python.pro.br'
upper_email = 'NORMALIZED@python.pro.br'
upper_email_user = mommy.make(django_user_model, email=upper_email)
Expand Down
39 changes: 39 additions & 0 deletions pythonpro/core/tests/test_lead_email_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from importlib import import_module

import pytest
from django.contrib.auth.models import Group
from model_mommy import mommy

# Workaround since module beginning with number can't be imported in regular way
migration_module = import_module('pythonpro.core.migrations.0011_normalizing_lead_email')


def test_no_msg_for_already_normalized_emails(django_user_model):
mommy.make(django_user_model, email='lower@python.pro.br')
msg = next(migration_module.normalize_all_user_leads_emails(django_user_model), None)
assert msg is None


@pytest.mark.parametrize('role', 'client member'.split())
def test_conflicting_email_msg_for_non_leads(django_user_model, role):
lower_email = 'normalized@python.pro.br'
upper_email = 'NORMALIZED@python.pro.br'
group = mommy.make(Group, name=role)
upper_email_user = mommy.make(django_user_model, email=upper_email, groups=[group])
mommy.make(django_user_model, email=lower_email)
msg = next(migration_module.normalize_all_user_leads_emails(django_user_model))
upper_email_user = django_user_model.objects.get(id=upper_email_user.id)
assert msg == f'User groups {role} user {upper_email_user.id}, {upper_email_user.email}'


def test_conflicting_email_excluded(django_user_model):
lower_email = 'normalized@python.pro.br'
group = mommy.make(Group, name='lead')
mommy.make(django_user_model, email='NORMALIZED@python.pro.br', groups=[group])
mommy.make(django_user_model, email='NORMALIZEd@python.pro.br', groups=[group])
mommy.make(django_user_model, email=lower_email)
msg = next(migration_module.normalize_all_user_leads_emails(django_user_model), None)
assert msg is None
first_user, second_user, *_ = list(django_user_model.objects.order_by('id').all())
assert first_user.email == f'excluded1.{lower_email}'
assert second_user.email == f'excluded2.{lower_email}'