Example #1
0
    def process(cls, root, info, **inputs):
        """delete object according id, if it's from ArchiveModel then just archive it"""
        if cls.model:
            base64_id = inputs.get(cls.id_name, '').strip()
            if base64_id:
                _type, _id = from_global_id(base64_id)
                if not _type.startswith(cls.model.__name__):
                    # base64 id type not match
                    raise MutationException(f'{base64_id} is a invalid {cls.model.__name__} id.', code='not_found')

                if issubclass(cls.model, ArchiveModel):
                    delete_count = cls.model.objects.filter(id=_id).update(is_archived=True)
                    # deleted_object = delete_count and cls.model.objects.get(id=_id) or None
                    deleted_object = None  # at this moment only return 200 status without deleted object
                else:
                    delete_count, deleted_object = cls.model.objects.filter(id=_id).delete()

                if not delete_count:
                    raise MutationException(f'{cls.model.__name__} with id={_id} not found.',
                                            code='not_found')
                return deleted_object
            else:
                raise MutationException(f'Please provide id for {cls.model.__name__}',
                                        code='mandatory')
        raise NotImplementedError
Example #2
0
    def validate(cls, root, info, **inputs):
        base64_id = inputs.get(cls.id_name, '').strip()
        if not base64_id:
            raise MutationException('Please provide id.', code='mandatory')

        _type, _id = from_global_id(base64_id)
        if not cls.model.objects.filter(id=_id).exists():
            raise MutationException(f'{cls.model.__name__} id={base64_id} not found.', code='not_found')

        return inputs
Example #3
0
    def process(cls, root, info, **inputs):
        if cls.model:
            base64_id = inputs.get(cls.id_name, '').strip()
            input_name = cls.get_primary_input_name()
            input_data = inputs.get(input_name, {})
            if base64_id and input_data:
                _type, _id = from_global_id(base64_id)
                updated_count = cls.model.objects.filter(id=_id).update(**input_data)
                if not updated_count:
                    raise MutationException(f'{input_name} with id={_id} not found.', code='not_found')

                obj = cls.model.objects.get(id=_id)
                return obj
            raise MutationException(f'{input_name} with id={base64_id} not found.', code='not_found')
        raise NotImplementedError
Example #4
0
    def validate_fk_field(cls, data, field_name, fk_model, mandatory=True):
        """convert base64 global id to local db id"""
        base64_id = data.get(field_name, None)

        if not base64_id and mandatory:
            raise MutationException(f'Please provide `{field_name}`.', code='mandatory')

        try:
            _type, db_id = from_global_id(base64_id)
        except Exception as ex:
            raise MutationException(f'Please provide a validate global id for field {field_name}.', code='not_found')

        if fk_model.objects.filter(id=db_id).exists():
            data[field_name] = db_id  # replace base64 global id with db int id
        else:
            raise MutationException(f'{field_name} id={base64_id} not found.', code='not_found')
Example #5
0
    def process(cls, root, info, **inputs):
        if cls.model:
            input_name = cls.get_primary_input_name()
            data = inputs.get(input_name, {})
            if not data:
                raise MutationException(f'Please provide data for {input_name}', code='mandatory')
            try:
                # try to use create classmethod in model
                create_func = cls.get_create_from_model()
                if create_func:
                    obj = create_func(**data)
                else:
                    obj = cls.model.objects.create(**data)
            except Exception as ex:
                raise MutationException(str(ex), code=ex.__class__.__name__)

            return obj
        raise NotImplementedError
Example #6
0
    def validate(cls, root, info, **inputs):
        if cls.model:
            input_name = cls.get_primary_input_name()
            if inputs.get(input_name, {}):
                return inputs
            else:
                raise MutationException(f'Please provide data for {input_name}', code='mandatory')

        raise NotImplementedError
Example #7
0
 def validate(cls, root, info, **inputs):
     if not inputs.get(cls.id_name, '').strip():
         raise MutationException(f'Please provide id for {cls.model.__name__}.', code='mandatory')
     return inputs