def patch(self, id): """ Areas Method to update a single Area --- tags: - Areas parameters: - name: id in: path type: int required: true description: The area Id - in: body name: body schema: id: AreaToUpdate properties: name: type: string description: The name of the Area default: "Area Name String" responses: 200: description: The updated area schema: $ref: '#/definitions/Area' """ area = Area.query.get_or_404(id) area_dict = request.get_json() if not area_dict: resp = {'message': 'No input data provided'} return resp, status.HTTP_400_BAD_REQUEST errors = area_schema.validate(area_dict) if errors: return errors, status.HTTP_400_BAD_REQUEST try: if 'name' in area_dict: area_name = area_dict['name'] if Area.is_unique(id=id, name=area_name): area.name = area_name else: response = { 'error': 'A area with the same name already exists' } return response, status.HTTP_400_BAD_REQUEST area.update() return self.get(id) except SQLAlchemyError as e: db.session.rollback() resp = {"error": str(e)} return resp, status.HTTP_400_BAD_REQUEST
def post(self): """ Areas Method to create a new Area --- tags: - Areas parameters: - in: body name: body schema: $ref: '#/definitions/AreaToUpdate' responses: 201: description: The updated area schema: $ref: '#/definitions/Area' """ request_dict = request.get_json() if not request_dict: resp = {'message': 'No input data provided'} return resp, status.HTTP_400_BAD_REQUEST errors = area_schema.validate(request_dict) if errors: return errors, status.HTTP_400_BAD_REQUEST area_name = request_dict['name'] if not Area.is_unique(id=0, name=area_name): response = {'error': 'A area with the same name already exists'} return response, status.HTTP_400_BAD_REQUEST try: area = Area(area_name) area.add(area) query = Area.query.get(area.id) result = area_schema.dump(query).data return result, status.HTTP_201_CREATED except SQLAlchemyError as e: db.session.rollback() resp = {"error": str(e)} return resp, status.HTTP_400_BAD_REQUEST
def create_areas(self): for a in areas: if Area.is_unique(id=0, name=a): area = Area(a) area.add(area)