odooredmine-apipython-redmine

Attachments in Redmine Python Rest API


enter code heredir(sttachments) will give the following value.

[u'author', u'content_type', u'content_url', u'created_on', u'description', u'filename', u'filesize', u'id', u'thumbnail_url']

But I cannot able to read the container_id of attachments, due to this I can't able to map my attachments with issue.

How to read the container_id of attachments and how to map the attachments with issue

    from redmine import Redmine
    conn_red = Redmine('http://localhost:3000', username='admin',   password='admin')
    issue = conn_red.issue.all()
    attachment = {}
    att_list = []
    for id in issue:
       for att in id.attachments:
           attachment['file_name'] = att.content_url
           attachment['created_date'] = att.created_on
           att_list.append((0, 0, attachment))
           print "issue",id.id,"attachments",att.id,"att_list",att_list

The above code give the following result

issue 7 attachments 5 att_list [(0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)})]
issue 6 attachments 5 att_list [(0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)})]
issue 5 attachments 5 att_list [(0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)})]
issue 4 attachments 5 att_list [(0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)})]
issue 3 attachments 5 att_list [(0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/5/3BasicSkills_Eng.pdf', 'created_date': datetime.datetime(2016, 6, 2, 8, 43, 51)})]
issue 2 attachments 3 att_list [(0, 0, {'file_name': u'http://localhost:3000/attachments/download/3/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 15, 42)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/3/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 15, 42)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/3/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 15, 42)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/3/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 15, 42)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/3/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 15, 42)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/3/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 15, 42)})]
issue 1 attachments 4 att_list [(0, 0, {'file_name': u'http://localhost:3000/attachments/download/4/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 16, 40)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/4/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 16, 40)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/4/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 16, 40)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/4/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 16, 40)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/4/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 16, 40)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/4/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 16, 40)}), (0, 0, {'file_name': u'http://localhost:3000/attachments/download/4/page.png', 'created_date': datetime.datetime(2016, 5, 30, 8, 16, 40)})]

The second list is repeated with same data.

EDIT ONE

I here by attach the screen shot of attachments table, here container_id is associated with issues number, so I want to prepare a list based on issue with attachments,

For example Issue no 1 i.e container_id 1 has two attachments i.e ID 1 and 4, so I have to prepare a list [(0,0,{filename:'vignesh.png'}),(0,0,{filename:'page.png'})], so issue number 1 has two issue that should be created as a list.


Solution

  • So as far as I understood the problem is the repeated data, but it has nothing to do with Python-Redmine or Redmine's REST API. The problem happens because your att_list = [] is defined on module level, if you define it inside first for() loop then everything should work as expected, e.g.:

    from redmine import Redmine
    conn_red = Redmine('http://localhost:3000', username='admin', password='admin')
    issues = conn_red.issue.all()
    for issue in issues:
       att_list = []
       for att in issue.attachments:
           attachment = {
               'file_name': att.content_url,
               'created_date': att.created_on
           }
           att_list.append((0, 0, attachment))
    
       print "issue",issue.id, "att_list", att_list