From 6cbf0e477d57e17414514e677de6234559468ba3 Mon Sep 17 00:00:00 2001
From: Jacek Lebioda <jacek.lebioda.001@student.uni.lu>
Date: Sat, 4 Mar 2017 00:11:44 +0100
Subject: [PATCH] Docx processing

---
 readme.md                | 10 +++++-----
 requirements.txt         |  2 ++
 smash/web/docx_helper.py | 30 ++++++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 5 deletions(-)
 create mode 100644 smash/web/docx_helper.py

diff --git a/readme.md b/readme.md
index 42b1f415..901118ed 100644
--- a/readme.md
+++ b/readme.md
@@ -3,7 +3,7 @@
 [![coverage report](https://git-r3lab.uni.lu/piotr.atyjaszyk/scheduling-system/badges/master/coverage.svg)](https://git-r3lab.uni.lu/piotr.atyjaszyk/scheduling-system/commits/master)
 
 ## Required software (on ubuntu's OS family):
-  - `sudo apt-get install libpq-dev python-dev postgresql postgresql-contrib python virtualenv python-virtualenv gcc`
+  - `sudo apt-get install libpq-dev python-dev postgresql postgresql-contrib python virtualenv python-virtualenv gcc python-lxml libxml2-dev`
 
 ## Postgres installation
   - If you don't have postgres installed, complete step seven from [https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn](https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn) (remember to save all the credentials, they will be necessary to run the application).
@@ -30,9 +30,9 @@
 ## Production deployment
   - git pull and other project installation should be performed in a dir where this django app should be installed, in this tutorial it's /var/www/scheduling-system/
   - install nginx: `apt-get install nginx`
-  - create gunicorn service in systemd (http://docs.gunicorn.org/en/stable/deploy.html#systemd): 
+  - create gunicorn service in systemd (http://docs.gunicorn.org/en/stable/deploy.html#systemd):
 
-### /etc/systemd/system/gunicorn.service 
+### /etc/systemd/system/gunicorn.service
 
 ```
 [Unit]
@@ -67,7 +67,7 @@ ListenStream=[::]:8000
 
 [Install]
 WantedBy=sockets.target
-  
+
    - modify nginx configuration
 
 # /etc/nginx/nginx.conf
@@ -173,4 +173,4 @@ server {
 }
 ```
   - extract static files and make them available via nginx: `./manage.py collectstatic`
-  - you start application by starting gunicorn and nginx: `service gunicorn start`, `service nginx start`
\ No newline at end of file
+  - you start application by starting gunicorn and nginx: `service gunicorn start`, `service nginx start`
diff --git a/requirements.txt b/requirements.txt
index 72e849e2..c251252a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -3,3 +3,5 @@ gunicorn==19.6.0
 Pillow==3.4.2
 psycopg2==2.6.2
 pytz==2016.10
+lxml==3.7.3
+python-docx==0.8.6
\ No newline at end of file
diff --git a/smash/web/docx_helper.py b/smash/web/docx_helper.py
new file mode 100644
index 00000000..b3c0d5e7
--- /dev/null
+++ b/smash/web/docx_helper.py
@@ -0,0 +1,30 @@
+import copy
+from docx import Document
+
+def dfs_update(node, replace_dict):
+	# If this is a non-empty text node
+	if node.text != None and len(node.text) > 0:
+		# Apply transformations to node's text
+		for what_to_replace in replace_dict:
+			node.text = node.text.replace(what_to_replace, replace_dict[what_to_replace])
+	# Update child elements
+	for child in node:
+		dfs_update(child, replace_dict)
+
+
+def process_file(path_to_docx, path_to_new_docx, changes_to_apply):
+	"""
+	Tries to open the docx document using given path to file.
+	Then, applies the transformations- replaces template tags
+	in format of ##name## to values specified in the second
+	argument.
+	"""
+	generated = Document()
+	doc = Document(path_to_docx)
+	for c in changes_to_apply:
+		for element in doc.part.element[0]:
+			w = copy.deepcopy(element)
+			dfs_update(w, c)
+			generated._body._element.append(w)
+
+	generated.save(path_to_new_docx)
-- 
GitLab