def populate_sameuser(apps, schema_editor): User = apps.get_model('auth', 'User') SameUser = apps.get_model('accounts', 'SameUser') # Get all case-insensitive duplicate email addresses which are used in more than one account email_addresses = User.objects.values_list(Lower('email'), flat=True)\ .annotate(num_email=Count('email'))\ .filter(num_email__gt=1) for email in email_addresses: # Get all users with that email address uss = User.objects.filter( email__iexact=email).order_by('-last_login').all() u1 = uss[0] for u2 in uss[1:]: # Assign users to variables and modify email of second user (to prevent the duplicates) u2.email = transform_unique_email(email) u2.save() # Create SameUser object SameUser.objects.create(main_user=u1, main_orig_email=email, secondary_user=u2, secondary_orig_email=email)
def populate_sameuser(apps, schema_editor): User = apps.get_model("auth", "User") SameUser = apps.get_model("accounts", "SameUser") # Get all email addresses which are used in more than one account email_addresses = User.objects.values_list("email", flat=True)\ .annotate(num_email=Count('email'))\ .filter(num_email__gt=1).exclude(email="") for email in email_addresses: # Get all users with that email address uss = User.objects.filter(email=email).order_by('-last_login').all() # Check that there are 2 users (there should be no more than 2 users with the same address) assert uss.count() == 2 # Assign users to variables and modify email of second user (to prevent the duplicates) u1 = uss[0] u2 = uss[1] u2.email = transform_unique_email(email) u2.save() # Create SameUser object SameUser.objects.create(main_user=u1, main_orig_email=email, secondary_user=u2, secondary_orig_email=email)
def setUp(self): self.user_a = User.objects.create_user("user_a", password="******", email='*****@*****.**') self.original_shared_email = '*****@*****.**' self.user_b = User.objects.create_user("user_b", password="******", email=self.original_shared_email) self.user_c = User.objects.create_user("user_c", password="******", email=transform_unique_email(self.original_shared_email)) SameUser.objects.create( main_user=self.user_b, main_orig_email=self.user_b.email, secondary_user=self.user_c, secondary_orig_email=self.user_b.email, # Must be same email (original) )
def setUp(self): self.user_a = User.objects.create_user("user_a", password="******", email='*****@*****.**') self.original_shared_email = '*****@*****.**' self.user_b = User.objects.create_user("user_b", password="******", email=self.original_shared_email) self.user_c = User.objects.create_user("user_c", password="******", email=transform_unique_email(self.original_shared_email)) SameUser.objects.create( main_user=self.user_b, main_orig_email=self.user_b.email, secondary_user=self.user_c, secondary_orig_email=self.user_b.email, # Must be same email (original) )
def populate_sameuser(apps, schema_editor): User = apps.get_model('auth', 'User') SameUser = apps.get_model('accounts', 'SameUser') # Get all case-insensitive duplicate email addresses which are used in more than one account email_addresses = User.objects.values_list(Lower('email'), flat=True)\ .annotate(num_email=Count('email'))\ .filter(num_email__gt=1) for email in email_addresses: # Get all users with that email address uss = User.objects.filter(email__iexact=email).order_by('-last_login').all() u1 = uss[0] for u2 in uss[1:]: # Assign users to variables and modify email of second user (to prevent the duplicates) u2.email = transform_unique_email(email) u2.save() # Create SameUser object SameUser.objects.create(main_user=u1, main_orig_email=email, secondary_user=u2, secondary_orig_email=email)
def secondary_trans_email(self): return transform_unique_email(self.orig_email)
def secondary_trans_email(self): return transform_unique_email(self.orig_email)