Example #1
0
    def post(self):
        request_data = request.get_json()
        try:
            name = request_data['name']
            text = request_data['text']
            if not isinstance(auth.current_user(), User):
                raise Exception
            owner_id = auth.current_user().id
        except KeyError:
            return Message.value_error()
        except Exception:
            return Message.creation_error()

        if note_schema.validate(data=request_data, session=session):
            return Message.creation_error()

        note = Note(
            name=name,
            text=text,
            owner_id=owner_id)

        if note is None:
            return Message.creation_error()

        session.add(note)
        session.commit()

        return Message.successful('created', 201)
Example #2
0
    def test_create_user_value_error(self):

        response = self.client.post("/api/user/create/",
                                    data=json.dumps(
                                        dict(username="******",
                                             password="******")),
                                    content_type='application/json')

        self.assertEqual((response.get_json(), 400), Message.value_error())
Example #3
0
    def test_create_tag_value_error(self):

        response = self.client.post(
            "/api/tag/",
            data=json.dumps(dict(name=None, )),
            content_type='application/json',
            headers={
                'Authorization':
                'Basic ' +
                base64.b64encode(b'[email protected]:user1test').decode('utf-8')
            })

        self.assertEqual((response.get_json(), 400), Message.value_error())
Example #4
0
    def post(self):
        request_data = request.get_json()
        try:
            name = request_data['name']
        except KeyError:
            return Message.value_error()

        if tag_schema.validate(data=request_data, session=session):
            return Message.value_error()
        tag = Tag(name=name)

        if tag is None:
            return Message.creation_error()

        session.add(tag)

        try:
            session.commit()
        except IntegrityError:
            session.rollback()
            return Message.creation_error()

        return Message.successful('created', 201)
Example #5
0
    def post(self):
        request_data = request.get_json()
        try:
            username = request_data['username']
            email = request_data['email']
            password = request_data['password']
        except KeyError:
            return Message.value_error()

        user = User(username=username, email=email, password=password)

        if user is None:
            return Message.creation_error()

        session.add(user)
        try:
            session.commit()
        except IntegrityError:
            session.rollback()
            return Message.creation_error()

        return Message.successful('created', 201)