Пример #1
0
 def post(self):
     try:
         body = request.get_json()
         category = Category(**body)
         category.save()
         return {'id': str(category.id)}, 200
     except (ValidationError, FieldDoesNotExist) as e:
         raise e
     except NotUniqueError:
         raise CategoryAlreadyExistsError
     except Exception as e:
         raise InternalServerError
Пример #2
0
    def put(self, id):
        try:
            body = request.get_json()
            if len(body) == 0:
                raise ValidationError

            if 'create_datetime' in body or 'modify_datetime' in body:
                raise CreateOrModifyDateNotCanBeSet
            body['modify_datetime'] = datetime.utcnow()

            if 'categories' in body:
                try:
                    body['categories'] = [Category.objects(name=name).get() for name in set(body['categories'])]
                except DoesNotExist:
                    raise CategoryNotExistsError

            Article.objects.get(id=id).update(**body)
            return '', 200
        except (CategoryNotExistsError, ValidationError) as e:
            raise e
        except InvalidQueryError:
            raise FieldDoesNotExist
        except DoesNotExist:
            raise ArticleNotExistsError
        except Exception:
            raise InternalServerError
Пример #3
0
    def post(self):
        try:
            user_id = get_jwt_identity()
            user = User.objects.get(id=user_id)
            body = request.get_json()

            if 'create_datetime' in body or 'modify_datetime' in body:
                raise CreateOrModifyDateNotCanBeSet
            body['create_datetime'] = None
            body['modify_datetime'] = None

            if 'categories' not in body:
                raise ValidationError
            try:
                body['categories'] = [Category.objects(name=name).get() for name in set(body['categories'])]
            except DoesNotExist:
                raise CategoryNotExistsError

            article = Article(**body, added_by=user)
            article.save()
            return {'id': str(article.id)}, 200
        except (ValidationError, FieldDoesNotExist, CreateOrModifyDateNotCanBeSet, CategoryNotExistsError) as e:
            raise e
        except NotUniqueError:
            raise ArticleAlreadyExistsError
        except Exception as e:
            raise InternalServerError
Пример #4
0
    def test_successful_delete_category(self):
        login_token = self.getLoginToken()

        category_id = self.createCategory(name="category1",
                                          login_token=login_token)

        response = self.app.delete(
            '/api/category/{0}'.format(category_id),
            headers={"Authorization": f"Bearer {login_token}"})

        self.assertEqual(200, response.status_code)
        self.assertEqual(Category.objects(id=category_id).count(), 0)
Пример #5
0
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('category', type=str)
        args = parser.parse_args()

        if args['category'] is not None:
            try:
                category_id = Category.objects(name=args['category']).get()
            except DoesNotExist:
                raise CategoryNotExistsError
            articles = Article.objects(categories=category_id).to_json()
        else:
            articles = Article.objects().to_json()
        return Response(articles, mimetype="application/json", status=200)
Пример #6
0
    def test_successful_post_category(self):
        login_token = self.getLoginToken()

        category_payload = {"name": "category1"}

        response = self.app.post('/api/categories',
                                 headers={
                                     "Content-Type": "application/json",
                                     "Authorization": f"Bearer {login_token}"
                                 },
                                 data=json.dumps(category_payload))
        category_id = response.json['id']

        self.assertEqual(200, response.status_code)

        category = Category.objects(id=category_id).get()
        self.assertEqual(category.name, category_payload['name'])
Пример #7
0
    def test_successful_put_category(self):
        login_token = self.getLoginToken()

        category_id = self.createCategory(name="category1",
                                          login_token=login_token)

        category_edit_payload = {"name": "new_category1"}

        response = self.app.put('/api/category/{0}'.format(category_id),
                                headers={
                                    "Content-Type": "application/json",
                                    "Authorization": f"Bearer {login_token}"
                                },
                                data=json.dumps(category_edit_payload))

        self.assertEqual(200, response.status_code)

        category = Category.objects(id=category_id).get()
        self.assertEqual(category.name, category_edit_payload['name'])
Пример #8
0
 def get(self):
     categories = Category.objects().to_json()
     return Response(categories, mimetype="application/json", status=200)