Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiline strings #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
Из папки репозитория:

Запуск:


cp .env_template .env
docker-compose build
docker-compose up

Деплой на production:

./build_and_run.sh <RECAPTCHA_SITE_KEY> <RECAPTCHA_SECRET_KEY>

Запуск тестов (когда докер работает и прослушивает адрес localhost:5000):

pytest ./app/templater/tests.py -s
Expand All @@ -18,4 +23,4 @@
или запустите тестовую конфигурацию докера:

docker-compose -f ./docker-compose.test.yml build
docker-compose -f ./docker-compose.test.yml up
docker-compose -f ./docker-compose.test.yml up
18 changes: 13 additions & 5 deletions app/templater/lib/templater/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import csv
import xlrd
from docxtpl import DocxTemplate
from docxtpl import DocxTemplate, RichText
from jinja2 import Environment, Template, meta
from .custom_exceptions import TemplateTypeNotSupported, OutputNameTemplateSyntax
from .custom_odt_renderer import CustomOdtRenderer
Expand All @@ -20,23 +20,31 @@ def __init__(self):

class TemplateRenderer(object):

MULTILINE_PREFIX = "&^<\\multiline>"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add examples of templates and tables for this feature and update README.md with its description


def __init__(self):
self.contexts = []
self.fieldnames = []
self.raw_table = []

def _preprocess_value(self, value):
if value.startswith(self.MULTILINE_PREFIX):
value = RichText(value[len(self.MULTILINE_PREFIX):])
return value

# Load data from csv file
def load_csv(self, csvfile, delimiter):
# open csv file and prepare context array
lines = csvfile.read().decode("utf-8-sig").splitlines()
reader = csv.DictReader(lines, delimiter=delimiter)
lines = csvfile.read().decode("utf-8-sig")
reader = csv.DictReader(io.StringIO(lines), delimiter=delimiter)
self.fieldnames = reader.fieldnames
self.raw_table.append(self.fieldnames)
for row in reader:
context = {}
r = []
for key, value in row.items():
context[key] = value
value = value.lstrip('\"').rstrip('\"')
context[key] = self._preprocess_value(value)
r.append(value)
self.contexts.append(context)
self.raw_table.append(r)
Expand All @@ -52,7 +60,7 @@ def load_excel(self, excelfile):
r = []
for col in range(sh.ncols):
value = str(sh.cell_value(rowx=row, colx=col))
context[self.fieldnames[col]] = value
context[self.fieldnames[col]] = self._preprocess_value(value)
r.append(value)
self.contexts.append(context)
self.raw_table.append(r)
Expand Down