def post(self): """POST method for categories """ err = CategorySchema().validate(request.json) if err: return err cat = Category(**request.json).save() return CategorySchema().dump(cat)
def put(self, cat_id): """ PUT method for updating :param cat_id: category id that we want to update """ if not cat_id: return {'msg': 'cat_id not defined for update'} cat = Category.objects.get(id=cat_id) cat.modify(**request.json) return CategorySchema().dump(cat)
def get(self, cat_id=None): """GET method for categories :param cat_id ID of category. If absent - returns all the existing categories """ many = not cat_id try: query = Category.objects.get( id=cat_id) if cat_id else Category.objects() except DoesNotExist: return {'msg': f'Category with id {cat_id} not exists'} return CategorySchema().dump(query, many=many)