Commit 25dcd0f5 authored by Tristan Cavelier's avatar Tristan Cavelier

Erp5-jIO connector updated

parent 6ac76144
......@@ -56,19 +56,24 @@ from Products.ERP5Type.Log import log\n
\n
context.REQUEST.response.setHeader("Access-Control-Allow-Origin", "*")\n
\n
try: option = json.loads(context.REQUEST.form["option"])\n
jio = context.JIO_class()\n
\n
log(context.REQUEST.form["option"])\n
try: option = jio.jsonUtf8Loads(context.REQUEST.form["option"])\n
except KeyError:\n
option = {}\n
try:\n
mode = str(context.REQUEST.form["mode"])\n
except KeyError:\n
mode = "normal"\n
\n
jio = context.JIO_class(mode=mode)\n
try: mode = str(context.REQUEST.form["mode"])\n
except KeyError: mode = "generic"\n
jio.setMode(mode)\n
\n
metadata_json = jio.getAllDocuments(option)\n
\n
return jio.sendSuccess(metadata_json)\n
\n
document_list = context.portal_catalog(portal_type="Web Page")\n
# document_list = context.portal_catalog(portal_type="Web Page")\n
\n
return jio.sendSuccess(jio.getAllDocsFromDocumentList(document_list, include_docs=option.get("include_docs")))\n
# return jio.sendSuccess(jio.getAllDocsFromDocumentList(document_list, include_docs=option.get("include_docs")))\n
</string> </value>
</item>
<item>
......
......@@ -59,6 +59,7 @@ class FakeDocument():\n
def getObject(self): return self\n
def hasProperty(self, property_id): return False\n
\n
\n
class JioErp5Only():\n
def getDocumentAttachment(self, metadata_json):\n
tool.checkMetadata(metadata_json)\n
......@@ -77,6 +78,7 @@ class JioErp5Only():\n
document_dict["workflow_history"] = [v for v in document.workflow_history]\n
return document_dict\n
\n
\n
class JioGeneric():\n
"Processes generic jIO requests"\n
def __init__(self):\n
......@@ -93,7 +95,9 @@ class JioGeneric():\n
self.allowed_property_id_dict = ["title", "short_title", "description",\n
"language", "reference", "version",\n
"format", "posted_date", "type",\n
"expiration_date",\n
"contributor", "subject", "category"]\n
self.allowed_portal_type_list = ["Web Page", "Image"]\n
self.local_attachment_key = "local_attachment_dict"\n
\n
def getDocumentAttachment(self, metadata_json):\n
......@@ -106,11 +110,11 @@ class JioGeneric():\n
if metadata_json.get("_attachment") == "body" and \\\n
"text_content" in document_dict:\n
return document_dict["text_content"]\n
elif document_dict.get(self.local_attachment_key) is not None and \\\n
metadata_json.get("_attachment") in \\\n
document_dict.get(self.local_attachment_key):\n
return document_dict[self.local_attachment_key][\n
metadata_json["_attachment"]]["data"]\n
# elif document_dict.get(self.local_attachment_key) is not None and \\\n
# metadata_json.get("_attachment") in \\\n
# document_dict.get(self.local_attachment_key):\n
# return document_dict[self.local_attachment_key][\n
# metadata_json["_attachment"]]["data"]\n
raise KeyError("Missing attachment")\n
\n
def getDocumentMetadata(self, metadata_json):\n
......@@ -131,16 +135,16 @@ class JioGeneric():\n
}\n
}\n
)\n
if document_dict.get(self.local_attachment_key) is not None:\n
tmp = {}\n
for k, v in document_dict[self.local_attachment_key].items():\n
tmp[k] = {\n
"length": len(document_dict[self.local_attachment_key][k]["data"]),\n
"content_type": document_dict[self.local_attachment_key][k][\n
"content_type"]\n
}\n
real_document_dict["_attachments"] = tool.dictFusion(\n
real_document_dict.get("_attachments"), tmp);\n
# if document_dict.get(self.local_attachment_key) is not None:\n
# tmp = {}\n
# for k, v in document_dict[self.local_attachment_key].items():\n
# tmp[k] = {\n
# "length": len(document_dict[self.local_attachment_key][k]["data"]),\n
# "content_type": document_dict[self.local_attachment_key][k][\n
# "content_type"]\n
# }\n
# real_document_dict["_attachments"] = tool.dictFusion(\n
# real_document_dict.get("_attachments"), tmp);\n
while True:\n
try: k, v = document_dict.popitem()\n
except KeyError: break\n
......@@ -170,6 +174,7 @@ class JioGeneric():\n
real_document_dict[k] = v\n
real_document_dict["_id"] = metadata_json["_id"]\n
real_document_dict["type"] = document.getPortalType()\n
#tool.stringifyDictDateValue(real_document_dict)\n
return real_document_dict\n
\n
def updateDocumentMetadataEditKw(self, metadata_json,\n
......@@ -200,14 +205,15 @@ class JioGeneric():\n
if metadata_json.get("_attachment") == "body":\n
document.edit(text_content=metadata_json.get("_data"))\n
else:\n
edit_kw = {self.local_attachment_key:\n
document.getProperty(self.local_attachment_key)}\n
if edit_kw.get(self.local_attachment_key) is None:\n
edit_kw[self.local_attachment_key] = {}\n
edit_kw[self.local_attachment_key][metadata_json.get("_attachment")] = {\n
"content_type": metadata_json.get("_mimetype"),\n
"data": metadata_json.get("_data")}\n
document.edit(**edit_kw)\n
raise ValueError("Unauthorized attachment id")\n
# edit_kw = {self.local_attachment_key:\n
# document.getProperty(self.local_attachment_key)}\n
# if edit_kw.get(self.local_attachment_key) is None:\n
# edit_kw[self.local_attachment_key] = {}\n
# edit_kw[self.local_attachment_key][metadata_json.get("_attachment")] = {\n
# "content_type": metadata_json.get("_mimetype"),\n
# "data": metadata_json.get("_data")}\n
# document.edit(**edit_kw)\n
return {"ok": True, "id": metadata_json["_id"],\n
"attachment": metadata_json.get("_attachment")}\n
\n
......@@ -248,15 +254,36 @@ class JioGeneric():\n
except ValueError: raise LookupError("Missing document")\n
except KeyError: raise LookupError("Missing document")\n
document_id = document.getId()\n
document.getParentValue.manage_delObjects(ids=[document_id])\n
return {"ok": True, "id": document_id}\n
document.getParentValue().manage_delObjects(ids=[document_id])\n
return {"ok": True, "id": metadata_json["_id"]}\n
\n
def removeAttachment(self, document, data_dict):\n
document = document.getObject()\n
def removeAttachment(self, metadata_json):\n
tool.checkMetadata(metadata_json)\n
try: document = tool.getDocumentFromUrl(metadata_json["_id"])\n
except AttributeError: raise ValueError("Bad document id")\n
except ValueError: raise LookupError("Missing document")\n
except KeyError: raise LookupError("Missing document")\n
edit_kw = {}\n
if data_dict.get("_attachment") == "body.html":\n
edit_kw[self.mode["attachment_data_key"]] = None\n
document.edit(**edit_kw)\n
if metadata_json.get("_attachment") == "body":\n
if document.getTextContent() == None:\n
raise LookupError("Missing attachment")\n
document.edit(text_content = None)\n
else:\n
raise ValueError("Unauthorized attachment id")\n
return {"ok": True, "id": metadata_json["_id"],\n
"attachment": metadata_json.get("_attachment")}\n
\n
def getAllDocuments(self, option_json):\n
response = {"rows":[]}\n
for portal_type in self.allowed_portal_type_list:\n
for document in context.portal_catalog(portal_type=portal_type):\n
url = tool.getUrlFromDocument(document)\n
row = {"id": url, "key": url, "values": {}}\n
if option_json.get(\'include_docs\') is True:\n
row["doc"] = self.getDocumentMetadata({"_id": url})\n
response["rows"].append(row)\n
response["total_rows"] = len(response["rows"])\n
return response\n
\n
class JioTool():\n
# TODO doc strings\n
......@@ -341,6 +368,8 @@ class JioTool():\n
elif isinstance(obj, dict):\n
return dict(((k.encode("utf-8"), self.recursiveEncodeToUtf8(v)) \\\n
for k, v in obj.iteritems()))\n
else:\n
return obj\n
\n
def jsonUtf8Loads(self, json_str):\n
return self.recursiveEncodeToUtf8(json.loads(json_str))\n
......@@ -392,6 +421,12 @@ class JioTool():\n
\n
def removeDocument(self, metadata_json):\n
return self.jio.removeDocument(metadata_json)\n
\n
def removeAttachment(self, metadata_json):\n
return self.jio.removeAttachment(metadata_json)\n
\n
def getAllDocuments(self, option_json):\n
return self.jio.getAllDocuments(option_json)\n
\n
def sendSuccess(self, param):\n
return json.dumps({"err": None, "response": param})\n
......
......@@ -68,9 +68,7 @@ jio.setMode(mode)\n
\n
try:\n
response_json = jio.putDocumentMetadata(doc, overwrite=False)\n
except ValueError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot post document", str(e)))\n
except TypeError as e:\n
except (ValueError, TypeError) as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot post document", str(e)))\n
except LookupError as e:\n
return jio.sendError(jio.createConflictDict("Cannot post document", str(e)))\n
......
......@@ -68,9 +68,7 @@ jio.setMode(mode)\n
\n
try:\n
response_json = jio.putDocumentMetadata(doc)\n
except ValueError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot put document", str(e)))\n
except TypeError as e:\n
except (ValueError, TypeError) as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot put document", str(e)))\n
\n
return jio.sendSuccess(response_json)\n
......
......@@ -68,10 +68,10 @@ jio.setMode(mode)\n
\n
try:\n
response_json = jio.removeDocument(doc)\n
except ValueError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot remove document", str(e)))\n
except TypeError as e:\n
except (ValueError, TypeError) as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot remove document", str(e)))\n
except LookupError as e:\n
return jio.sendError(jio.createNotFoundDict("Cannot remove document", str(e)))\n
\n
return jio.sendSuccess(response_json)\n
</string> </value>
......
......@@ -58,31 +58,22 @@ context.REQUEST.response.setHeader("Access-Control-Allow-Origin", "*")\n
\n
jio = context.JIO_class()\n
\n
try:\n
doc = json.loads(context.REQUEST.form["doc"])\n
except IndexError:\n
return jio.sendError(jio.createBadRequestErrorObject("Unable to operate put request", "No document received"))\n
try:\n
mode = str(context.REQUEST.form["mode"])\n
try: doc = jio.jsonUtf8Loads(context.REQUEST.form["doc"])\n
except KeyError:\n
mode = "normal"\n
return jio.sendError(jio.createBadRequestDict("Cannot get document", "No document information received"))\n
\n
try: mode = str(context.REQUEST.form["mode"])\n
except KeyError: mode = "generic"\n
jio.setMode(mode)\n
\n
doc = jio.recursiveEncodeToUtf8(doc)\n
\n
if not isinstance(doc.get("_id"), str) or doc.get("_id") == "":\n
return jio.sendError(jio.createBadRequestErrorObject("Unable to operate put request", "Document id is missing"))\n
\n
document_list = jio.getDocumentListFromId(doc.get("_id"))\n
\n
if len(document_list) == 0:\n
return jio.sendError(jio.createNotFoundErrorObject("Unable to find \\"" + doc.get("_id") + "\\"", "Document not found"))\n
else:\n
# TODO manage several documents\n
jio.removeAttachment(document_list[0], doc)\n
try:\n
response_json = jio.removeAttachment(doc)\n
except (ValueError, TypeError) as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot remove attachment", str(e)))\n
except LookupError as e:\n
return jio.sendError(jio.createNotFoundDict("Cannot remove attachment", str(e)))\n
\n
return jio.sendSuccess({"ok": True, "id": doc["_id"], "attachment": doc["_attachment"]})\n
return jio.sendSuccess(response_json)\n
</string> </value>
</item>
<item>
......
3
\ No newline at end of file
4
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment