From c36d184ef92231b42c03ef0c78ca0cbe49ad626e Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 18 Apr 2020 04:11:12 +0200 Subject: [PATCH] Email is now unique --- users/migrations/0090_auto_20200418_0206.py | 35 +++++++++++++++++++++ users/models.py | 1 + 2 files changed, 36 insertions(+) create mode 100644 users/migrations/0090_auto_20200418_0206.py diff --git a/users/migrations/0090_auto_20200418_0206.py b/users/migrations/0090_auto_20200418_0206.py new file mode 100644 index 00000000..f5153b30 --- /dev/null +++ b/users/migrations/0090_auto_20200418_0206.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.28 on 2020-04-18 00:06 +from __future__ import unicode_literals + +from django.db import migrations, models +from django.db.models import Count + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0089_auto_20200418_0112'), + ] + + + def remove_double_email_accounts(apps, schema_editor): + db_alias = schema_editor.connection.alias + users = apps.get_model("users", "User") + duplicates = users.objects.using(db_alias).values('email').annotate(email_count=Count('email')).filter(email_count__gt=1) + for email_item in duplicates: + duplicate_users = users.objects.using(db_alias).filter(email=email_item['email']).order_by('last_login', 'registered') + for user in duplicate_users[1:]: + user.email=None + user.save() + + def revert_remove_double_email_accounts(apps, schema_editor): + return + + operations = [ + migrations.RunPython(remove_double_email_accounts, revert_remove_double_email_accounts), + migrations.AlterField( + model_name='user', + name='email', + field=models.EmailField(blank=True, help_text='External email address allowing us to contact you.', max_length=254, null=True, unique=True), + ), + ] diff --git a/users/models.py b/users/models.py index 621617c3..1959219c 100755 --- a/users/models.py +++ b/users/models.py @@ -207,6 +207,7 @@ class User( email = models.EmailField( blank=True, null=True, + unique=True, help_text=_("External email address allowing us to contact you."), ) local_email_redirect = models.BooleanField(