def update(new_post, categories):
    """Update post.

    Arguments:
    new_post -- Posts object
    categories - list of categories
    """

    session = DBSession()

    # get data
    post = session.query(Posts).filter(Posts.id == new_post.id,
                                       Posts.is_deleted == False).one()

    # update data
    if new_post.header is not None:
        post.header = new_post.header

    if new_post.content is not None:
        post.content = new_post.content

    if categories:
        post.categories.clear()
        for c in categories:
            category = session.query(Categories).get(c)
            post.categories.append(category)

    post.modification_date = datetime.now().replace(
        microsecond=0).isoformat(' ')

    session.commit()
    session.close()
Beispiel #2
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, msg_id: int, token: dict, *args,
                           **kwargs) -> BaseHTTPResponse:

        try:
            db_msg = session.get_msg_by_id(msg_id)
        except DBMsgNotExistsException:
            raise SanicMsgNotFoundException

        if token.get('uid') is not db_msg.sender_id:
            return await self.make_response_json(status=403)

        req_model = ReqUpdateMsgDTO(body)

        try:
            db_msg = msg_queries.update_msg(session, db_msg.id, req_model)
        except DBMsgNotExistsException:
            raise SanicMsgNotFoundException

        try:
            session.commit()
        except (DBIntegrityException, DBDataException):
            raise SanicDBException

        res_model = ResGetMsgDTO(db_msg)

        return await self.make_response_json(body=res_model.dump(), status=200)
Beispiel #3
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, uid: int, token: str, *args,
                            **kwargs) -> BaseHTTPResponse:
        if token.get('uid') != uid:
            return await self.make_response_json(status=403)

        try:
            user_queries.delete_user(session, uid)
        except DBUserNotExistsException:
            raise SanicUserNotFoundException("User not found")

        msgs = msg_queries.get_msgs(session, uid)

        for msg in msgs:
            try:
                user = user_queries.get_user(session, user_id=msg.recipient_id)
            except DBUserNotExistsException:
                user = None
            if uid is msg.sender_id and user is None:
                msg.is_delete = True
            try:
                user = user_queries.get_user(session, user_id=msg.sender_id)
            except DBUserNotExistsException:
                user = None
            if uid is msg.recipient_id and user is None:
                msg.is_delete = True

        try:
            session.commit()
        except (DBIntegrityException, DBDataException):
            raise SanicDBException

        return await self.make_response_json(body={}, status=200)
def add(category):
    """Add new category"""
    session = DBSession()

    session.add(category)

    session.commit()
    session.close()
Beispiel #5
0
def add(user):
    """Add new user"""
    session = DBSession()

    session.add(user)

    session.commit()
    session.close()
def delete(id):
    """Delete post by id"""
    session = DBSession()

    post = session.query(Posts).filter(Posts.id == id,
                                       Posts.is_deleted == False).one()

    # actually just set is_deleted flag to True
    post.is_deleted = True
    post.deletion_date = datetime.now().replace(microsecond=0).isoformat(' ')

    session.commit()
    session.close()
def restore(post_id):
    """Restore deleted post by id"""
    session = DBSession()

    post = session.query(Posts).filter(Posts.id == post_id,
                                       Posts.is_deleted == True).one()

    post.modification_date = datetime.now().replace(
        microsecond=0).isoformat(' ')
    post.deletion_date = None
    post.is_deleted = False

    session.commit()
    session.close()
Beispiel #8
0
    async def method_post(self, request: Request, body: dict,
                          session: DBSession, *args,
                          **kwargs) -> BaseHTTPResponse:
        req_model = ReqCreateUserDTO(body)

        hash_pass = generate_hash(req_model.password)
        try:
            db_user = user_query.create_user(session, req_model, hash_pass)
        except DBUserExistsException:
            raise SanicUserConflictException("User already exist")
        try:
            session.commit()
        except (DBIntegrityException, DBDataException):
            raise SanicDBException

        response_model = ResGetUserDTO(db_user)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
Beispiel #9
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, msg_id: int, token: dict,
                            *args, **kwargs):
        try:
            db_msg = msg_queries.get_msg(session, msg_id)
        except DBMsgNotExistsException:
            raise SanicMsgNotFoundException("Message not found")

        if token.get('uid') is not db_msg.sender_id:
            return await self.make_response_json(status=403)

        msg_queries.delete_msg(session, msg_id)

        try:
            session.commit()
        except (DBIntegrityException, DBDataException):
            raise SanicDBException

        return await self.make_response_json(body={}, status=200)
Beispiel #10
0
    async def method_post(self, request: Request, body: dict,
                          session: DBSession, token: dict, *args,
                          **kwargs) -> BaseHTTPResponse:

        req_model = ReqCreateMsgDTO(body)

        try:
            db_msg = message_queries.create_msg(session, req_model,
                                                token.get('uid'))
        except DBUserNotExistsException:
            raise SanicUserNotFoundException("User not found")

        try:
            session.commit()
        except (DBIntegrityException, DBDataException):
            raise SanicDBException

        res_model = ResGetMsgDTO(db_msg)

        return await self.make_response_json(body=res_model.dump(), status=201)
def add(post, categories):
    """Add new post and return it's id.

    Arguments:
    post - Posts object
    categories - list of categories id
    """

    session = DBSession()

    session.add(post)
    # add categories to the post
    for id in categories:
        category = session.query(Categories).get(id)
        post.categories.append(category)

    id = post.id

    session.commit()
    session.close()
    return id
Beispiel #12
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, uid: int, token: dict, *args,
                           **kwargs) -> BaseHTTPResponse:
        if token.get('uid') != uid:
            return await self.make_response_json(status=403)

        req_model = ReqUpdateUserDTO(body)

        try:
            user = user_queries.update_user(session, uid, req_model)
        except DBUserNotExistsException:
            raise SanicUserNotFoundException("User not found")

        try:
            session.commit()
        except (DBIntegrityException, DBDataException):
            raise SanicDBException

        res_model = ResGetUserDTO(user)

        return await self.make_response_json(body=res_model.dump(), status=200)