flask-restplus-server-example icon indicating copy to clipboard operation
flask-restplus-server-example copied to clipboard

Nested path for PATCH operations ?

Open askz opened this issue 6 years ago • 2 comments

imagine I have a profile linked to my user and I want to expose only one route (/users/) to patch the twos, is it actually possible ? Seems like the actual implementation isn't compatible ? is it ?

I created two tuples and merged them with :
PATH_CHOICES = USER_CHOICES (as /%s) + PROFILE_CHOICES (as /profile/%s)

But I get this error :

marshmallow.exceptions.ValidationError: Field 'parts_number' does not exist, so it cannot be patched

askz avatar Sep 20 '17 17:09 askz

So, I came up with :

    @classmethod
    def replace(cls, obj, field, value, state):
        if 'profile/' in field:
            sub_obj, sub_field = field.split('/')
            sub_obj = getattr(obj, sub_obj)
            if hasattr(sub_obj, sub_field):
                obj = sub_obj
                field = sub_field
        if not hasattr(obj, field):
            raise ValidationError("Field '%s' does not exist, so it cannot be patched" % field)
        setattr(obj, field, value)
        return True

This is obviously temporary and hard-coded for my specific use case. But in the future I'll probably need it for more modules, and will make a PR if I have the time !

I'm open to suggestions though.

askz avatar Sep 22 '17 11:09 askz

@askz I am sorry for the late response! I think, your case is quite legal, though I had never had such a requirement yet. I will be happy to accept a PR. Your ad-hoc implementation is quite reasonable to me.

frol avatar Sep 24 '17 15:09 frol