예제 #1
0
def filter_user(field, value, first=False):
    with db_session() as sess:
        user_orms = sess.query(UserOrm).filter(getattr(UserOrm, field) == value)
        users = [User.from_orm(u) for u in user_orms]
        if first:
            return users[0] if users else None
        return users
예제 #2
0
def db_insert(users, posts, comments):
    with db_session() as sess:
        user_orms, post_orms, comment_orms = dataset_to_orms(sess=sess,
                                                             users=users,
                                                             posts=posts,
                                                             comments=comments)
        [sess.add(u) for u in user_orms]
        [sess.add(p) for p in post_orms]
        [sess.add(c) for c in comment_orms]
        sess.commit()

        # have to run the second time, as foreign keys (author_id, post_id etc.) will not be set during first commit
        # for some reason sqlalchemy only allow set id when both original orm (say, user) and the connected orm (say, post)
        # are presented in database. There might be a better way to do this...
        user_orms, post_orms, comment_orms = dataset_to_orms(sess=sess,
                                                             users=users,
                                                             posts=posts,
                                                             comments=comments)
        [sess.add(u) for u in user_orms]
        [sess.add(p) for p in post_orms]
        [sess.add(c) for c in comment_orms]
        sess.commit()
예제 #3
0
def delete_post(post_id):
    with db_session() as sess:
        post_orm = sess.query(PostOrm).get(post_id)
        sess.delete(post_orm)
        sess.commit()
예제 #4
0
def create_post(title, body, author):
    with db_session() as sess:
        author_orm = sess.query(UserOrm).get(author.id)
        post_orm = Post(title=title, body=body, author=author).to_orm(author_orm=author_orm)
        sess.add(post_orm)
        sess.commit()
예제 #5
0
def save_post(post:Post):
    with db_session() as sess:
        post_orm = sess.query(PostOrm).get(post.id)
        post_orm = post.to_orm(post_orm=post_orm)
        sess.add(post_orm)
        sess.commit()
예제 #6
0
def get_post(post_id):
    with db_session() as sess:
        _logger.info(f'fetching post with id: {post_id}')
        post_orm = sess.query(PostOrm).get(post_id)
        return load_post_lv1(post_orm)
예제 #7
0
def load_latest_posts(n=10):
    with db_session() as sess:
        post_orms = sess.query(PostOrm).order_by(PostOrm.last_modified.desc()).limit(n)
        return [load_post_lv1(post_orm) for post_orm in post_orms]
예제 #8
0
def create_user(username, nickname, password, email):
    with db_session() as sess:
        user_orm = User(username=username, nickname=nickname,
                password=password, email=email).to_orm()
        sess.add(user_orm)
        sess.commit()
예제 #9
0
def save_user(user: User):
    with db_session() as sess:
        user_orm = sess.query(UserOrm).get(user.id)
        user_orm = user.to_orm(user_orm)
        sess.add(user_orm)
        sess.commit()
예제 #10
0
def get_user(user_id):
    with db_session() as sess:
        _logger.info(f'fetching user with id: {user_id}')
        user_orm = sess.query(UserOrm).get(user_id)
        return load_user_lv1(user_orm)