Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__pycache__/
.venv/
build/
dist/
connectpyse.egg-info/
10 changes: 9 additions & 1 deletion connectpyse/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@

0.7.1
---
- Added support for Opportunity Teams
- Added support for Opportunity Teams

0.7.2
---
- Added support for gathering object _info

0.7.5
---
- Added support for Document Download
17 changes: 17 additions & 0 deletions connectpyse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ their appropriate sections. Import the API class(es) you want to leverage and th
>>> print(doc.title)
>>>

### To download the documents attached to an opportunity:

>>> from connectpyse.system import document_api
>>> from connectpyse.system import document_download_api
>>> from connectpyse.sales import opportunity_api
>>> o = opportunity_api.OpportunityAPI(url=URL, auth=AUTH)
>>> d = document_api.DocumentAPI(url=URL, auth=AUTH)

>>> a_opp = o.get_opportunity_by_id(1234)
>>> myDocs = d.get_documents(a_pp)
>>> for doc in myDocs:
>>> doc_download = document_download_api.DocumentDownloadAPI(doc.id, url=URL, auth=AUTH)
>>> ## Option 1: Get the BytesIO object
>>> bytes = doc_download.download_document()
>>> ## Option 2: Download directly to disk
>>> doc_download.save_to_file("/path/to/file.txt")

### For example to get a Member's office phone number you would:

>>> from connectpyse.system import members_api
Expand Down
6 changes: 6 additions & 0 deletions connectpyse/cw_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Parent class for module controller classes
from io import BytesIO
from .config import API_URL, basic_auth, ENSURE_ASCII
from .restapi import Client

Expand Down Expand Up @@ -33,6 +34,11 @@ def _get(self):
for json in json_results:
yield self._class(json)

def _get_bytes(self):
results_str = getattr(self, self.module).get(user_headers=self.basic_auth,
user_params=self._format_user_params())
return self._class({"bytes": BytesIO(results_str.encode("iso-8859-1"))})

def _create(self, a_object):
# Ideally take the_item and submit that as the user_data
try:
Expand Down
10 changes: 10 additions & 0 deletions connectpyse/system/document_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from ..cw_model import CWModel


class DocumentDownload(CWModel):

def __init__(self, json_dict=None):
self.bytes = None # (BytesIO)

# initialize object with bytes
super().__init__(json_dict)
22 changes: 22 additions & 0 deletions connectpyse/system/document_download_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from ..cw_controller import CWController
# Class for /system/document/:id/download
from . import document_download


class DocumentDownloadAPI(CWController):
def __init__(self, parent, **kwargs):
self.module_url = 'system'
self.module = 'documents/{}/download'.format(parent)
self._class = document_download.DocumentDownload
super().__init__(**kwargs) # instance gets passed to parent object

def download_document(self):
return super()._get_bytes()

def save_to_file(self, path=""):
document = self.download_document()
if path == "":
raise ValueError("A valid path is required to save the document.")
with open(path, 'wb') as f:
f.write(document.bytes.getvalue())
return True
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
long_description = f.read()

setup(name='connectpyse',
version='0.7.3',
version='0.7.5',
description='A ConnectWise API tool for the rest of us.',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
Loading