diff --git a/backend/app/tools/pdfjinja/README.md b/backend/app/tools/pdfjinja/README.md new file mode 100644 index 0000000..d57ec54 --- /dev/null +++ b/backend/app/tools/pdfjinja/README.md @@ -0,0 +1,25 @@ +#pdfjinja 1.0.0 + +##Dependencies : + +python-dev +python-pip +libtiff5-dev +libjpeg8-dev +zlib1g-dev +libfreetype6-dev +liblcms2-dev +libwebp-dev +tcl8.6-dev +tk8.6-dev +python-tk +pdftk +libmagickwand-dev + +##Installation: + +pip install pdfjinja + +##Test : + +pdfjinja -h diff --git a/backend/app/tools/pdfjinja/insertTemplate.py b/backend/app/tools/pdfjinja/insertTemplate.py new file mode 100644 index 0000000..7df834e --- /dev/null +++ b/backend/app/tools/pdfjinja/insertTemplate.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +""" Script python qui remplie les pdf basés sur un template jinja. """ + +import os + +from pdfjinja import PdfJinja + +def remplirTemplate (dirname_template, pdf_template, dirname_output_file, pdf_output,dictionnaire): + """ + Fonction qui permet de remplir un pdf template + :param dirname_template: chemin du fichier de template + :param pdf_template: nom du fichier de template + :param dirname_output_file: chemin des pdf généré + :param pdf_output: nom du fichier pdf à générer + :param dictionnaire: dictionnaire contenant le nom des textfields des pdf ainsi que leurs valeurs + :return: + """ + template_pdf_file = os.path.join(dirname_template, pdf_template) + template_pdf = PdfJinja(template_pdf_file) + + rendered_pdf = template_pdf(dictionnaire) + + output_file = os.path.join(dirname_output_file, pdf_output) + rendered_pdf.write(open(output_file, 'wb')) diff --git a/backend/app/utils.py b/backend/app/utils.py index ab6b5fc..64e6eda 100644 --- a/backend/app/utils.py +++ b/backend/app/utils.py @@ -5,11 +5,11 @@ from hashlib import sha512 SIMPLE_CHARS = string.ascii_letters + string.digits -def get_random_string(length=24): +def get_random_string(length=32): return ''.join(random.choice(SIMPLE_CHARS) for i in range(length)) -def get_random_hash(length=24): +def get_random_hash(length=64): hash = sha512() hash.update(get_random_string()) return hash.hexdigest()[:length] diff --git a/backend/manage.py b/backend/manage.py index 13a8b16..82c0eb5 100644 --- a/backend/manage.py +++ b/backend/manage.py @@ -76,8 +76,6 @@ class RunTests(Command): """Seed the db """ def run(self): Config.configure_app(config="test") - os.system("python manage.py -t db downgrade base") - os.system("python manage.py -t db upgrade") test_loader = unittest.defaultTestLoader test_runner = unittest.TextTestRunner() test_suite = test_loader.discover('tests') diff --git a/backend/tests/__init__.py b/backend/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/tests/tools/pdfjinja/sample.pdf b/backend/tests/tools/pdfjinja/sample.pdf new file mode 100644 index 0000000..18d520f Binary files /dev/null and b/backend/tests/tools/pdfjinja/sample.pdf differ diff --git a/backend/tests/tools/pdfjinja/sample_backup.pdf b/backend/tests/tools/pdfjinja/sample_backup.pdf new file mode 100644 index 0000000..37e02b4 Binary files /dev/null and b/backend/tests/tools/pdfjinja/sample_backup.pdf differ diff --git a/backend/tests/tools/pdfjinja/testInsertTemplate.py b/backend/tests/tools/pdfjinja/testInsertTemplate.py new file mode 100644 index 0000000..c613537 --- /dev/null +++ b/backend/tests/tools/pdfjinja/testInsertTemplate.py @@ -0,0 +1,53 @@ +import json +import os +import unittest +from io import BytesIO + +from pathlib import Path +from pdfjinja import PdfJinja + +from app.tools.pdfjinja.insertTemplate import remplirTemplate + + +class insertTemplateTestCase(unittest.TestCase): + + datadir = os.path.join(os.path.dirname(__file__)) + + def setUp(self): + pdffile = os.path.join(self.datadir, "sample.pdf") + self.data = { + 'firstName': 'Renan', + 'lastName': 'Husson', + 'address': { + 'street': '24 rue de la pommes', + 'apt': 'C317', + 'city': 'TOULOUSE', + 'zipcode': 31000 + }, + 'universite':'Jean Jaures', + 'spirit': 'Panda', + 'evil': True, + 'language': { + 'french': True, + 'esperento': True + } +} + self.pdfjinja = PdfJinja(pdffile) + + def tearDown(self): + del self.data + del self.pdfjinja + + def test_render(self): + remplirTemplate(self.datadir,"sample.pdf",self.datadir,"output.pdf",self.data) + output = self.pdfjinja(self.data) + outfile = BytesIO() + output.write(outfile) + outfile.seek(0) + self.assertTrue(len(outfile.read()) > 0, "Output PDF is not empty.") + self.assertTrue(Path(self.datadir+"/output.pdf").is_file(),"Pdf généré inexistant") + os.remove(self.datadir+"/output.pdf") + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/backend/tox.ini b/backend/tox.ini index 5a98442..cdb5ede 100644 --- a/backend/tox.ini +++ b/backend/tox.ini @@ -7,7 +7,7 @@ skipsdist = True [testenv] commands= - coverage run --source app/api --omit app/api/*/model.py manage.py runtests + coverage run --source app/api app/tools --omit app/api/*/model.py manage.py runtests coverage report -m coverage xml coverage html