예제 #1
0
    def post(self, args, mould_id):
        mould = self.record
        success, error_message = mould.validate_abilities(
            args.get('abilities', {}))
        if not success:
            return error_message, 422

        parent_id = args.pop('parent_id', None)
        if mould.parent:
            if parent_id:
                parent = Instance.find_by_pk(parent_id)
                if not parent:
                    error(InterfaceTips.PARENT_DATA_NOT_EXISTED)
                args.update({'parent': parent})
            else:
                error(InterfaceTips.PARENT_ID_IS_REQUIRED)

        # TODO check record is existed
        # if Mould.existed_record(**args):
        #     error(InterfaceTips.RECORD_HAS_EXISTED)

        instance = Instance.create(mould=mould, **args)
        return instance_detail_schema.dump(instance).data, 201
예제 #2
0
    def put(self, args, instance_id):
        layer_id = args.get('layer_id', None)
        if layer_id and not validate_valid_layer_id(layer_id):
            error(InterfaceTips.INVALID_LAYER_ID)
        instance = self.record
        success, error_message = instance.mould.validate_abilities(
            args.get('abilities', {}))
        if not success:
            return error_message, 422

        parent_id = args.pop('parent_id', None)
        if instance.mould.parent:
            if parent_id:
                parent = Instance.find_by_pk(parent_id)
                if not parent:
                    error(InterfaceTips.PARENT_DATA_NOT_EXISTED)
                args.update({'parent': parent})
            else:
                error(InterfaceTips.PARENT_ID_IS_REQUIRED)

        # if Mould.existed_record(instance, **args):
        #     error(InterfaceTips.RECORD_HAS_EXISTED)
        instance = instance.update(**args)
        return instance_detail_schema.dump(instance).data
예제 #3
0
 def get(self, instance_id):
     instance = self.record
     return instance_detail_schema.dump(instance).data