gertvdijk / redmine-file-uploader

Python script to upload files to the 'Files' section in a Redmine project.
Apache License 2.0
6 stars 3 forks source link

Add option to delete files #4

Open Farkal opened 8 years ago

Farkal commented 8 years ago

I need to delete files in redmine Files module.

I find this code but it's in ruby:

agent.get(URI.join(url, files_page_path)) do |files_page|
  file_link = files_page.link_with(:text => File.basename(filename))
  if file_link
    file_id = file_link.href.match(/download\/(\d+)/)[1].to_i
    agent.post(URI.join(url, "/attachments/#{file_id}"), {'_method' => 'delete', 'authenticity_token' => files_page.at('meta[@name="csrf-token"]')[:content]})
  end
end
Farkal commented 8 years ago

I have made a function to delete the files:

def delete_file(self, filename):
        page = self.browser.open(str(self.url + 'projects/' + self.project_id + '/files'))
        tree = lxml.html.fromstring(page.get_data())
        meta_csrftoken = tree.xpath("//meta[@name='csrf-token']/@content")[0]
        for link in self.browser.links(text_regex=filename):
            file_id = link.url.split("/")[-2]
            url = self.url + 'attachments/' + file_id
            print "Deleting", url
            data = urllib.urlencode({u'_method': 'delete', u'authenticity_token': meta_csrftoken})
            self.browser.open(url, data)
            print "File deleted"

But you need to add this imports

import urllib
import lxml.html