Commit c9cc425e authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

erp5_jio: more hardcoded code to handle task reports + code to handle query

parent 770c5009
...@@ -51,24 +51,28 @@ ...@@ -51,24 +51,28 @@
<item> <item>
<key> <string>_body</string> </key> <key> <string>_body</string> </key>
<value> <string>import json\n <value> <string>import json\n
from Products.ERP5Type.Log import log\n #from Products.ERP5Type.Log import log\n
# use JSON.parse as json.loads and JSON.stringify as json.dumps\n # use JSON.parse as json.loads and JSON.stringify as json.dumps\n
\n \n
context.REQUEST.response.setHeader("Access-Control-Allow-Origin", "*")\n context.REQUEST.response.setHeader("Access-Control-Allow-Origin", "*")\n
\n \n
try: option = json.loads(context.REQUEST.form["option"])\n jio = context.JIO_class()\n
\n
try: option = jio.jsonUtf8Loads(context.REQUEST.form["option"])\n
except KeyError:\n except KeyError:\n
option = {}\n option = {}\n
try:\n
mode = str(context.REQUEST.form["mode"])\n
except KeyError:\n
mode = "normal"\n
\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 \n
document_list = context.portal_catalog(portal_type="Web Page")\n # document_list = context.portal_catalog(portal_type="Web Page")\n
\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> </string> </value>
</item> </item>
<item> <item>
......
...@@ -52,13 +52,114 @@ ...@@ -52,13 +52,114 @@
<key> <string>_body</string> </key> <key> <string>_body</string> </key>
<value> <string encoding="cdata"><![CDATA[ <value> <string encoding="cdata"><![CDATA[
import json, hashlib\n import json\n
#from Products.ERP5Type.Log import log\n from DateTime import DateTime\n
from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, ComplexQuery\n
from Products.ERP5Type.Log import log\n
portal = context.getPortalObject()\n
\n
def Task_setProjectTitle(document, project_title):\n
project_list = portal.portal_catalog(portal_type="Project", title=project_title)\n
project_url = None\n
if project_title:\n
if len(project_list):\n
project = project_list[0].getObject()\n
else:\n
project = portal.project_module.newContent(title=project_title)\n
project_url = project.getRelativeUrl()\n
#portal.person_module.log("Task_setProjectTitle, project", project_url)\n
document.setSourceProject(project_url)\n
\n
def convertTaskReportStateToJioState(state):\n
# portal.person_module.log("state =========>", state)\n
map = {\n
"comfirmed": "Comfirmed",\n
"started": "Started",\n
"stopped": "Completed",\n
"draft": "Draft"\n
};\n
return state if map.get(state) is None else map[state]\n
\n
def changeTaskReportState(document, state):\n
state = state.lower()\n
current_state = document.getSimulationState()\n
if state == "comfirmed":\n
if current_state == "draft":\n
document.confirm()\n
return\n
if state == "started":\n
if current_state in ["draft", "confirmed"]:\n
document.start()\n
if current_state == "stopped":\n
document.restart()\n
return\n
if state == "completed":\n
if current_state in ["draft", "confirmed", "started"]:\n
document.stop()\n
return\n
\n
def camelCaseToUnderscores(string):\n
result = ""\n
tmp = ""\n
for char in string:\n
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":\n
tmp += char\n
elif tmp != "":\n
tmp = tmp[:-1].lower() + "_" + tmp[-1].lower()\n
if tmp[0] != "_":\n
tmp = "_" + tmp\n
result += tmp + char\n
tmp = ""\n
else:\n
result += char\n
if result[0] == \'_\' and string[0] != \'_\': return result[1:]\n
return result\n
\n
def underscores_to_camel_case(string):\n
return reduce(lambda v, s: v + s.title(), string.split("_"))\n
\n
def dictGetKeyFromValue(obj, value, *args):\n
for k, v in obj.items():\n
if v == value:\n
return k\n
if len(args) == 0:\n
raise ValueError(\'value not found\')\n
return args[0]\n
\n
def UTF8DeepJsonEncoder(obj):\n
# string -> unicode -> str\n
if isinstance(obj, unicode):\n
return obj.encode("UTF-8")\n
# array -> list\n
if isinstance(obj, list):\n
for i in xrange(len(obj)):\n
obj[i] = UTF8DeepJsonEncoder(obj[i])\n
return obj\n
# object -> dict\n
if isinstance(obj, dict):\n
for k, v in obj.items():\n
v = UTF8DeepJsonEncoder(v)\n
del obj[k]\n
obj[UTF8DeepJsonEncoder(k)] = v\n
return obj\n
# number (int) -> int, long\n
# true -> True\n
# false -> False\n
# null -> None\n
return obj\n
\n
def json_loads(string):\n
return UTF8DeepJsonEncoder(json.loads(string))\n
\n
def jsonDeepCopy(json_dict):\n
"Clones the JSON object in deep and returns the clone"\n
return json_loads(json.dumps(json_dict))\n
\n \n
class FakeDocument():\n class FakeDocument():\n
def getObject(self): return self\n def getObject(self): return self\n
def hasProperty(self, property_id): return False\n def hasProperty(self, property_id): return False\n
\n \n
\n
class JioErp5Only():\n class JioErp5Only():\n
def getDocumentAttachment(self, metadata_json):\n def getDocumentAttachment(self, metadata_json):\n
tool.checkMetadata(metadata_json)\n tool.checkMetadata(metadata_json)\n
...@@ -77,40 +178,52 @@ class JioErp5Only():\n ...@@ -77,40 +178,52 @@ class JioErp5Only():\n
document_dict["workflow_history"] = [v for v in document.workflow_history]\n document_dict["workflow_history"] = [v for v in document.workflow_history]\n
return document_dict\n return document_dict\n
\n \n
\n
class JioGeneric():\n class JioGeneric():\n
"Processes generic jIO requests"\n "Processes generic jIO requests"\n
def __init__(self):\n def __init__(self):\n
self.simple_conversion_dict = {\n self.simple_conversion_dict = {\n
"content_type": "format",\n "content_type": "format",\n
"effective_date": "posted_date",\n
"portal_type": "type",\n "portal_type": "type",\n
"contributor_list": "contributor",\n "contributor_list": "contributor",\n
"subject_list": "subject",\n "subject_list": "subject",\n
"categories_list": "category"\n "categories_list": "category",\n
"creation_date": "created",\n
"modification_date": "modified",\n
"start_date": "start",\n
"stop_date": "stop"\n
}\n }\n
# order deny, allow\n # order deny, allow\n
# deny from all\n # deny from all\n
self.allowed_property_id_dict = ["title", "short_title", "description",\n self.allowed_property_id_list = ["title", "short_title", "description",\n
"language", "reference", "version",\n "language", "reference", "version", "project",\n
"format", "posted_date", "type",\n "format", "type", "start", "stop", "state",\n
"effective_date", "expiration_date",\n
"contributor", "subject", "category"]\n "contributor", "subject", "category"]\n
self.local_attachment_key = "local_attachment_dict"\n self.type_attachment_key = {\n
"Web Page": "text_content",\n
"Image": "data"\n
}\n
self.allowed_portal_type_list = ["Task Report"]\n
# self.local_attachment_key = "local_attachment_dict"\n
\n \n
def getDocumentAttachment(self, metadata_json):\n def getDocumentAttachment(self, metadata_json):\n
tool.checkMetadata(metadata_json)\n tool.checkMetadata(metadata_json)\n
try: document = tool.getDocumentFromUrl(metadata_json.get("_id"))\n try: document = tool.getDocumentFromUrl(metadata_json.get("_id"))\n
except AttributeError: raise ValueError("Bad document id")\n except AttributeError: raise ValueError("Bad document id")\n
except ValueError: raise LookupError("Missing document")\n except (ValueError, KeyError): raise LookupError("Missing document")\n
except KeyError: raise LookupError("Missing document")\n
document_dict = tool.getDocumentProperties(document)\n document_dict = tool.getDocumentProperties(document)\n
attachment_key = self.type_attachment_key.get(document.getPortalType())\n
if metadata_json.get("_attachment") == "body" and \\\n if metadata_json.get("_attachment") == "body" and \\\n
"text_content" in document_dict:\n attachment_key in document_dict:\n
return document_dict["text_content"]\n data = document_dict[attachment_key]\n
elif document_dict.get(self.local_attachment_key) is not None and \\\n if data is not None:\n
metadata_json.get("_attachment") in \\\n return data\n
document_dict.get(self.local_attachment_key):\n # elif document_dict.get(self.local_attachment_key) is not None and \\\n
return document_dict[self.local_attachment_key][\n # metadata_json.get("_attachment") in \\\n
metadata_json["_attachment"]]["data"]\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 raise KeyError("Missing attachment")\n
\n \n
def getDocumentMetadata(self, metadata_json):\n def getDocumentMetadata(self, metadata_json):\n
...@@ -122,25 +235,27 @@ class JioGeneric():\n ...@@ -122,25 +235,27 @@ class JioGeneric():\n
document_dict = tool.getDocumentProperties(document)\n document_dict = tool.getDocumentProperties(document)\n
real_document_dict = {}\n real_document_dict = {}\n
# get attachments metas\n # get attachments metas\n
if document_dict.get("text_content") is not None:\n attachment_key = self.type_attachment_key.get(document.getPortalType())\n
if attachment_key is not None and \\\n
document_dict.get(attachment_key) is not None:\n
real_document_dict["_attachments"] = tool.dictFusion(\n real_document_dict["_attachments"] = tool.dictFusion(\n
real_document_dict.get("_attachments"), {\n real_document_dict.get("_attachments"), {\n
"body": {\n "body": {\n
"length": len(document_dict["text_content"]),\n "length": len(document_dict[attachment_key]),\n
"content_type": document_dict.get("content_type")\n "content_type": document_dict.get("content_type")\n
}\n }\n
}\n }\n
)\n )\n
if document_dict.get(self.local_attachment_key) is not None:\n # if document_dict.get(self.local_attachment_key) is not None:\n
tmp = {}\n # tmp = {}\n
for k, v in document_dict[self.local_attachment_key].items():\n # for k, v in document_dict[self.local_attachment_key].items():\n
tmp[k] = {\n # tmp[k] = {\n
"length": len(document_dict[self.local_attachment_key][k]["data"]),\n # "length": len(document_dict[self.local_attachment_key][k]["data"]),\n
"content_type": document_dict[self.local_attachment_key][k][\n # "content_type": document_dict[self.local_attachment_key][k][\n
"content_type"]\n # "content_type"]\n
}\n # }\n
real_document_dict["_attachments"] = tool.dictFusion(\n # real_document_dict["_attachments"] = tool.dictFusion(\n
real_document_dict.get("_attachments"), tmp);\n # real_document_dict.get("_attachments"), tmp);\n
while True:\n while True:\n
try: k, v = document_dict.popitem()\n try: k, v = document_dict.popitem()\n
except KeyError: break\n except KeyError: break\n
...@@ -149,11 +264,10 @@ class JioGeneric():\n ...@@ -149,11 +264,10 @@ class JioGeneric():\n
continue\n continue\n
if k in self.simple_conversion_dict:\n if k in self.simple_conversion_dict:\n
k = self.simple_conversion_dict.get(k)\n k = self.simple_conversion_dict.get(k)\n
if k in self.allowed_property_id_dict:\n if k in self.allowed_property_id_list:\n
try: v.time\n if isinstance(v, DateTime):\n
except AttributeError: pass\n v = v.ISO8601()\n
else: v = str(v)\n if k == attachment_key:\n
if k == "text_content":\n
real_document_dict["_attachments"] = {\n real_document_dict["_attachments"] = {\n
"body": {\n "body": {\n
"length": len(v)\n "length": len(v)\n
...@@ -169,7 +283,16 @@ class JioGeneric():\n ...@@ -169,7 +283,16 @@ class JioGeneric():\n
real_document_dict["relation"] = relation_list\n real_document_dict["relation"] = relation_list\n
real_document_dict[k] = v\n real_document_dict[k] = v\n
real_document_dict["_id"] = metadata_json["_id"]\n real_document_dict["_id"] = metadata_json["_id"]\n
real_document_dict["date"] = document.getCreationDate().ISO8601()\n
real_document_dict["created"] = document.getCreationDate().ISO8601()\n
real_document_dict["modified"] = document.getModificationDate().ISO8601()\n
real_document_dict["type"] = document.getPortalType()\n real_document_dict["type"] = document.getPortalType()\n
# HARD CODE for task report documents\n
if document.getPortalType() == "Task Report":\n
real_document_dict["state"] = convertTaskReportStateToJioState(document.getSimulationState())\n
real_document_dict["project"] = document.getSourceProjectTitle()\n
if real_document_dict["project"] == None: del real_document_dict["project"]\n
#tool.stringifyDictDateValue(real_document_dict)\n
return real_document_dict\n return real_document_dict\n
\n \n
def updateDocumentMetadataEditKw(self, metadata_json,\n def updateDocumentMetadataEditKw(self, metadata_json,\n
...@@ -183,7 +306,7 @@ class JioGeneric():\n ...@@ -183,7 +306,7 @@ class JioGeneric():\n
if jio_key == meta_key:\n if jio_key == meta_key:\n
doc_key = erp5_key\n doc_key = erp5_key\n
break\n break\n
if meta_key in self.allowed_property_id_dict:\n if meta_key in self.allowed_property_id_list:\n
if meta_value is None and document.hasProperty(doc_key) or \\\n if meta_value is None and document.hasProperty(doc_key) or \\\n
meta_value is not None:\n meta_value is not None:\n
edit_kw[doc_key] = meta_value\n edit_kw[doc_key] = meta_value\n
...@@ -197,21 +320,25 @@ class JioGeneric():\n ...@@ -197,21 +320,25 @@ class JioGeneric():\n
def putDocumentAttachment(self, metadata_json):\n def putDocumentAttachment(self, metadata_json):\n
tool.checkMetadata(metadata_json)\n tool.checkMetadata(metadata_json)\n
document = tool.getDocumentFromUrl(metadata_json["_id"]).getObject()\n document = tool.getDocumentFromUrl(metadata_json["_id"]).getObject()\n
attachment_key = self.type_attachment_key.get(document.getPortalType())\n
if metadata_json.get("_attachment") == "body":\n if metadata_json.get("_attachment") == "body":\n
document.edit(text_content=metadata_json.get("_data"))\n edit_kw = {attachment_key: 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 document.edit(**edit_kw)\n
return {"ok": True, "id": metadata_json["_id"],\n else:\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 {"id": metadata_json["_id"],\n
"attachment": metadata_json.get("_attachment")}\n "attachment": metadata_json.get("_attachment")}\n
\n \n
def putDocumentMetadata(self, metadata_json, overwrite=True):\n def putDocumentMetadata(self, metadata_json, overwrite=True):\n
hard_code_json = metadata_json.copy();\n
doc_id = metadata_json.get("_id")\n doc_id = metadata_json.get("_id")\n
document = None\n document = None\n
try: document = tool.getDocumentFromUrl(doc_id)\n try: document = tool.getDocumentFromUrl(doc_id)\n
...@@ -225,10 +352,13 @@ class JioGeneric():\n ...@@ -225,10 +352,13 @@ class JioGeneric():\n
document.getObject().edit(\n document.getObject().edit(\n
**self.updateDocumentMetadataEditKw(metadata_json, document=document))\n **self.updateDocumentMetadataEditKw(metadata_json, document=document))\n
else:\n else:\n
# document does not exist\n
if "_id" in metadata_json:\n if "_id" in metadata_json:\n
tool.newDocumentFromUrl(\n try:\n
metadata_json["_id"],\n tool.newDocumentFromUrl(\n
self.updateDocumentMetadataEditKw(metadata_json))\n metadata_json["_id"],\n
self.updateDocumentMetadataEditKw(metadata_json))\n
except KeyError: raise KeyError("Bad document id")\n
elif "type" in metadata_json:\n elif "type" in metadata_json:\n
try:\n try:\n
document = tool.newDocumentFromType(\n document = tool.newDocumentFromType(\n
...@@ -239,7 +369,14 @@ class JioGeneric():\n ...@@ -239,7 +369,14 @@ class JioGeneric():\n
raise ValueError("Bad type")\n raise ValueError("Bad type")\n
else:\n else:\n
raise TypeError("Type missing")\n raise TypeError("Type missing")\n
return {"ok": True, "id": doc_id}\n # HARD CODE for task report documents\n
#portal.person_module.log("document type ------->", hard_code_json)\n
if hard_code_json.get("type") == "Task Report":\n
if isinstance(hard_code_json.get("state"), str):\n
changeTaskReportState(document, hard_code_json["state"])\n
#portal.person_module.log("document type Task Report, metadata project", hard_code_json.get("project"))\n
Task_setProjectTitle(document, hard_code_json.get("project"))\n
return {"id": doc_id}\n
\n \n
def removeDocument(self, metadata_json):\n def removeDocument(self, metadata_json):\n
tool.checkMetadata(metadata_json)\n tool.checkMetadata(metadata_json)\n
...@@ -248,18 +385,170 @@ class JioGeneric():\n ...@@ -248,18 +385,170 @@ class JioGeneric():\n
except ValueError: raise LookupError("Missing document")\n except ValueError: raise LookupError("Missing document")\n
except KeyError: raise LookupError("Missing document")\n except KeyError: raise LookupError("Missing document")\n
document_id = document.getId()\n document_id = document.getId()\n
document.getParentValue.manage_delObjects(ids=[document_id])\n document.getParentValue().manage_delObjects(ids=[document_id])\n
return {"ok": True, "id": document_id}\n return {"id": metadata_json["_id"]}\n
\n \n
def removeAttachment(self, document, data_dict):\n def removeAttachment(self, metadata_json):\n
document = document.getObject()\n tool.checkMetadata(metadata_json)\n
edit_kw = {}\n try: document = tool.getDocumentFromUrl(metadata_json["_id"])\n
if data_dict.get("_attachment") == "body.html":\n except AttributeError: raise ValueError("Bad document id")\n
edit_kw[self.mode["attachment_data_key"]] = None\n except ValueError: raise LookupError("Missing document")\n
document.edit(**edit_kw)\n except KeyError: raise LookupError("Missing document")\n
attachment_key = self.type_attachment_key.get(document.getPortalType())\n
if metadata_json.get("_attachment") == "body":\n
if document.getTextContent() == None:\n
raise LookupError("Missing attachment")\n
edit_kw = {attachment_key: None}\n
document.edit(**edit_kw)\n
else:\n
raise ValueError("Unauthorized attachment id")\n
return {"id": metadata_json["_id"],\n
"attachment": metadata_json.get("_attachment")}\n
\n
def parseQuery(self, query_dict):\n
def rec(query_dict):\n
if query_dict.get("type") == "simple":\n
# if query_dict.get("key") not in self.allowed_property_id_list:\n
# return None\n
for erp5_key, jio_key in self.simple_conversion_dict.items():\n
if query_dict["key"] == jio_key:\n
query_dict["key"] = erp5_key\n
break\n
return SimpleQuery(comparison_operator=query_dict[\'operator\'],\n
**{query_dict[\'key\']: query_dict[\'value\']})\n
if query_dict.get("type") == "complex":\n
tool.listMapReplace(rec, query_dict[\'query_list\'])\n
try:\n
while True: query_dict[\'query_list\'].remove(None)\n
except ValueError: pass\n
return ComplexQuery(operator=query_dict[\'operator\'],\n
*query_dict[\'query_list\'])\n
return None\n
return rec(query_dict)\n
\n
def getAllDocuments(self, option_json):\n
response = {"rows":[]}\n
kw = {}\n
if isinstance(option_json.get(\'query\'), dict):\n
kw[\'query\'] = self.parseQuery(option_json["query"])\n
kw[\'query\'] = ComplexQuery(\n
kw[\'query\'],\n
ComplexQuery(\n
operator=\'or\',\n
*[SimpleQuery(comparison_operator="=", portal_type=x) \\\n
for x in self.allowed_portal_type_list]\n
),\n
comparison_operator=\'and\'\n
)\n
else:\n
kw[\'query\'] = ComplexQuery(\n
operator=\'or\',\n
*[SimpleQuery(comparison_operator="=", portal_type=x) \\\n
for x in self.allowed_portal_type_list]\n
)\n
\n
if isinstance(option_json.get(\'limit\'), list):\n
kw[\'limit\'] = tuple(option_json[\'limit\'])\n
\n
c = self.simple_conversion_dict\n
if isinstance(option_json.get(\'sort_on\'), list):\n
for i in range(len(option_json[\'sort_on\'])):\n
s = option_json[\'sort_on\'][i]\n
option_json[\'sort_on\'][i] = dictGetKeyFromValue(c, s[0], s[0])\n
kw[\'sort_on\'] = option_json[\'sort_on\']\n
\n
if not isinstance(option_json.get(\'select_list\'), list):\n
option_json[\'select_list\'] = []\n
if option_json[\'select_list\'] != []:\n
id_list = context.portal_catalog.getSQLCatalog().getColumnIds()\n
i = len(option_json[\'select_list\']) - 1\n
while i >= 0:\n
s = option_json[\'select_list\'][i]\n
option_json[\'select_list\'][i] = dictGetKeyFromValue(c, s, s)\n
if option_json[\'select_list\'][i] not in id_list:\n
option_json[\'select_list\'].pop(i)\n
i -= 1\n
kw[\'select_list\'] = option_json[\'select_list\']\n
#portal.person_module.log("catalog ----------===============>", kw);\n
for document in context.portal_catalog(**kw):\n
url = tool.getUrlFromDocument(document)\n
row = {"id": url, "key": url, "value": {}}\n
for erp5_meta in option_json[\'select_list\']:\n
jio_meta = c.get(erp5_meta, erp5_meta)\n
row[\'value\'][jio_meta] = getattr(document, erp5_meta, None)\n
if isinstance(row[\'value\'][jio_meta], DateTime):\n
row[\'value\'][jio_meta] = row[\'value\'][jio_meta].ISO8601()\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
# def getAllDocuments(self, option_json):\n
# response = {"rows":[]}\n
# editkw = {}\n
# if "query" in option_json:\n
# editkw[\'query\'] = self.parseQuery(option_json["query"])\n
# editkw[\'query\'] = ComplexQuery(\n
# editkw[\'query\'],\n
# ComplexQuery(\n
# operator=\'or\',\n
# *[SimpleQuery(comparison_operator="=", portal_type=x) \\\n
# for x in self.allowed_portal_type_list]\n
# ),\n
# comparison_operator=\'and\'\n
# )\n
# # if isinstance(option_json.get(\'limit\'), list):\n
# # editkw[\'limit\'] = tuple(option_json[\'limit\'])\n
\n
# # if isinstance(option_json.get(\'select_list\'), list):\n
# # for sub_list in option_json[\'select_list\']:\n
# # sub_list = tuple(sub_list)\n
# # editkw[\'select_list\'] = option_json[\'select_list\']\n
\n
# for document in context.portal_catalog(query=query):\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
# else:\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
# 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 \n
class JioTool():\n class JioTool():\n
# TODO doc strings\n # TODO doc strings\n
\n
def listMapReplace(self, function, li):\n
"""listMapReplace(function, list)\n
\n
li = [1, 2, 3]\n
listFilter(lambda x: x + 1, li)\n
print(li) -> [2, 3, 4]\n
\n
"""\n
for i in range(len(li)):\n
li[i] = function(li[i])\n
\n
def createBadRequestDict(self, message, reason):\n def createBadRequestDict(self, message, reason):\n
return {\n return {\n
"status": 405,\n "status": 405,\n
...@@ -268,6 +557,15 @@ class JioTool():\n ...@@ -268,6 +557,15 @@ class JioTool():\n
"message": message,\n "message": message,\n
"reason": reason\n "reason": reason\n
}\n }\n
\n
def createForbiddenDict(self, message, reason):\n
return {\n
"status": 403,\n
"statusText": "Forbidden",\n
"error": "forbidden",\n
"message": message,\n
"reason": reason\n
}\n
\n \n
def createNotFoundDict(self, message, reason):\n def createNotFoundDict(self, message, reason):\n
return {\n return {\n
...@@ -286,10 +584,6 @@ class JioTool():\n ...@@ -286,10 +584,6 @@ class JioTool():\n
"message": message,\n "message": message,\n
"reason": reason\n "reason": reason\n
}\n }\n
\n
def jsonDeepCopy(self, json_dict):\n
"Clones the JSON object in deep and returns the clone"\n
return json.loads(json.dumps(json_dict))\n
\n \n
def checkMetadata(self, metadata_json):\n def checkMetadata(self, metadata_json):\n
"Check if the id of the metadata is good"\n "Check if the id of the metadata is good"\n
...@@ -330,26 +624,14 @@ class JioTool():\n ...@@ -330,26 +624,14 @@ class JioTool():\n
property_id = property_definition["id"]\n property_id = property_definition["id"]\n
document_dict[property_id] = document.getProperty(property_id)\n document_dict[property_id] = document.getProperty(property_id)\n
return document_dict\n return document_dict\n
\n
def recursiveEncodeToUtf8(self, obj):\n
if isinstance(obj, (str, unicode)):\n
return obj.encode("utf-8")\n
elif isinstance(obj, list):\n
return [self.recursiveEncodeToUtf8(x) for x in obj]\n
elif isinstance(obj, tuple):\n
return tuple((self.recursiveEncodeToUtf8(x) for x in obj))\n
elif isinstance(obj, dict):\n
return dict(((k.encode("utf-8"), self.recursiveEncodeToUtf8(v)) \\\n
for k, v in obj.iteritems()))\n
\n \n
def jsonUtf8Loads(self, json_str):\n def jsonUtf8Loads(self, json_str):\n
return self.recursiveEncodeToUtf8(json.loads(json_str))\n return json_loads(json_str)\n
\n \n
def stringifyDictDateValue(self, obj_dict):\n def stringifyDictDateValue(self, obj_dict):\n
for k, v in obj_dict.items():\n for k, v in obj_dict.items():\n
try: v.time\n if isinstance(v, DateTime):\n
except AttributeError: pass\n obj_dict[k] = v.ISO8601()\n
else: obj_dict[k] = str(v)\n
\n \n
def formatMetadataToPut(self, metadata_json):\n def formatMetadataToPut(self, metadata_json):\n
for k, v in metadata_json.iteritems():\n for k, v in metadata_json.iteritems():\n
...@@ -381,17 +663,25 @@ class JioTool():\n ...@@ -381,17 +663,25 @@ class JioTool():\n
def getDocumentAttachment(self, metadata_json):\n def getDocumentAttachment(self, metadata_json):\n
return self.jio.getDocumentAttachment(metadata_json)\n return self.jio.getDocumentAttachment(metadata_json)\n
\n \n
def putDocumentMetadata(self, metadata_json, overwrite=True):\n def putDocumentMetadata(self, metadata_json, overwrite=True, need_id=False):\n
return self.jio.putDocumentMetadata(\n metadata = self.formatMetadataToPut(\n
self.formatMetadataToPut(\n jsonDeepCopy(metadata_json))\n
self.recursiveEncodeToUtf8(\n if need_id:\n
self.jsonDeepCopy(metadata_json))), overwrite=overwrite)\n if not isinstance(metadata.get("_id"), str) or metadata.get("_id") == "":\n
raise ValueError("Document id needed")\n
return self.jio.putDocumentMetadata(metadata, overwrite=overwrite)\n
\n \n
def putDocumentAttachment(self, attachment_json):\n def putDocumentAttachment(self, attachment_json):\n
return self.jio.putDocumentAttachment(attachment_json)\n return self.jio.putDocumentAttachment(attachment_json)\n
\n \n
def removeDocument(self, metadata_json):\n def removeDocument(self, metadata_json):\n
return self.jio.removeDocument(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 \n
def sendSuccess(self, param):\n def sendSuccess(self, param):\n
return json.dumps({"err": None, "response": param})\n return json.dumps({"err": None, "response": param})\n
......
...@@ -75,7 +75,7 @@ jio.setMode(mode)\n ...@@ -75,7 +75,7 @@ jio.setMode(mode)\n
try:\n try:\n
metadata_json = jio.getDocumentMetadata(doc)\n metadata_json = jio.getDocumentMetadata(doc)\n
except ValueError as e:\n except ValueError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot get document", str(e)))\n return jio.sendError(jio.createConflictDict("Cannot get document", str(e)))\n
except LookupError as e:\n except LookupError as e:\n
return jio.sendError(jio.createNotFoundDict("Cannot get document", str(e)))\n return jio.sendError(jio.createNotFoundDict("Cannot get document", str(e)))\n
\n \n
......
...@@ -75,7 +75,7 @@ jio.setMode(mode)\n ...@@ -75,7 +75,7 @@ jio.setMode(mode)\n
try:\n try:\n
attachment_data = jio.getDocumentAttachment(doc)\n attachment_data = jio.getDocumentAttachment(doc)\n
except ValueError as e:\n except ValueError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot get attachment", str(e)))\n return jio.sendError(jio.createConflictDict("Cannot get attachment", str(e)))\n
except LookupError as e:\n except LookupError as e:\n
return jio.sendError(jio.createNotFoundDict("Cannot get attachment", str(e)))\n return jio.sendError(jio.createNotFoundDict("Cannot get attachment", str(e)))\n
\n \n
......
...@@ -60,7 +60,8 @@ jio = context.JIO_class()\n ...@@ -60,7 +60,8 @@ jio = context.JIO_class()\n
\n \n
try: doc = jio.jsonUtf8Loads(context.REQUEST.form["doc"])\n try: doc = jio.jsonUtf8Loads(context.REQUEST.form["doc"])\n
except KeyError:\n except KeyError:\n
return jio.sendError(jio.createBadRequestDict("Cannot get document", "No document information received"))\n return jio.sendError(jio.createBadRequestDict(\n
"Cannot get document", "No document information received"))\n
\n \n
try: mode = str(context.REQUEST.form["mode"])\n try: mode = str(context.REQUEST.form["mode"])\n
except KeyError: mode = "generic"\n except KeyError: mode = "generic"\n
...@@ -68,12 +69,12 @@ jio.setMode(mode)\n ...@@ -68,12 +69,12 @@ jio.setMode(mode)\n
\n \n
try:\n try:\n
response_json = jio.putDocumentMetadata(doc, overwrite=False)\n response_json = jio.putDocumentMetadata(doc, overwrite=False)\n
except ValueError as e:\n except (ValueError, TypeError, LookupError) as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot post document", str(e)))\n
except 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 return jio.sendError(jio.createConflictDict("Cannot post document", str(e)))\n
# except KeyError as e:\n
# return jio.sendError(jio.createForbiddenDict("Cannot post document", str(e)))\n
# except LookupError as e:\n
# return jio.sendError(jio.createConflictDict("Cannot post document", str(e)))\n
\n \n
return jio.sendSuccess(response_json)\n return jio.sendSuccess(response_json)\n
</string> </value> </string> </value>
......
...@@ -67,11 +67,11 @@ except KeyError: mode = "generic"\n ...@@ -67,11 +67,11 @@ except KeyError: mode = "generic"\n
jio.setMode(mode)\n jio.setMode(mode)\n
\n \n
try:\n try:\n
response_json = jio.putDocumentMetadata(doc)\n response_json = jio.putDocumentMetadata(doc, need_id=True)\n
except ValueError as e:\n except (ValueError, TypeError, KeyError) as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot put document", str(e)))\n return jio.sendError(jio.createConflictDict("Cannot put document", str(e)))\n
except TypeError as e:\n # except KeyError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot put document", str(e)))\n # return jio.sendError(jio.createForbiddenDict("Cannot put document", str(e)))\n
\n \n
return jio.sendSuccess(response_json)\n return jio.sendSuccess(response_json)\n
</string> </value> </string> </value>
......
...@@ -68,10 +68,10 @@ jio.setMode(mode)\n ...@@ -68,10 +68,10 @@ jio.setMode(mode)\n
\n \n
try:\n try:\n
response_json = jio.putDocumentAttachment(doc)\n response_json = jio.putDocumentAttachment(doc)\n
except ValueError as e:\n except (ValueError, TypeError, KeyError) as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot put attachment", str(e)))\n return jio.sendError(jio.createConflictDict("Cannot put attachment", str(e)))\n
except TypeError as e:\n # except TypeError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot put attachment", str(e)))\n # return jio.sendError(jio.createBadRequestDict("Cannot put attachment", str(e)))\n
\n \n
return jio.sendSuccess(response_json)\n return jio.sendSuccess(response_json)\n
</string> </value> </string> </value>
......
...@@ -68,10 +68,10 @@ jio.setMode(mode)\n ...@@ -68,10 +68,10 @@ jio.setMode(mode)\n
\n \n
try:\n try:\n
response_json = jio.removeDocument(doc)\n response_json = jio.removeDocument(doc)\n
except ValueError as e:\n except (ValueError, TypeError) as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot remove document", str(e)))\n
except TypeError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot remove document", str(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 \n
return jio.sendSuccess(response_json)\n return jio.sendSuccess(response_json)\n
</string> </value> </string> </value>
......
...@@ -58,31 +58,22 @@ context.REQUEST.response.setHeader("Access-Control-Allow-Origin", "*")\n ...@@ -58,31 +58,22 @@ context.REQUEST.response.setHeader("Access-Control-Allow-Origin", "*")\n
\n \n
jio = context.JIO_class()\n jio = context.JIO_class()\n
\n \n
try:\n try: doc = jio.jsonUtf8Loads(context.REQUEST.form["doc"])\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
except KeyError:\n except KeyError:\n
mode = "normal"\n return jio.sendError(jio.createBadRequestDict("Cannot get document", "No document information received"))\n
\n \n
try: mode = str(context.REQUEST.form["mode"])\n
except KeyError: mode = "generic"\n
jio.setMode(mode)\n jio.setMode(mode)\n
\n \n
doc = jio.recursiveEncodeToUtf8(doc)\n try:\n
\n response_json = jio.removeAttachment(doc)\n
if not isinstance(doc.get("_id"), str) or doc.get("_id") == "":\n except (ValueError, TypeError) as e:\n
return jio.sendError(jio.createBadRequestErrorObject("Unable to operate put request", "Document id is missing"))\n return jio.sendError(jio.createConflictDict("Cannot remove attachment", str(e)))\n
\n except LookupError as e:\n
document_list = jio.getDocumentListFromId(doc.get("_id"))\n return jio.sendError(jio.createNotFoundDict("Cannot remove attachment", str(e)))\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
\n \n
return jio.sendSuccess({"ok": True, "id": doc["_id"], "attachment": doc["_attachment"]})\n return jio.sendSuccess(response_json)\n
</string> </value> </string> </value>
</item> </item>
<item> <item>
......
2 3
\ No newline at end of file \ 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