Skip to content

Commit

Permalink
Fixed add_attachments which now is able to stream the files, preventi…
Browse files Browse the repository at this point in the history
…ng size limitation bugs.
  • Loading branch information
ssbarnea committed Jan 13, 2015
2 parents 76ac3b2 + fefe644 commit 5bf1c96
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
15 changes: 7 additions & 8 deletions jira/client.py
Expand Up @@ -27,6 +27,8 @@

from six import string_types
from six.moves import html_parser
from requests_toolbelt import MultipartEncoder
import requests

# JIRA specific resources
from jira.resources import Resource, Issue, Comment, Project, Attachment, Component, Dashboard, Filter, Votes, Watchers, \
Expand Down Expand Up @@ -342,16 +344,13 @@ def add_attachment(self, issue, attachment, filename=None):
if not fname:
fname = os.path.basename(attachment.name)

content_type = mimetypes.guess_type(fname)[0]
if not content_type:
content_type = 'application/octet-stream'
m = MultipartEncoder(
fields={
'file': (fname, attachment, 'text/plain')}
)

files = {
'file': (fname, attachment, content_type)
}
# requests knows to add the correct content type when we set it None
r = self._session.post(
url, files=files, headers=CaseInsensitiveDict({'content-type': None})) # 'multipart/form-data'
url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type})) # 'multipart/form-data'

attachment = Attachment(self._options, self._session, json_loads(r)[0])
return attachment
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -3,3 +3,4 @@ tlslite>=0.4.4
six>=1.9.0
filemagic>=1.6
setuptools>=0.8.0
requests_toolbelt
7 changes: 6 additions & 1 deletion setup.py
Expand Up @@ -38,6 +38,10 @@ def initialize_options(self):
# pass
self.pytest_args.append("-s")

if os.isatty(sys.stdout.fileno()):
# when run manually we enable fail fast
self.pytest_args.append("--maxfail=2")

try:
import coveralls
self.pytest_args.append("--cov=%s" % NAME)
Expand Down Expand Up @@ -102,7 +106,8 @@ def run(self):
install_requires=['requests>=1.2.3',
'requests_oauthlib>=0.3.3',
'tlslite>=0.4.4',
'six>=1.5.2'],
'six>=1.5.2',
'requests_toolbelt'],
setup_requires=[],
tests_require=['pytest', 'tlslite>=0.4.4', 'requests>=2.0',
'setuptools', 'pep8', 'autopep8', 'sphinx', 'six>=1.9.0'],
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.py
Expand Up @@ -417,7 +417,7 @@ def test_add_attachment(self):
def test_delete(self):
attach_count = len(self.jira.issue(self.issue_1).fields.attachment)
attachment = self.jira.add_attachment(self.issue_1,
open(TEST_ATTACH_PATH))
open(TEST_ATTACH_PATH), 'to be deleted')
self.assertEqual(len(self.jira.issue(self.issue_1).fields.attachment),
attach_count + 1)
attachment.delete()
Expand Down

0 comments on commit 5bf1c96

Please sign in to comment.