Beispiel #1
0
 def get_data(self):
     data = json.loads(self.request.body.decode('utf-8'))
     up_down = check_param(data,
                           name='up_down',
                           type_param='boolean',
                           required=True)
     return up_down
Beispiel #2
0
    async def post(self):

        # Create data
        data = json.loads(self.request.body.decode('utf-8'))
        title = check_param(data,
                            name='title',
                            type_param='string',
                            required=True)
        body = check_param(data,
                           name='body',
                           type_param='string',
                           required=True)
        tags = check_param(data, name='tags', type_param='list', required=True)
        if len(tags) < 1:
            raise BadRequestError('At least one tag is required')

        # Add tag
        tags_to_add = []
        for tag_str in tags:
            tag = self.application.db.query(Tag).filter_by(
                label=tag_str).first()
            if tag is None:
                tag = Tag(label=tag_str)
                self.application.db.add(tag)
            tags_to_add.append(tag)

        question = Question(title=title,
                            body=body,
                            user=self.user,
                            tags=tags_to_add)
        self.application.db.add(question)

        # Commit in DB
        try:
            self.application.db.commit()
        except SQLAlchemyError as error:
            self.application.db.rollback()
            raise InternalServerError('Unable to create the question.', error)

        # Returns response
        self.set_status(201)
        self.write({'data': question.to_dict()})
        self.finish()
Beispiel #3
0
    async def post(self):
        # Create data
        data = json.loads(self.request.body.decode('utf-8'))
        firstname = check_param(data,
                                name='firstname',
                                type_param='string',
                                required=True)
        lastname = check_param(data,
                               name='lastname',
                               type_param='string',
                               required=True)
        email = check_param(data,
                            name='email',
                            type_param='string',
                            required=True)
        password = check_param(data,
                               name='password',
                               type_param='string',
                               required=True)
        hashed_password = hashlib.md5(password.encode('utf-8')).hexdigest()

        user = User(firstname=firstname,
                    lastname=lastname,
                    email=email,
                    password=hashed_password)
        self.application.db.add(user)

        # Commit in DB
        try:
            self.application.db.commit()
        except SQLAlchemyError as error:
            self.application.db.rollback()
            raise InternalServerError('Unable to create the user.', error)

        # Returns response
        self.set_status(201)
        self.write({'data': user.to_dict()})
        self.finish()
Beispiel #4
0
    async def post(self, question_id):

        # Create data
        data = json.loads(self.request.body.decode('utf-8'))
        body = check_param(data,
                           name='body',
                           type_param='string',
                           required=True)
        answer = Answer(body=body, question_id=question_id, user=self.user)

        # Commit in DB
        try:
            self.application.db.add(answer)
            self.application.db.commit()
        except SQLAlchemyError as error:
            self.application.db.rollback()
            raise InternalServerError('Unable to create the answer.', error)

        # Returns response
        self.set_status(201)
        self.write({'data': answer.to_dict()})
        self.finish()
Beispiel #5
0
    async def put(self, question_id):

        # Update basic properties
        data = json.loads(self.request.body.decode('utf-8'))
        update_by_property_list(['title', 'body'], data, self.object)

        # Update tags
        if 'tags' in data:
            tags = check_param(data,
                               name='tags',
                               type_param='list',
                               required=True)
            if len(tags) < 1:
                raise BadRequestError('At least one tag is required')

            # Add tag
            tags_to_add = []
            for tag_str in tags:
                tag = self.application.db.query(Tag).filter_by(
                    label=tag_str).first()
                if tag is None:
                    tag = Tag(label=tag_str)
                    self.application.db.add(tag)
                tags_to_add.append(tag)

            # Update property in question
            self.object.tags = tags_to_add

        # Commit in DB
        try:
            self.application.db.commit()
        except SQLAlchemyError as error:
            self.application.db.rollback()
            raise InternalServerError('Unable to update the question.', error)

        # Returns response
        self.set_status(200)
        self.write({'data': self.object.to_dict()})
        self.finish()