def on_post(self, req, resp): args = parse(schema=GroupSchema(partial=False, exclude=['id'], unknown=marshmallow.RAISE), request=req) group = Group(**args) req.context.db_session.add(group) req.context.db_session.commit() resp.media = GroupSchema().dump(group) resp.status = falcon.HTTP_200
def on_delete(self, req, resp, id): group = req.context.db_session.query(Group).get(id) req.context.db_session.delete(group) req.context.db_session.commit() resp.media = GroupSchema().dump(group) resp.status = falcon.HTTP_200
def on_get(self, req, resp, id): group = req.context.db_session.query(Group).get(id) if not group: raise EntityNotExists(description='Group with id=%s not exists' % id) resp.media = GroupSchema().dump(group) resp.status = falcon.HTTP_200
def on_patch(self, req, resp, id): args = parse(schema=GroupSchema(partial=True, exclude=['id'], unknown=marshmallow.RAISE), request=req) group = req.context.db_session.query(Group).get(id) if not group: raise EntityNotExists(description='Group with id=%s not exists' % id) group.update(**args) req.context.db_session.add(group) req.context.db_session.commit() resp.media = GroupSchema().dump(group) resp.status = falcon.HTTP_200
def on_post(self, req, resp, group_id, item_id): group = req.context.db_session.query(Group).get(group_id) item = req.context.db_session.query(Item).get(item_id) if not group: raise EntityNotExists(description='Group with id=%s not exists' % group_id) if not item: raise EntityNotExists(description='Item with id=%s not exists' % item_id) if item in group.items: raise EntityExists( description='Group %s already has item id=%s assigned' % (group_id, item_id)) group.items.append(item) req.context.db_session.commit() resp.media = GroupSchema().dump(group) resp.status = falcon.HTTP_200
def on_get(self, req, resp): all_groups = req.context.db_session.query(Group).all() resp.media = GroupSchema(many=True).dump(all_groups) resp.status = falcon.HTTP_200
from flask import request from flask_restful import Resource from flask_jwt_extended import jwt_required from datetime import datetime from models.group import GroupModel from schemas.group import GroupSchema NAME_ALREADY_EXISTS = "A group with name '{}' already exists." ERROR_INSERTING = "An error occurred while {} a new group." GROUP_NOT_FOUND = "Group not found." GROUP_DELETED = "Group deleted." group_schema = GroupSchema() group_list_schema = GroupSchema(many=True) class Group(Resource): @classmethod def get(cls, id: int): group = GroupModel.find_by_id(id) if group: return group_schema.dump(group), 200 return {"message": GROUP_NOT_FOUND}, 404 @classmethod @jwt_required(fresh=True) def delete(cls, id: int): group = GroupModel.find_by_id(id) if group: group.delete_from_db() return {"message": GROUP_DELETED}, 200