From 49899133047e25df3a97b0a2b3748e5d461f2dfa Mon Sep 17 00:00:00 2001 From: Rick Rongen Date: Tue, 12 Mar 2019 11:14:00 +0100 Subject: [PATCH] Added support for asset upload Removed lxml dependency --- Crx/connection.py | 28 +++++++++++++++++++++++++++- requirements.txt | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Crx/connection.py b/Crx/connection.py index 65a3c08..6db7969 100644 --- a/Crx/connection.py +++ b/Crx/connection.py @@ -25,6 +25,8 @@ CRX_SERVER_ROOT = '/crx/server/crx.default/jcr:root/' CRX_QUERY = '/crx/de/query.jsp' WCM_REFERENCES = '/bin/wcm/references.json' +CREATE_ASSET = '.createasset.html' + JSON_DATA_EXTENSION = '.1.json' QUERY_TYPES = { @@ -66,7 +68,7 @@ class Connection: """ self._session.auth = (username, password) - def query(self, query: str, query_type: str = 'SQL2') -> List[str]: + def query(self, query: str, query_type: str = 'SQL2', raise_on_error: bool = True) -> List[str]: """ Perform an query and return the matching paths. Query may be an XPATH, SQL or SQL2 Query @@ -87,6 +89,8 @@ class Connection: data = response.json() # TODO check for error + if not data['success']: + raise ValueError(data['errorMessage']) return list(map(lambda node: node['path'], data['results'])) @@ -106,6 +110,28 @@ class Connection: }) return response.json()['pages'] + def upload_asset(self, dam_directory: str, filename: str, data: bytes, content_type: str): + """ + Upload an asset to the DAM as if it was uploaded through the GUI + + Args: + dam_directory: The directory to upload to (including /content/dam) + filename: The file name of the asset (no path or anything) + data: The content of the asset + content_type: The content type of the asset + + Raises: + When an error occurs, Request will raise an error for the incorrect status code + """"" + url: str = self._host + dam_directory + CREATE_ASSET + files = { + 'file': (filename, data, content_type), + 'fileName': filename, + '_charset_': 'utf-8' + } + resp = self._session.post(url, files=files) + resp.raise_for_status() + def get_node_raw(self, path: str): """ Get the raw JSON dictionary of a node. diff --git a/requirements.txt b/requirements.txt index c11a8e1..414a690 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ requests >= 2.21.0 -lxml >= 4.3.0 +#lxml >= 4.3.0 # For unprotect function # pywin32 >= 224