DialogHistory.py 4.0 KB

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