pyopenproject icon indicating copy to clipboard operation
pyopenproject copied to clipboard

How to add_attachment in work packages ?

Open aa6my opened this issue 2 years ago • 0 comments

Hi, i already try the def add_attachment in work_package_service.py, have bug or i using wrong method ?

oP = OpenProject(url=os.getenv('OP_URL'), api_key=os.getenv('OP_APIKEY'))
wS = oP.get_work_package_service()
aT = oP.get_attachment_service()

updates_wP = wS.find_all()
wP_undict = json.dumps(updates_wP[0].__dict__)
wp_dict = json.loads(wP_undict, object_hook=lambda d: SimpleNamespace(**d))

attachment = json.loads(json.dumps(aT.create(filename="openproject.png",description="Openproject logo!",file_path='openproject.png').__dict__), object_hook=lambda d: SimpleNamespace(**d))

a = Attachment({'id': int(attachment.id)})
find = aT.find(a)

wS.add_attachment(wp_dict, a)

variable find

{"_embedded": {"author": {"_type": "User", "id": 5, "name": "test first", "createdAt": "2023-01-16T03:59:24Z", "updatedAt": "2023-01-30T05:43:27Z", "login": "testfirst", "admin": true, "firstName": "try", "lastName": "first", "email": "[email protected]", "avatar": "https://secure.gravatar.com/avatar/eb7123308772a36e26f1e0c2caaf3081?default=404&secure=true", "status": "active", "identityUrl": null, "language": "en", "_links": {"self": {"href": "/api/v3/users/5", "title": "try first"}, "memberships": {"href": "/api/v3/memberships?filters=%5B%7B%22principal%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%225%22%5D%7D%7D%5D", "title": "Members"}, "showUser": {"href": "/users/5", "type": "text/html"}, "updateImmediately": {"href": "/api/v3/users/5", "title": "Update testfirst", "method": "patch"}, "lock": {"href": "/api/v3/users/5/lock", "title": "Set lock on testfirst", "method": "post"}, "auth_source": {"href": "/api/v3/auth_sources/1", "title": "test"}}}}, "_type": "Attachment", "id": 174, "fileName": "openproject.png", "fileSize": 1524, "description": {"format": "plain", "raw": "Openproject logo!", "html": "<p>Openproject logo!</p>"}, "contentType": "image/png", "digest": {"algorithm": "md5", "hash": "1b02dd2fb4b6951b12378b5b992f7f74"}, "createdAt": "2023-02-03T07:43:05Z", "_links": {"self": {"href": "/api/v3/attachments/174", "title": "openproject.png"}, "author": {"href": "/api/v3/users/5", "title": "test first"}, "container": {"href": null}, "staticDownloadLocation": {"href": "/api/v3/attachments/174/content"}, "downloadLocation": {"href": "/api/v3/attachments/174/content"}, "delete": {"href": "/api/v3/attachments/174", "method": "delete"}}}

error

Traceback (most recent call last):
  File "/home/aa6my/.local/lib/python3.10/site-packages/pyopenproject/api_connection/request.py", line 32, in execute
    response.raise_for_status()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 415 Client Error: Unsupported Media Type for url: https://test.com/api/v3/work_packages//1/attachments

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/aa6my/.local/lib/python3.10/site-packages/pyopenproject/business/services/command/work_package/add_attachment.py", line 26, in execute
    json=self.attachment.__dict__).execute()
  File "/home/aa6my/.local/lib/python3.10/site-packages/pyopenproject/api_connection/request.py", line 59, in execute
    raise RequestError(f"Error running request with the URL (HTTPError):"
pyopenproject.api_connection.exceptions.request_exception.RequestError: Error running request with the URL (HTTPError): 'https://test.com/api/v3/work_packages//1/attachments'.
 {"_type":"Error","errorIdentifier":"urn:openproject-org:api:v3:errors:TypeNotSupported","message":"Expected CONTENT-TYPE to be 'multipart/form-data' but got 'application/json'."}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/aa6my/_dev/opmigration/migrate.py", line 15, in <module>
    attachment()
  File "/home/aa6my/_dev/opmigration/main.py", line 146, in attachment
    wS.add_attachment(wp_dict, find)
  File "/home/aa6my/.local/lib/python3.10/site-packages/pyopenproject/business/services/work_package_service_impl.py", line 44, in add_attachment
    return AddAttachment(self.connection, work_package, attachment).execute()
  File "/home/aa6my/.local/lib/python3.10/site-packages/pyopenproject/business/services/command/work_package/add_attachment.py", line 28, in execute
    raise BusinessError(f"Error adding new attachment: {self.attachment.title}") from re
AttributeError: 'Attachment' object has no attribute 'title'

aa6my avatar Feb 03 '23 09:02 aa6my