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 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 get(self): stages = (Stage.select(Stage, StageProperty).join( StageProperty, join_type=pw.JOIN_LEFT_OUTER).order_by(Stage.id).aggregate_rows()) stage_count = Stage.select().count() result = { 'meta': { 'page': 1, 'count': stage_count }, 'results': StageSchema(stages, many=True).data } return jsonify(result)
def put(self, stage_id): with db.transaction() as txn: try: data = json.loads(request.data) stage = (Stage.select(Stage, StageProperty).join( StageProperty, join_type=pw.JOIN_LEFT_OUTER).where( Stage.id == stage_id).order_by( Stage.id).aggregate_rows()).first() if stage: updated = Stage(name=data.get('name', stage.name)) stage_schema = StageSchema(only=('name', )) stage_errors = stage_schema.validate(updated._data) if stage_errors: return stage_errors, 400 stage.update(**updated._data).execute() if data.get('properties', None): StageProperty.delete().where( StageProperty.stage == stage_id).execute() for _property in data.get('properties'): stage_properties = StageProperty( stage=stage.id, stage_property=_property.get('stage_property')) stage_property_schema = StagePropertySchema( exclude=('id', )) stage_property_errors = stage_property_schema.validate( stage_properties._data) if stage_property_errors: txn.rollback() return stage_property_errors, 400 stage_properties.save() db.commit() return Response(status=200) return {'error': 'stage not found'}, 404 except Exception, e: txn.rollback() return {'error': e.message}, 403
def validate_is_stage(input_stage): stage = Stage.select().where( Stage.id == int(input_stage.get('id'))).first() if stage: return True raise ValidationError('Not a valid stage')
def get(self, stage_id): stage = (Stage.select(Stage, StageProperty).join( StageProperty, join_type=pw.JOIN_LEFT_OUTER).where(Stage.id == stage_id).order_by( Stage.id).aggregate_rows()).first() if stage: return StageSchema(stage).data return {'error': 'stage not found'}, 404
def post(self): ''' Creates a new stage ''' with db.transaction() as txn: try: data = json.loads(request.data) stage = Stage(name=data.get('name')) stage_schema = StageSchema(only=('name')) stage_errors = stage_schema.validate(stage._data) if stage_errors: return stage_errors, 400 stage.save() for _property in data.get('properties', []): stage_properties = StageProperty( stage=stage.id, stage_property=_property.get('stage_property')) stage_property_schema = StagePropertySchema( exclude=('id', )) stage_property_errors = stage_property_schema.validate( stage_properties._data) if stage_property_errors: txn.rollback() return stage_property_errors, 400 stage_properties.save() db.commit() return Response(status=201) except pw.IntegrityError: txn.rollback() return {'error': 'id already taken'}, 403 except Exception, e: txn.rollback() return {'error': e.message}, 403
def delete(self, stage_id): stage = (Stage.select(Stage, StageProperty).join( StageProperty, join_type=pw.JOIN_LEFT_OUTER).where(Stage.id == stage_id).order_by( Stage.id).aggregate_rows()).first() if stage: stage.delete_instance(recursive=True) return Response(status=204) return {'error': 'stage not found'}, 404
def validate_stage_order(input_order): # << is a peewee operator for SQL IN if len(input_order) == 0: raise ValidationError('You must specify at least one stage') stages = Stage.select(Stage.id).where(Stage.id << input_order) if stages.count() != len(input_order): stage_ids = [i.id for i in stages] bad_stage_ids = list( set(stage_ids).symmetric_difference(set(input_order))) raise ValidationError( 'The following stage ids do not exist: {stages}'.format( stages=', '.join([unicode(i) for i in bad_stage_ids]))) return True
def setUp(self): super(StagesResourceTest, self).setUp() # create fixture stages and flows self.stage = Stage.create(name='stage1').save() self.stage_url = '/api/v1/stage/{id}'.format(id=self.stage)