From 99e3526865e0c58a4c8b270753741b491f67251b Mon Sep 17 00:00:00 2001 From: Rick Rongen Date: Tue, 12 Mar 2019 15:04:42 +0100 Subject: [PATCH] Added support for (De)Activation of pages and added a better error for missing nodes --- Crx/__init__.py | 4 ++-- Crx/connection.py | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Crx/__init__.py b/Crx/__init__.py index 62774b4..59583c3 100644 --- a/Crx/__init__.py +++ b/Crx/__init__.py @@ -1,7 +1,7 @@ -from .connection import Connection +from .connection import Connection, CrxException, CrxNodeNotFound from .simplenode import SimpleNode from .util import get_simple_con __version__ = (1, 0, 0, 0) -__all__ = ["Connection", "SimpleNode", "get_simple_con"] +__all__ = ["Connection", "CrxException", "CrxNodeNotFound", "SimpleNode", "get_simple_con"] diff --git a/Crx/connection.py b/Crx/connection.py index 6db7969..8731cc6 100644 --- a/Crx/connection.py +++ b/Crx/connection.py @@ -24,6 +24,7 @@ download.jsp?path=%2Fcontent%2Fdam%2Fbeeldbank%2Fvrouw-direct-naar.jpg%2Fjcr%3Ac CRX_SERVER_ROOT = '/crx/server/crx.default/jcr:root/' CRX_QUERY = '/crx/de/query.jsp' WCM_REFERENCES = '/bin/wcm/references.json' +WCM_REPLICATE = '/bin/replicate.json' CREATE_ASSET = '.createasset.html' @@ -40,6 +41,12 @@ class CrxException(ValueError): pass +class CrxNodeNotFound(CrxException): + def __init__(self, path: str, response: requests.Response): + self.path = path + self.response = response + + class Connection: def __init__(self, host: str = 'localhost', @@ -47,12 +54,14 @@ class Connection: protocol: str = 'http', root: str = CRX_SERVER_ROOT, query: str = CRX_QUERY, - image_references: str = WCM_REFERENCES): + image_references: str = WCM_REFERENCES, + wcm_replicate: str = WCM_REPLICATE): self._host = f'{protocol}://{host}:{port}' self._data_root = self._host + root self._query_path = self._host + query self._image_references = self._host + image_references + self._wcm_replicate = self._host + wcm_replicate self._session = requests.session() @@ -149,6 +158,9 @@ class Connection: except requests.exceptions.RequestException as exception: raise CrxException() # todo more specific exceptions + if response.status_code == 404: + raise CrxNodeNotFound(path, response) + try: data = response.json() except ValueError: @@ -168,6 +180,18 @@ class Connection: """ return SimpleNode(path, self.get_node_raw(path), self) + def replicate(self, path: str, deactivate: bool = False): + """ + Replicate a page to the publish servers + + Args: + path: The page to replicate + deactivate: Deactivate instead of activate + """ + command = 'deactivate' if deactivate else 'activate' + resp = self._session.post(self._wcm_replicate, files={'path': path, 'cmd': command}) + resp.raise_for_status() + def download_binary(self, path: str) -> bytes: """ Download the binary data of a node. (usually jcr:data).