Skip to content

Commit

Permalink
Merge pull request #222 from KerkhoffTechnologies/3636-add-attachment…
Browse files Browse the repository at this point in the history
…-uploading

[ISSUE-3636] Add attachment uploading
  • Loading branch information
kti-sam authored Aug 16, 2024
2 parents 29a27db + 61ba5dc commit 43ee861
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion djautotask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
VERSION = (1, 6, 1, 'final')
VERSION = (1, 6, 2, 'final')

# pragma: no cover
if VERSION[-1] != "final":
Expand Down
22 changes: 21 additions & 1 deletion djautotask/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.core.cache import cache
from django.db import models
from django.utils import timezone
from djautotask.utils import DjautotaskSettings
from djautotask.utils import DjautotaskSettings, encode_file_to_base64
from retrying import retry

RETRY_WAIT_EXPONENTIAL_MULTAPPLIER = 1000 # Initial number of milliseconds to
Expand Down Expand Up @@ -973,6 +973,26 @@ def document_download(self, object_id, document_id, record_type):
self._log_failed(response)
return None

def upload_attachments(self, object_id, file, recordType):
ENDPOINT_DOCUMENTS = f'{recordType}/{object_id}/Attachments'
endpoint_url = f'{self.api_base_url}/{ENDPOINT_DOCUMENTS}'

file_name, file_content = file['file']
file_data = encode_file_to_base64(file_content)

body = {
"id": 0,
"attachmentType": "FILE_ATTACHMENT",
"fullPath": file_name,
"publish": 1,
"title": file_name,
"data": file_data
}

response = self.request('POST', endpoint_url, body)

return response

def get_attachment(self, object_id, document_id, record_type):
return self.document_download(object_id, document_id, record_type)

Expand Down
5 changes: 5 additions & 0 deletions djautotask/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
from django.conf import settings


Expand All @@ -17,3 +18,7 @@ def get_settings(self):
request_settings.update(settings.DJAUTOTASK_CONF_CALLABLE())

return request_settings


def encode_file_to_base64(file_content):
return base64.b64encode(file_content.read()).decode('utf-8')

0 comments on commit 43ee861

Please sign in to comment.