Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Create Name model for usernames #265

Merged
merged 6 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions features/steps/failed_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ def when_form_submit(context):
@then('the new user will not be registered')
def then_user_registered(context):
# just one user exists, the first one
user_query = User.query.filter_by(username=context.test_user['username'])
assert user_query.count() == 1
user = User.get(username=context.test_user['username'])
assert user is not None
17 changes: 11 additions & 6 deletions lastuser_core/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from coaster.sqlalchemy import TimestampMixin, BaseMixin, BaseScopedNameMixin, UuidMixin # Imported from here by other models # NOQA
# Imported from here by other models
from coaster.sqlalchemy import TimestampMixin, BaseMixin, BaseScopedNameMixin, UuidMixin # NOQA
from coaster.db import db


Expand Down Expand Up @@ -47,7 +48,13 @@ def merge_users(user1, user2):
else:
keep_user, merge_user = user2, user1

# 1. Inspect all tables for foreign key references to merge_user and switch to keep_user.
# 1. Release the username
if not keep_user.username:
if merge_user.username:
keep_user.username = merge_user.username
merge_user.username = None

# 2. Inspect all tables for foreign key references to merge_user and switch to keep_user.
for model in db.Model.__subclasses__():
if model != User:
# a. This is a model and it's not the User model. Does it have a migrate_user classmethod?
Expand All @@ -57,12 +64,10 @@ def merge_users(user1, user2):
elif hasattr(model, 'user_id') and hasattr(model, 'query'):
for row in model.query.filter_by(user_id=merge_user.id).all():
row.user_id = keep_user.id
# 2. Add merge_user's uuid to olduserids. Commit session.
# 3. Add merge_user's uuid to olduserids. Commit session.
db.session.add(UserOldId(id=merge_user.uuid, user=keep_user))
# 3. Mark merge_user as merged. Commit session.
# 4. Mark merge_user as merged. Commit session.
merge_user.status = USER_STATUS.MERGED
# 4. Release the username
merge_user.username = None
# 5. Commit all of this
db.session.commit()

Expand Down
Loading