Exemple #1
0
        def routine(user: User, thread: Thread):
            """
            Creates new Post table object and returns it as JSON representation.
            Image posting is totally optional (but if chosen, it needs both 'image' and 'image_name' fields.

            :param user:   User object
            :param thread: Thread object
            :return: New Post Object as JSON object
            """
            try:
                self.check_args()
                self.validate_args()

                anon  = str_to_bool(self.args['anon'])
                image = self.media_processing() if self.args['image'] is not None and \
                                                   self.args['image_name'] is not None else None

                post = Post((user.id == thread.author), anon, self.args['text'], thread.id, user.id, thread.board,
                            self.args['reply'], image)

                # Add new Post table to database
                uchan.add_to_db(post)

                # Add new ThreadUser link
                if thread.get_authid(user.id) is None:
                    uchan.add_to_db(ThreadUser(thread.id, user.id))

                # Increments thread counter
                thread.incr_replies((image is not None))
                uchan.commit()

                return responses.successful(201, JSONRepresentation.post(thread.get_last_post(), thread, user))
            except ValueError as msg:
                return responses.client_error(400, '{}'.format(msg))
Exemple #2
0
        def routine(user: User, board: Board):
            """
            Create new Thread linked to specified board, from POST JSON data.

            :param user:  Requesting User object
            :param board: Board object
            :return: New thread inside specified board
            """
            try:
                # Check thread JSON arguments
                self.check_args()
                self.validate_args()

                # Process anon, image and construct new entity
                anon   = str_to_bool(self.args['anon'])
                image  = self.media_processing()
                thread = Thread(anon, self.args['title'], self.args['text'], image, board.id, user.id)

                # Add new Thread table to database
                uchan.add_to_db(thread)

                # Add new ThreadUser link
                thread = user.get_last_thread()
                uchan.add_to_db(ThreadUser(thread.id, user.id))

                return responses.successful(201, JSONRepresentation.thread(thread, user))
            except ValueError as msg:
                return responses.client_error(400, '{}'.format(msg))
            except KeyError as key:
                return responses.client_error(400, 'Invalid parameter: {}'.format(key))
Exemple #3
0
        def accepting_routine(user: User, chatrequest: ChatRequest):
            # Define new Chat entity
            chat = Chat(chatrequest.u_from, chatrequest.u_to)
            chatrequest.accept()
            uchan.add_to_db(chat)

            # Return new chat
            return responses.successful(201, 'Chat request accepted')
Exemple #4
0
    def add_to_general_boards(user: User):
        """
        Adds specified user to general boards (university.id = 1).

        :param user: User activating object
        :return: Nothing
        """
        for board in Board.query.filter_by(university=1).all():
            uchan.add_to_db(UserBoard(user.id, board.id), False)
Exemple #5
0
    def add_to_university_board(user: User):
        """
        Adds specified user to its university board.

        :param user: User activating object
        :return: Nothing
        """
        board = Board.query.filter_by(university=user.university).first()
        if board is not None:
            uchan.add_to_db(UserBoard(user.id, board.id), False)
Exemple #6
0
        def request_routine(user: User):
            threaduser = ChatRequestAPI.get_threaduser(id)

            if threaduser is None:
                return responses.client_error(404, 'User map object does not exists')

            if user.has_requested_chat(threaduser.user):
                return responses.client_error(409, 'Chat already requested')

            request = ChatRequest(user.id, threaduser.user)
            uchan.add_to_db(request)

            return responses.successful(201, 'Chat request sent')
Exemple #7
0
    def post(self):
        """
        POST method implementation for API Registration resource entity.

        :return: JSON response to Registration method (201 Created, 409 Conflict, 400 Bad Request)
        """
        try:
            # Arguments validation
            self.check_args()
            self.validate_args()

            # Integrity checks
            if not self.check_existing_email():
                return responses.client_error(409, "Email already registered")

            if not self.check_existing_nickname():
                return responses.client_error(409, "Nickname already registered")

            salt, token = self.generate_salt(), self.generate_token()

            # Creating new User entity
            user = User(
                self.args["nickname"],
                routines.hashing_password(salt, self.args["password"]),
                salt,
                self.args["university"],
                self.args["email"],
                self.args["gender"],
                False,
                token,
            )

            # TODO: remove this and insert email sending
            print(user.token)

            # Add new entity to database
            uchan.add_to_db(user)
            return responses.successful(201, "Registration sent")
        except ValueError as msg:
            # Arguments validation error
            return responses.client_error(400, "{}".format(msg))
        except IntegrityError as msg:
            # Database integrity error
            return responses.client_error(
                409, "Registration error, check your JSON or contact server manteiner".format(msg)
            )
Exemple #8
0
    def post(self):
        """
        POST method implementation for API Session resource entity.

        :return: JSON response (201 Created, 400 Bad Request, 409 Conflict [Database IntegrityError])
        """
        try:
            self.check_args()
            self.validate_args()

            session, user = self.register_session(request)

            uchan.add_to_db(session)
            return responses.successful(201, {'token': session.token, 'user': JSONRepresentation.me(user)})
        except ValueError as msg:
            # Arguments validation error
            return responses.client_error(400, '{}'.format(msg))
        except IntegrityError as msg:
            # Database integrity error
            return responses.client_error(409, 'Session error, check your JSON or contact server manteiner'
                                          .format(msg))
Exemple #9
0
#!flask/bin/python
from sys import argv
from server import uchan
from server.models import User, Board, UserBoard

help = """
    Usage: update_boards.py [memo board] [name board]
"""

if __name__ == '__main__' and len(argv) == 3:
    memo = argv[1]
    name = argv[2]

    uchan.add_to_db(Board(memo, name, 1))
    board = Board.query.order_by(Board.id.desc()).first()

    for user in User.query.all():
        uchan.add_to_db(UserBoard(user.id, board.id), False)

    uchan.commit()

else:
    print(help)
Exemple #10
0
#!flask/bin/python
from server import uchan
from server.models import Board
from flask import json

with open('boards.json', 'r') as general_boards:
    boards_list = json.load(general_boards)

for board in boards_list:
    memo = board['memo']
    name  = board['name']

    x = Board(memo, name, 1)
    uchan.add_to_db(x, False)

uchan.commit()