Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bryan Kaperick
erp5
Commits
c9cc425e
Commit
c9cc425e
authored
Nov 08, 2013
by
Tristan Cavelier
Committed by
Sebastien Robin
Nov 08, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
erp5_jio: more hardcoded code to handle task reports + code to handle query
parent
770c5009
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
417 additions
and
131 deletions
+417
-131
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_allDocs.xml
...io/SkinTemplateItem/portal_skins/erp5_jio/JIO_allDocs.xml
+13
-9
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_class.xml
..._jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_class.xml
+371
-81
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_get.xml
...p5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_get.xml
+1
-1
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_getAttachment.xml
...nTemplateItem/portal_skins/erp5_jio/JIO_getAttachment.xml
+1
-1
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_post.xml
...5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_post.xml
+7
-6
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_put.xml
...p5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_put.xml
+5
-5
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_putAttachment.xml
...nTemplateItem/portal_skins/erp5_jio/JIO_putAttachment.xml
+4
-4
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_remove.xml
...jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_remove.xml
+3
-3
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_removeAttachment.xml
...mplateItem/portal_skins/erp5_jio/JIO_removeAttachment.xml
+11
-20
bt5/erp5_jio/bt/revision
bt5/erp5_jio/bt/revision
+1
-1
No files found.
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_allDocs.xml
View file @
c9cc425e
...
...
@@ -51,24 +51,28 @@
<item>
<key>
<string>
_body
</string>
</key>
<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
\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
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>
...
...
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_class.xml
View file @
c9cc425e
...
...
@@ -52,13 +52,114 @@
<key>
<string>
_body
</string>
</key>
<value>
<string
encoding=
"cdata"
>
<![CDATA[
import json, hashlib\n
#from Products.ERP5Type.Log import log\n
import json\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
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,40 +178,52 @@ 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
self.simple_conversion_dict = {\n
"content_type": "format",\n
"effective_date": "posted_date",\n
"portal_type": "type",\n
"contributor_list": "contributor",\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
# order deny, allow\n
# deny from all\n
self.allowed_property_id_dict = ["title", "short_title", "description",\n
"language", "reference", "version",\n
"format", "posted_date", "type",\n
self.allowed_property_id_list = ["title", "short_title", "description",\n
"language", "reference", "version", "project",\n
"format", "type", "start", "stop", "state",\n
"effective_date", "expiration_date",\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
def getDocumentAttachment(self, metadata_json):\n
tool.checkMetadata(metadata_json)\n
try: document = tool.getDocumentFromUrl(metadata_json.get("_id"))\n
except AttributeError: raise ValueError("Bad document id")\n
except ValueError: raise LookupError("Missing document")\n
except KeyError: raise LookupError("Missing document")\n
except (ValueError, KeyError): raise LookupError("Missing 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
"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
attachment_key in document_dict:\n
data = document_dict[attachment_key]\n
if data is not None:\n
return 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
...
...
@@ -122,25 +235,27 @@ class JioGeneric():\n
document_dict = tool.getDocumentProperties(document)\n
real_document_dict = {}\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.get("_attachments"), {\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
}\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
...
...
@@ -149,11 +264,10 @@ class JioGeneric():\n
continue\n
if k in self.simple_conversion_dict:\n
k = self.simple_conversion_dict.get(k)\n
if k in self.allowed_property_id_dict:\n
try: v.time\n
except AttributeError: pass\n
else: v = str(v)\n
if k == "text_content":\n
if k in self.allowed_property_id_list:\n
if isinstance(v, DateTime):\n
v = v.ISO8601()\n
if k == attachment_key:\n
real_document_dict["_attachments"] = {\n
"body": {\n
"length": len(v)\n
...
...
@@ -169,7 +283,16 @@ class JioGeneric():\n
real_document_dict["relation"] = relation_list\n
real_document_dict[k] = v\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
# 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
\n
def updateDocumentMetadataEditKw(self, metadata_json,\n
...
...
@@ -183,7 +306,7 @@ class JioGeneric():\n
if jio_key == meta_key:\n
doc_key = erp5_key\n
break\n
if meta_key in self.allowed_property_id_
dic
t:\n
if meta_key in self.allowed_property_id_
lis
t:\n
if meta_value is None and document.hasProperty(doc_key) or \\\n
meta_value is not None:\n
edit_kw[doc_key] = meta_value\n
...
...
@@ -197,21 +320,25 @@ class JioGeneric():\n
def putDocumentAttachment(self, metadata_json):\n
tool.checkMetadata(metadata_json)\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
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
edit_kw = {attachment_key: metadata_json.get("_data")}\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
\n
def putDocumentMetadata(self, metadata_json, overwrite=True):\n
hard_code_json = metadata_json.copy();\n
doc_id = metadata_json.get("_id")\n
document = None\n
try: document = tool.getDocumentFromUrl(doc_id)\n
...
...
@@ -225,10 +352,13 @@ class JioGeneric():\n
document.getObject().edit(\n
**self.updateDocumentMetadataEditKw(metadata_json, document=document))\n
else:\n
# document does not exist\n
if "_id" in metadata_json:\n
try:\n
tool.newDocumentFromUrl(\n
metadata_json["_id"],\n
self.updateDocumentMetadataEditKw(metadata_json))\n
except KeyError: raise KeyError("Bad document id")\n
elif "type" in metadata_json:\n
try:\n
document = tool.newDocumentFromType(\n
...
...
@@ -239,7 +369,14 @@ class JioGeneric():\n
raise ValueError("Bad type")\n
else:\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
def removeDocument(self, metadata_json):\n
tool.checkMetadata(metadata_json)\n
...
...
@@ -248,18 +385,170 @@ 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 {"
id": metadata_json["_id"]
}\n
\n
def removeAttachment(self, document, data_dict):\n
document = document.getObject()\n
edit_kw = {}\n
if data_dict.get("_attachment") == "body.html":\n
edit_kw[self.mode["attachment_data_key"]] = None\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
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
class JioTool():\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
return {\n
"status": 405,\n
...
...
@@ -268,6 +557,15 @@ class JioTool():\n
"message": message,\n
"reason": reason\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
def createNotFoundDict(self, message, reason):\n
return {\n
...
...
@@ -286,10 +584,6 @@ class JioTool():\n
"message": message,\n
"reason": reason\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
def checkMetadata(self, metadata_json):\n
"Check if the id of the metadata is good"\n
...
...
@@ -330,26 +624,14 @@ class JioTool():\n
property_id =
property_definition["id"]\n
document_dict[property_id]
=
document.getProperty(property_id)\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
def
jsonUtf8Loads(self,
json_str):\n
return
self.recursiveEncodeToUtf8(json.loads(json_str)
)\n
return
json_loads(json_str
)\n
\n
def
stringifyDictDateValue(self,
obj_dict):\n
for
k,
v
in
obj_dict.items():\n
try:
v.time\n
except
AttributeError:
pass\n
else:
obj_dict[k]
=
str(v)\n
if
isinstance(v,
DateTime):\n
obj_dict[k]
=
v.ISO8601()\n
\n
def
formatMetadataToPut(self,
metadata_json):\n
for
k,
v
in
metadata_json.iteritems():\n
...
...
@@ -381,17 +663,25 @@ class JioTool():\n
def
getDocumentAttachment(self,
metadata_json):\n
return
self.jio.getDocumentAttachment(metadata_json)\n
\n
def
putDocumentMetadata(self,
metadata_json,
overwrite=
True):\n
return
self.jio.putDocumentMetadata(\n
self.formatMetadataToPut(\n
self.recursiveEncodeToUtf8(\n
self.jsonDeepCopy(metadata_json))),
overwrite=
overwrite)\n
def
putDocumentMetadata(self,
metadata_json,
overwrite=
True,
need_id=
False):\n
metadata =
self.formatMetadataToPut(\n
jsonDeepCopy(metadata_json))\n
if
need_id:\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
def
putDocumentAttachment(self,
attachment_json):\n
return
self.jio.putDocumentAttachment(attachment_json)\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
...
...
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_get.xml
View file @
c9cc425e
...
...
@@ -75,7 +75,7 @@ jio.setMode(mode)\n
try:\n
metadata_json =
jio.getDocumentMetadata(doc)\n
except
ValueError
as
e:\n
return
jio.sendError(jio.create
BadReques
tDict("Cannot
get
document",
str(e)))\n
return
jio.sendError(jio.create
Conflic
tDict("Cannot
get
document",
str(e)))\n
except
LookupError
as
e:\n
return
jio.sendError(jio.createNotFoundDict("Cannot
get
document",
str(e)))\n
\n
...
...
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_getAttachment.xml
View file @
c9cc425e
...
...
@@ -75,7 +75,7 @@ jio.setMode(mode)\n
try:\n
attachment_data =
jio.getDocumentAttachment(doc)\n
except
ValueError
as
e:\n
return
jio.sendError(jio.create
BadReques
tDict("Cannot
get
attachment",
str(e)))\n
return
jio.sendError(jio.create
Conflic
tDict("Cannot
get
attachment",
str(e)))\n
except
LookupError
as
e:\n
return
jio.sendError(jio.createNotFoundDict("Cannot
get
attachment",
str(e)))\n
\n
...
...
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_post.xml
View file @
c9cc425e
...
...
@@ -60,7 +60,8 @@ jio = context.JIO_class()\n
\n
try: doc = jio.jsonUtf8Loads(context.REQUEST.form["doc"])\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
try: mode = str(context.REQUEST.form["mode"])\n
except KeyError: mode = "generic"\n
...
...
@@ -68,12 +69,12 @@ 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
return jio.sendError(jio.createBadRequestDict("Cannot post document", str(e)))\n
except LookupError as e:\n
except (ValueError, TypeError, LookupError) as 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
return jio.sendSuccess(response_json)\n
</string>
</value>
...
...
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_put.xml
View file @
c9cc425e
...
...
@@ -67,11 +67,11 @@ except KeyError: mode = "generic"\n
jio.setMode(mode)\n
\n
try:\n
response_json = jio.putDocumentMetadata(doc)\n
except
ValueError
as e:\n
return jio.sendError(jio.create
BadReques
tDict("Cannot put document", str(e)))\n
except Type
Error as e:\n
return jio.sendError(jio.createBadRequest
Dict("Cannot put document", str(e)))\n
response_json = jio.putDocumentMetadata(doc
, need_id=True
)\n
except
(ValueError, TypeError, KeyError)
as e:\n
return jio.sendError(jio.create
Conflic
tDict("Cannot put document", str(e)))\n
# except Key
Error as e:\n
# return jio.sendError(jio.createForbidden
Dict("Cannot put document", str(e)))\n
\n
return jio.sendSuccess(response_json)\n
</string>
</value>
...
...
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_putAttachment.xml
View file @
c9cc425e
...
...
@@ -68,10 +68,10 @@ jio.setMode(mode)\n
\n
try:\n
response_json = jio.putDocumentAttachment(doc)\n
except
ValueError
as e:\n
return jio.sendError(jio.create
BadReques
tDict("Cannot put attachment", str(e)))\n
except TypeError as e:\n
return jio.sendError(jio.createBadRequestDict("Cannot put attachment", str(e)))\n
except
(ValueError, TypeError, KeyError)
as e:\n
return jio.sendError(jio.create
Conflic
tDict("Cannot put attachment", str(e)))\n
#
except TypeError as e:\n
#
return jio.sendError(jio.createBadRequestDict("Cannot put attachment", str(e)))\n
\n
return jio.sendSuccess(response_json)\n
</string>
</value>
...
...
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_remove.xml
View file @
c9cc425e
...
...
@@ -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>
...
...
bt5/erp5_jio/SkinTemplateItem/portal_skins/erp5_jio/JIO_removeAttachment.xml
View file @
c9cc425e
...
...
@@ -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.createConflictDict("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>
...
...
bt5/erp5_jio/bt/revision
View file @
c9cc425e
2
\ No newline at end of file
3
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment