DialogHistory.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. # from module.ImageTool import rotate_image
  3. from pdf2image import convert_from_path
  4. from PySide6.QtCore import Slot, QUrl
  5. from PySide6.QtGui import QPixmap
  6. from PySide6.QtWidgets import QDialog
  7. from sqlalchemy import func, select
  8. from logger import logger
  9. from module import create_filename
  10. from module.ModelPropusk import PropuskDataMethods
  11. from module.wkhtmltopdf import create_pdf
  12. from module.QRCode import make
  13. from module.TemplatePropusk import TemplatePropusk
  14. from module.WorkWithDB import connect, list_personal, list_place, list_propusk
  15. from .ui_py.ui_DialogHistory import Ui_DialogHistory
  16. from time import sleep
  17. class DialogHistory(Ui_DialogHistory, QDialog):
  18. def __init__(self, parent=None):
  19. super(DialogHistory, self).__init__(parent)
  20. self.setupUi(self)
  21. self.line_search.textChanged.connect(
  22. self.filter_items
  23. )
  24. self.list_propusk.itemSelectionChanged.connect(
  25. self.selected_item
  26. )
  27. self.load_data()
  28. self.list_propusk.setCurrentRow(0)
  29. def selected_item(self) -> None:
  30. try:
  31. id_propusk = self.list_propusk.selectedItems()[0].text()
  32. self.load_page(int(id_propusk))
  33. except IndexError as err:
  34. logger.debug(err)
  35. self.browser.clear()
  36. except OSError as err:
  37. logger.debug(err)
  38. @Slot(str)
  39. def filter_items(self, text: str) -> None:
  40. for index in range(self.list_propusk.count()):
  41. item = self.list_propusk.item(index)
  42. if text:
  43. item.setHidden(not self.match_text(text, item.text()))
  44. else:
  45. item.setHidden(False)
  46. def match_text(self, text: str, keywords:str) -> bool:
  47. return text in keywords
  48. def load_data(self) -> None:
  49. with connect() as conn:
  50. rows = conn.execute(
  51. list_propusk.select()) \
  52. .all()
  53. for line in rows:
  54. self.list_propusk.addItem(str(line.id_propusk))
  55. def load_page(self, id_propusk: int) -> None:
  56. with connect() as conn:
  57. row = conn.execute(
  58. select(
  59. list_propusk.c.id_propusk,
  60. list_propusk.c.date_from,
  61. list_propusk.c.date_to,
  62. func.format(
  63. list_personal.c.lastname
  64. + " " + list_personal.c.firstname
  65. + " " + list_personal.c.middlename)
  66. .label("personal"),
  67. func.format(list_place.c.name_place).label("place"),
  68. list_propusk.c.receiving_man,
  69. list_propusk.c.purpose_visite,
  70. list_propusk.c.face,
  71. list_propusk.c.document
  72. ).select_from(list_propusk).join(
  73. list_personal, list_propusk.c.personal == list_personal.c.id
  74. ).join(
  75. list_place, list_propusk.c.place == list_place.c.id
  76. ).where(list_propusk.c.id_propusk == id_propusk)
  77. ).fetchone()
  78. pdm = PropuskDataMethods(*row)
  79. face = os.path.join(os.environ.get(
  80. 'PHOTO_DIR'), pdm.get_value("face"))
  81. documet = os.path.join(os.environ.get(
  82. 'PHOTO_DIR'), pdm.get_value("document"))
  83. pdm.set_value("face", face)
  84. pdm.set_value("document", documet)
  85. pdm.set_value("qrcode", make(pdm.get_value('id_propusk'), create_filename('qr')))
  86. path_doc = os.path.join(os.path.dirname(os.path.abspath(__package__)), 'docs')
  87. render_text = str(TemplatePropusk(pdm._propusk_data.__dict__, path_doc))
  88. # print(render_text)
  89. # Print(render_text)
  90. # pdf = create_pdf(render_text)
  91. # sleep(3)
  92. # print(F"file://{pdf}")
  93. self.browser.setHtml(render_text)
  94. # image = convert_from_path(pdf)[0]
  95. # image_path = create_filename()
  96. # QPixmap.fromImage(image.toqimage()).save(image_path, 'jpg')
  97. # self.browser.setText(F"<img src='file:{image_path}'>")