Example #1
0
def save_user(user_data: user_pb2.User) -> user_pb2.User:
    """Save a user in the database."""

    unused_, users_database, unused_ = mongo.get_connections_from_env()
    users_database = users_database.with_prefix('jobflix_')
    collection = users_database.user

    if user_data.profile.email:
        if db_user := collection.find_one(
                {'hashedEmail': (hashed_email := auth.hash_user_email(user_data.profile.email))},
                {'_id': 1, 'projects': 1}):
            user_data.user_id = str(db_user['_id'])
            new_projects = list(user_data.projects[:])
            user_data.ClearField('projects')
            user_data.projects.extend(
                proto.create_from_mongo(p, project_pb2.Project, always_create=True)
                for p in db_user.get('projects', []))
            old_project_ids = {p.project_id for p in user_data.projects}
            user_data.projects.extend(
                p for p in new_projects if _make_project_id(p) not in old_project_ids)
        elif user_data.user_id:
            collection.update_one({'_id': objectid.ObjectId(user_data.user_id)}, {'$set': {
                'profile.email': user_data.profile.email,
                'hashedEmail': hashed_email,
            }})
Example #2
0
def _save_low_level(user_data: user_pb2.User,
                    is_new_user: bool = False) -> user_pb2.User:
    user_collection = flask.current_app.config['USER_DATABASE'].user
    user_dict = json_format.MessageToDict(user_data)
    user_dict.update(SERVER_TAG)
    user_dict.pop('userId', None)
    if is_new_user:
        result = user_collection.insert_one(user_dict)
        user_data.user_id = str(result.inserted_id)
    else:
        user_collection.replace_one({'_id': safe_object_id(user_data.user_id)},
                                    user_dict)
    return user_data
Example #3
0
def save_low_level(
        user_data: user_pb2.User,
        *,
        is_new_user: bool = False,
        collection: Optional[pymongo.Collection] = None) -> user_pb2.User:
    """Save the user almost 'as is' in database."""

    user_collection = collection or _get_user_db().user
    user_dict = json_format.MessageToDict(user_data) | SERVER_TAG
    user_dict.pop('userId', None)
    if is_new_user:
        result = user_collection.insert_one(user_dict)
        user_data.user_id = str(result.inserted_id)
    else:
        user_collection.replace_one({'_id': safe_object_id(user_data.user_id)},
                                    user_dict)
    return user_data