From 2427d0c5cb88c4621556096d531881c64cf0e4fe Mon Sep 17 00:00:00 2001 From: sergei kasianenko Date: Sat, 7 Dec 2024 23:54:14 +0800 Subject: [PATCH 1/3] fix results_by_direction --- directory/models.py | 1 + results/forms/forms900.py | 122 ++++++++++++++++++++++++++++++++++++++ results/sql_func.py | 22 +++++++ 3 files changed, 145 insertions(+) create mode 100644 results/forms/forms900.py diff --git a/directory/models.py b/directory/models.py index 277b83fb7e..0e48946630 100644 --- a/directory/models.py +++ b/directory/models.py @@ -252,6 +252,7 @@ class Researches(models.Model): (11002, "110.02 - Заключение ВК по психиатрическому освидетельствованию"), (11101, "111.01 - Карта профосмотра несовершеннолетнего N 030-ПО/у-17"), (11201, "112.01 - Извещение о НР ЛП"), + (90001, "900.01 - Печать из схемы json"), ) RESULT_TITLE_FORMS = ( diff --git a/results/forms/forms900.py b/results/forms/forms900.py new file mode 100644 index 0000000000..22f7e42230 --- /dev/null +++ b/results/forms/forms900.py @@ -0,0 +1,122 @@ +from reportlab.platypus import PageBreak +import os.path + +from laboratory.settings import BASE_DIR +import simplejson as json + + +import json + +from reportlab.platypus import Paragraph, Spacer +from reportlab.lib.styles import getSampleStyleSheet +from reportlab.lib.units import mm +from copy import deepcopy +from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY, TA_RIGHT +from directions.models import Issledovaniya, Napravleniya +from laboratory.settings import FONTS_FOLDER +import os.path +from reportlab.pdfbase import pdfmetrics +from reportlab.pdfbase.ttfonts import TTFont + +from results.sql_func import get_paraclinic_result_by_iss + + +def form_01(direction: Napravleniya, iss: Issledovaniya, fwb, doc, leftnone, user=None, **kwargs): + """ + Форма печати протокола из шаблона + """ + + pdfmetrics.registerFont(TTFont("PTAstraSerifBold", os.path.join(FONTS_FOLDER, "PTAstraSerif-Bold.ttf"))) + pdfmetrics.registerFont(TTFont("PTAstraSerifReg", os.path.join(FONTS_FOLDER, "PTAstraSerif-Regular.ttf"))) + + styleSheet = getSampleStyleSheet() + style = styleSheet["Normal"] + style.fontName = "PTAstraSerifReg" + style.fontSize = 11 + style.leading = 12 + style.spaceAfter = 1.2 * mm + style.alignment = TA_JUSTIFY + style.firstLineIndent = 15 + + styleFL = deepcopy(style) + styleFL.firstLineIndent = 0 + + styleBold = deepcopy(style) + styleBold.fontName = "PTAstraSerifBold" + styleBold.firstLineIndent = 0 + + styleCenter = deepcopy(style) + styleCenter.alignment = TA_CENTER + styleCenter.spaceAfter = 0 * mm + + styleRight = deepcopy(style) + styleRight.aligment = TA_RIGHT + + styleCenterBold = deepcopy(styleBold) + styleCenterBold.alignment = TA_CENTER + styleCenterBold.firstLineIndent = 0 + styleCenterBold.fontSize = 12 + styleCenterBold.leading = 13 + styleCenterBold.face = "PTAstraSerifBold" + + styleTableCentre = deepcopy(style) + styleTableCentre.alignment = TA_CENTER + styleTableCentre.spaceAfter = 4.5 * mm + styleTableCentre.fontSize = 8 + styleTableCentre.leading = 4.5 * mm + + styleT = deepcopy(style) + styleT.firstLineIndent = 0 + styleJustified = deepcopy(style) + styleJustified.alignment = TA_JUSTIFY + styleJustified.spaceAfter = 4.5 * mm + styleJustified.fontSize = 12 + styleJustified.leading = 4.5 * mm + + styles_obj = { + "style": style, + "styleCenter": styleCenter, + "styleBold": styleBold, + "styleCenterBold": styleCenterBold, + "styleJustified": styleJustified, + "styleRight": styleRight, + } + + if not os.path.join(BASE_DIR, "forms", "pdf_templates"): + current_template_file = os.path.join(BASE_DIR, "forms", "pdf_templates", "template_federal_order_530_titul_page.json") + else: + current_template_file = os.path.join(BASE_DIR, "forms", "pdf_templates", "template_federal_order_530_titul_page.json") + current_template_file = "" + + fields_values = get_paraclinic_result_by_iss(iss.pk) + result_data = {i.field_title: i.field_value for i in fields_values} + + if current_template_file: + with open(current_template_file) as json_file: + data = json.load(json_file) + body_paragraphs = data["body_paragraphs"] + + objs = [] + if current_template_file: + for section in body_paragraphs: + objs = check_section_param(objs, styles_obj, section, result_data) + + fwb.extend(objs) + + return fwb + + +def check_section_param(objs, styles_obj, section, field_titles_value): + if section.get("Spacer"): + height_spacer = section.get("spacer_data") + objs.append(Spacer(1, height_spacer * mm)) + elif section.get("page_break"): + objs.append(PageBreak()) + elif section.get("text"): + field_titles_sec = section.get("fieldTitles") + data_fields = [field_titles_value.get(i) for i in field_titles_sec if field_titles_value.get(i)] + difference = len(field_titles_sec) - len(data_fields) + if len(data_fields) < len(field_titles_sec): + data_fields = [*data_fields, *["" for count in range(difference)]] + objs.append(Paragraph(section.get("text").format(*data_fields), styles_obj[section.get("style")])) + return objs diff --git a/results/sql_func.py b/results/sql_func.py index d319e407a7..184e519cd2 100644 --- a/results/sql_func.py +++ b/results/sql_func.py @@ -123,6 +123,28 @@ def get_paraclinic_results_by_direction(pk_dir): return rows +def get_paraclinic_result_by_iss(pk_iss): + with connection.cursor() as cursor: + cursor.execute( + """ + SELECT + directions_paraclinicresult.value as field_value, + directions_paraclinicresult.field_id as field_id, + directory_paraclinicInputField.title as field_title, + directory_paraclinicinputgroups.title as group_title + FROM directions_paraclinicresult + LEFT JOIN directory_paraclinicinputfield ON + directions_paraclinicresult.field_id=directory_paraclinicinputfield.id + LEFT JOIN directory_paraclinicinputgroups ON + directory_paraclinicInputField.group_id=directory_paraclinicinputgroups.id + WHERE directions_paraclinicresult.issledovaniye_id = %(pk_iss)s + """, + params={'pk_iss': pk_iss}, + ) + rows = namedtuplefetchall(cursor) + return rows + + def get_expertis_child_iss_by_issledovaniya(parent_iss_tuple): with connection.cursor() as cursor: cursor.execute( From 7fb44c61b72eca48c7063725c336527ccec03d9f Mon Sep 17 00:00:00 2001 From: sergei kasianenko Date: Sun, 8 Dec 2024 00:10:09 +0800 Subject: [PATCH 2/3] fix --- results/forms/forms900.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/results/forms/forms900.py b/results/forms/forms900.py index 22f7e42230..0c716d0553 100644 --- a/results/forms/forms900.py +++ b/results/forms/forms900.py @@ -1,12 +1,7 @@ from reportlab.platypus import PageBreak import os.path - from laboratory.settings import BASE_DIR import simplejson as json - - -import json - from reportlab.platypus import Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.units import mm From 78832655d18520e4cab72fb18e3836b615c7e1ca Mon Sep 17 00:00:00 2001 From: sergei kasianenko Date: Sun, 8 Dec 2024 00:26:15 +0800 Subject: [PATCH 3/3] fix --- directory/models.py | 1 - results/forms/{forms900.py => json_schema.py} | 0 2 files changed, 1 deletion(-) rename results/forms/{forms900.py => json_schema.py} (100%) diff --git a/directory/models.py b/directory/models.py index 0e48946630..277b83fb7e 100644 --- a/directory/models.py +++ b/directory/models.py @@ -252,7 +252,6 @@ class Researches(models.Model): (11002, "110.02 - Заключение ВК по психиатрическому освидетельствованию"), (11101, "111.01 - Карта профосмотра несовершеннолетнего N 030-ПО/у-17"), (11201, "112.01 - Извещение о НР ЛП"), - (90001, "900.01 - Печать из схемы json"), ) RESULT_TITLE_FORMS = ( diff --git a/results/forms/forms900.py b/results/forms/json_schema.py similarity index 100% rename from results/forms/forms900.py rename to results/forms/json_schema.py