def dto_to_orm(user_dto): # type: (UserDTO) -> User # Look if there is a user in the DB to take over the unchanged fields user = User.get_or_none(username=user_dto.username) # if the user is non existing, create a new user with the mandatory fields that can be further filled with the user_dto fields if user is None: mandatory_fields = {'username', 'password'} if not mandatory_fields.issubset(set(user_dto.loaded_fields)): raise ValueError('Cannot create user without mandatory fields `{0}`'.format('`, `'.join(mandatory_fields))) user = User(username=user_dto.username.lower(), password=user_dto.hashed_password) for field in ['accepted_terms']: if field in user_dto.loaded_fields: setattr(user, field, getattr(user_dto, field)) return user
def _migrate(cls): # type: () -> None old_sqlite_db = constants.get_config_database_file() if os.path.exists(old_sqlite_db): import sqlite3 connection = sqlite3.connect(old_sqlite_db, detect_types=sqlite3.PARSE_DECLTYPES, check_same_thread=False, isolation_level=None) cursor = connection.cursor() for row in cursor.execute( 'SELECT id, username, password, accepted_terms FROM users;' ): username = row[1] user = User.get_or_none(username=username) if user is None: user = User(username=username, password=row[2], accepted_terms=row[3]) user.save() cursor.execute('DROP TABLE users;')