def setUp(self): super(ContractsResourceTest, self).setUp() # create fixture stages and flows Stage.create(name='stage1').save() Stage.create(name='stage2').save() Flow.create(flow_name='flow1', stage_order=[1,2]).save() self.contract = Contract.create( item_number='1', spec_number='2', department='foo', commodity_title='test', status_comments='none', flow_name='flow1', current_stage=1 ).save() self.contract_url = '/api/v1/contract/{id}'.format(id=self.contract)
def validate_is_flow(flow_name): flow = Flow.select().where(Flow.flow_name == flow_name).first() if flow: return True raise ValidationError('Not a valid flow name')
def setUp(self): super(FlowsResourceTest, self).setUp() # create fixture stages and flows Stage.create(name='stage1').save() Stage.create(name='stage2').save() self.flow = Flow.create(flow_name='flow1', stage_order=[1, 2]).save()
def get(self, flow_id): flow = Flow.select().where(Flow.id == flow_id).first() if flow: return FlowSchema(flow).data return {'error': 'flow not found'}, 404
def delete(self, flow_id): flow = Flow.select().where(Flow.id == flow_id).first() if flow: flow.delete_instance() return Response(status=204) return {'error': 'flow not found'}, 404
def post(self): try: data = json.loads(request.data) flow = Flow(flow_name=data.get('flow_name'), stage_order=data.get('stage_order')) flow_schema = FlowSchema(exclude=('id', )) errors = flow_schema.validate(flow._data) if errors: return errors, 400 flow.save() return Response(status=201) except pw.IntegrityError, e: return {'error': e.message}, 400
def put(self, flow_id): flow = Flow.select().where(Flow.id == flow_id).first() if flow: data = json.loads(request.data) updated = Flow(flow_name=data.get('flow_name', flow.flow_name), stage_order=data.get('stage_order', flow.stage_order)) flow_schema = FlowSchema(exclude=('id', )) errors = flow_schema.validate(updated._data) if errors: return errors, 400 flow.update(**updated._data).execute() return Response(status=200) return {'error': 'flow not found'}, 404
def get(self): flows = Flow.select() flow_count = flows.count() result = { 'meta': { 'page': 1, 'count': flow_count }, 'results': FlowSchema(flows, many=True).data } return jsonify(result)