Search code examples
pythonredminepython-redmine

Query about usage of setattr() in Python-Redmine


setattr() for an item in Redmine issues, is failing, with the following error.

Traceback (most recent call last):
  File "E:\test\get_redmine_data.py", line 47, in <module>
    print (item.assigned_to)
  File "C:\Python27\lib\site-packages\redminelib\resources\standard.py", line 150, in __getattr__
    return super(Issue, self).__getattr__(attr)
  File "C:\Python27\lib\site-packages\redminelib\resources\base.py", line 164, in __getattr__
    attr, encoded = self.encode(attr, decoded, self.manager)
  File "C:\Python27\lib\site-packages\redminelib\resources\base.py", line 266, in encode
    return attr, manager.new_manager(cls._resource_map[attr]).to_resource(value)
  File "C:\Python27\lib\site-packages\redminelib\managers\base.py", line 29, in to_resource
    return self.resource_class(self, resource)
  File "C:\Python27\lib\site-packages\redminelib\resources\base.py", line 130, in __init__
    self._decoded_attrs = dict(dict.fromkeys(relations_includes), **attributes)
TypeError: type object argument after ** must be a mapping, not str

I am trying to set some default assignee, for issues where the assignee is not set. The code fails at the line, where I print the attribute I just set. My code is given below:

redmine = Redmine('http://redmine_url', username='uname', password='pwd')
project = redmine.project.get('proj_name')
work_items = project.issues
for item in work_items:
   assignee_not_set = getattr(item,'assigned_to',True)
   if assignee_not_set == True:
       print item.id
       setattr(item,'assigned_to','Deepak')
       print (item.assigned_to)

I also tried using the update() method,

redmine.project.update(item.id, assigned_to='Deepak')

That also fails with another error - redminelib.exceptions.ResourceNotFoundError: Requested resource doesn't exist. I verifed that the issue id exists in Redmine.


Solution

  • You have several problems here:

    1. The attribute name is assigned_to_id and not assigned_to
    2. It accepts user id which is int and not a username which is str
    3. No need to use setattr() here, just use item.assigned_to_id = 123
    4. You need to call item.save() after setting assigned_to_id otherwise it won't be saved to Redmine
    5. When you're trying to use update() method, you're using in on a Project resource and not on Issue resource, this is why you're getting ResourceNotFoundError

    All this information is available in the docs: https://python-redmine.com/resources/issue.html