Skip to content

Commit

Permalink
Added notebook prototype
Browse files Browse the repository at this point in the history
Ref: #287
  • Loading branch information
drfho committed Jun 19, 2024
1 parent f12ccd8 commit 1e726cd
Showing 1 changed file with 200 additions and 0 deletions.
200 changes: 200 additions & 0 deletions docs/notebooks/snippets_08_pythondocx.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Applying python-docx to ZMS content"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import ZODB\n",
"import os\n",
"from Products.Five.browser.tests.pages import SimpleView\n",
"from Products.zms import standard\n",
"from Products.zms import rest_api\n",
"import json\n",
"\n",
"# os.getcwd()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Create a ZODB connection to an existing ZODB database file\n",
"try:\n",
"\twd = '/home/zope/instance/zms5_dev/var/'\n",
"\tdb = ZODB.DB(os.path.join(wd, 'Data.fs'))\n",
"\tconn = db.open()\n",
"\troot = conn.root\n",
"\t###{'Application': <Application at >}\n",
"except:\n",
"\tdb.close()\n",
"\tprint('Error: Database connection had to be closed before reopened.')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Zope/src/Testing/makerequest.py\n",
"def makerequest(app, stdout=None, environ=None):\n",
" from io import BytesIO\n",
" from ZPublisher.BaseRequest import RequestContainer\n",
" from ZPublisher.HTTPRequest import HTTPRequest\n",
" from ZPublisher.HTTPResponse import HTTPResponse\n",
"\n",
" if stdout is None:\n",
" stdout = BytesIO()\n",
" if environ is None:\n",
" environ = {}\n",
" resp = HTTPResponse(stdout=stdout)\n",
" environ.setdefault('SERVER_NAME', 'nohost')\n",
" environ.setdefault('SERVER_PORT', '80')\n",
" environ.setdefault('REQUEST_METHOD', 'GET')\n",
" req = HTTPRequest(BytesIO(), environ, resp)\n",
" req._steps = ['noobject'] # Fake a published object.\n",
" req['ACTUAL_URL'] = req.get('URL') # Zope 2.7.4\n",
"\n",
" # Set default skin so that the request is usable for view look-ups.\n",
" from zope.publisher.browser import setDefaultSkin\n",
" setDefaultSkin(req)\n",
"\n",
" requestcontainer = RequestContainer(REQUEST=req)\n",
" return app.__of__(requestcontainer)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def makeresponse():\n",
" from io import StringIO\n",
" from ZPublisher.HTTPResponse import HTTPResponse\n",
" stdout = StringIO()\n",
" resp = HTTPResponse(stdout=stdout)\n",
" return resp"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"id\": \"e5\",\n",
" \"meta_id\": \"ZMSFolder\",\n",
" \"uid\": \"uid:60b30ef7-4e7d-4926-9309-c454906f6957\",\n",
" \"getPath\": \"/myzms2/content/e5\",\n",
" \"active\": 1,\n",
" \"title\": \"News about the ZMS concept\",\n",
" \"titlealt\": \"News\",\n",
" \"is_page\": true,\n",
" \"is_page_element\": false,\n",
" \"index_html\": \"./../../../../../../../myzms2/content/e5\",\n",
" \"parent_uid\": \"uid:4b46b796-d146-43c1-8eca-954d1ba2aafc\",\n",
" \"home_id\": \"myzms2\",\n",
" \"level\": 1,\n",
" \"restricted\": false,\n",
" \"titleimage\": null,\n",
" \"levelnfc\": \"\",\n",
" \"attr_dc_description\": \"Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusa\",\n",
" \"attr_dc_subject\": \"\",\n",
" \"attr_dc_type\": \"\",\n",
" \"attr_dc_creator\": \"\"\n",
"}\n"
]
}
],
"source": [
"# ZMS-Node /myzms2/content\n",
"context = root.Application.myzms2.content \n",
"# ##############################################\n",
"# Add REQUEST to zmscontext object\n",
"context = makerequest(context)\n",
"# Add REQUEST vars\n",
"context.REQUEST.set('lang','ger')\n",
"context.REQUEST.set('path_to_handle','')\n",
"# Add RESPONSE\n",
"context.REQUEST.set('RESPONSE', makeresponse())\n",
"# ##############################################\n",
"zmscontext = context.e5\n",
"request = rest_api._get_request(zmscontext)\n",
"\n",
"# tree_nodes = rest_api.RestApiController(context,request).list_tree_nodes(zmscontext)[0:1]\n",
"# print(json.dumps(tree_nodes,indent=2))\n",
"\n",
"doc = rest_api.get_attrs(zmscontext)\n",
"print(json.dumps(doc,indent=2))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from docx import Document\n",
"\n",
"document = Document()\n",
"\n",
"title = doc.get('title')\n",
"document.add_heading(title, 0)\n",
"\n",
"text = doc.get('attr_dc_description')\n",
"document.add_paragraph(text)\n",
"\n",
"document.save('demo.docx')"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"# Finally close ZODB connection\n",
"db.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "vpy38",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 1e726cd

Please sign in to comment.