nephila / python-taiga

:evergreen_tree: Python module for communicating with the Taiga API
https://python-taiga.readthedocs.io
MIT License
94 stars 41 forks source link

Wiki Page update fails due to Version number #149

Closed heieisch closed 9 months ago

heieisch commented 9 months ago

Description

when updating the content of an wiki Page the update fails unless you give the Version of the page as a parameter

Steps to reproduce

wiki_page = bord.list_wikipages()[0]
wiki_page.content = 'foobar'
#wiki_page.update(version=wiki_page.version)
wiki_page.update()

Versions

python taiga 1.1.0 (2023-04-23) python 3.9, 3.10 tested on Windows 11, Mac OS 13 and Fedora 38

Expected behaviour

Update of wiki Page without error

Actual behaviour

---------------------------------------------------------------------------
TaigaRestException                        Traceback (most recent call last)
/var/folders/tm/kg1mgm0n66l_rtzbfkwvwq500000gn/T/ipykernel_80815/11173270.py in <module>
      8 
      9 wiki_page.content = 'foobar'
---> 10 wiki_page.update()

/opt/anaconda3/lib/python3.9/site-packages/taiga/models/base.py in update(self, **args)
    152         if args:
    153             self_dict = dict(list(self_dict.items()) + list(args.items()))
--> 154         response = self.requester.put("/{endpoint}/{id}", endpoint=self.endpoint, id=self.id, payload=self_dict)
    155         obj_json = response.json()
    156         if "version" in obj_json:

/opt/anaconda3/lib/python3.9/site-packages/taiga/requestmaker.py in put(self, uri, payload, query, **parameters)
    156             return result
    157         else:
--> 158             raise exceptions.TaigaRestException(full_url, result.status_code, result.text, "PUT")
    159 
    160     def patch(self, uri, payload=None, query=None, **parameters):

TaigaRestException: {"version": "The version parameter is not valid"}

Additional information

using wiki_page.update(version=wiki_page.version) it works seamlessly

protoroto commented 9 months ago

@heieisch Thanks for the report! Are you able to open a pr to solve this problem?

heieisch commented 9 months ago

I probably am. From looking a few minutes into it it seems that the to_dict() method only outputs allowed_params and version is not one of them. Therefore the requester is missing this information.

Updating the self_dict, with the version key an value, in the update() function before the args update seems to be an hacky solution.

def update(self, **args):
        """
        Update the current :class:`InstanceResource`
        """
        self_dict = self.to_dict()
        if args:
            self_dict = dict(list(self_dict.items()) + list(args.items()))
        response = self.requester.put("/{endpoint}/{id}", endpoint=self.endpoint, id=self.id, payload=self_dict)
        obj_json = response.json()
        if "version" in obj_json:
            self.__dict__["version"] = obj_json["version"]
        return self

@protoroto Do you have a better idea how to get the. values to the requester?

protoroto commented 9 months ago

@heieisch I'll try to look into this tomorrow and I'll let you know! Thanks again!

protoroto commented 9 months ago

@heieisch Maybe adding version to the WikiPage allowed_params could be the right thing to do?

heieisch commented 9 months ago

Yes that would be an option. I do not know the implications of adding it to allowed_params but doing so solves the Issue.

protoroto commented 9 months ago

@heieisch I'll discuss this with @yakky and let you know as soon as possible

protoroto commented 9 months ago

@heieisch After discussing this with @yakky , we think that it is perfectly safe to add version to WikiPage allowed_params . If you open a pr I'll be happy to review it and merge/release a new version as soon as possible.