Skip to content

Commit 70772a8

Browse files
committed
original filename for uploads if filename not set
1 parent 5826dc3 commit 70772a8

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Changelog
2020
- ``dir(resource)`` and ``list(resource)`` now also show properties of an object
2121
- Support for ``issues_assigned`` and ``issues_authored`` relations in User object
2222
(`Issue #317 <https://github.com/maxtepkeev/python-redmine/issues/317>`__)
23+
- Original filename will be used as a filename for all uploaded files if a path was provided and filename wasn't set
2324
- *Pro Edition:* Added support for RedmineUP DealCategory ``create()``, ``update()``, ``delete()`` operations
2425
(see `docs <https://python-redmine.com/resources/deal_category.html#create-methods>`__ for details)
2526
- *Pro Edition:* RedmineUP CrmQuery resource now supports ``invoices`` and ``expenses`` relation attributes

docs/advanced/working_with_files.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Upload
2929
>>> data
3030
{'id': 7167, 'token': '7167.ed1ccdb093229ca1bd0b043618d88743'}
3131
32-
If a file-like object is provided, be sure that it contains ``str`` and not ``unicode`` under Python 2, and
33-
``bytes`` and not ``str`` under Python 3, otherwise Python-Redmine will have to make an additional conversion,
34-
which will affect performance.
32+
If a filename isn't specified, Python-Redmine will use the original filename from a path, if any. If a
33+
file-like object is provided, be sure that it contains ``bytes`` and not ``str``, otherwise Python-Redmine
34+
will have to make additional conversion, which will affect performance.
3535

3636
Download
3737
--------

redminelib/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ def upload(self, f, filename=None):
109109
if self.ver is not None and self.ver < (1, 4, 0):
110110
raise exceptions.VersionMismatchError('File uploading')
111111

112-
url = f'{self.url}/uploads.json'
113-
headers = {'Content-Type': 'application/octet-stream'}
114-
params = {'filename': filename or ''}
115-
116112
# There are myriads of file-like object implementations here and there and some of them don't have
117113
# a "read" method, which is wrong, but that's what we have, on the other hand it looks like all of
118114
# them implement a "close" method, that's why we check for it here. Also, we don't want to close the
@@ -138,9 +134,16 @@ def upload(self, f, filename=None):
138134
if not os.path.isfile(f) or os.path.getsize(f) == 0:
139135
raise exceptions.NoFileError
140136

137+
if not filename:
138+
filename = os.path.basename(f)
139+
141140
stream = open(f, 'rb')
142141
close = True
143142

143+
url = f'{self.url}/uploads.json'
144+
headers = {'Content-Type': 'application/octet-stream'}
145+
params = {'filename': filename or ''}
146+
144147
response = self.engine.request('post', url, params=params, data=stream, headers=headers)
145148

146149
if close:

0 commit comments

Comments
 (0)