DialogHistory.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from PySide6.QtWidgets import QDialog
  2. from PySide6.QtCore import Slot
  3. from .ui_py.ui_DialogHistory import Ui_DialogHistory
  4. from module.WorkWithDB import connect, list_propusk, list_personal, list_place
  5. from sqlalchemy import select, func
  6. from module.ModelPropusk import PropuskDataMethods
  7. from module.TemplatePropusk import TemplatePropusk
  8. import os
  9. from module.ImageTool import rotate_image
  10. from logger import logger
  11. class DialogHistory(Ui_DialogHistory, QDialog):
  12. def __init__(self, parent=None):
  13. super(DialogHistory, self).__init__(parent)
  14. self.setupUi(self)
  15. self.line_search.textChanged.connect(
  16. self.filter_items
  17. )
  18. self.list_propusk.itemSelectionChanged.connect(
  19. self.selected_item
  20. )
  21. self.load_data()
  22. self.list_propusk.setCurrentRow(0)
  23. def selected_item(self) -> None:
  24. try:
  25. id_propusk = self.list_propusk.selectedItems()[0].text()
  26. self.load_page(int(id_propusk))
  27. except IndexError as err:
  28. logger.debug(err)
  29. self.browser.clear()
  30. except OSError as err:
  31. logger.debug(err)
  32. @Slot(str)
  33. def filter_items(self, text: str) -> None:
  34. for index in range(self.list_propusk.count()):
  35. item = self.list_propusk.item(index)
  36. if text:
  37. item.setHidden(not self.match_text(text, item.text()))
  38. else:
  39. item.setHidden(False)
  40. def match_text(self, text, keywords) -> None:
  41. return text in keywords
  42. def load_data(self) -> None:
  43. with connect() as conn:
  44. rows = conn.execute(
  45. list_propusk.select()) \
  46. .all()
  47. for line in rows:
  48. self.list_propusk.addItem(str(line.id_propusk))
  49. def load_page(self, id_propusk: int) -> None:
  50. with connect() as conn:
  51. row = conn.execute(
  52. select(
  53. list_propusk.c.id_propusk,
  54. list_propusk.c.date_from,
  55. list_propusk.c.date_to,
  56. func.format(
  57. list_personal.c.lastname
  58. + " " + list_personal.c.firstname
  59. + " " + list_personal.c.middlename)
  60. .label("personal"),
  61. func.format(list_place.c.name_place).label("place"),
  62. list_propusk.c.receiving_man,
  63. list_propusk.c.purpose_visite,
  64. list_propusk.c.face_photo
  65. ).select_from(list_propusk).join(
  66. list_personal, list_propusk.c.personal == list_personal.c.id
  67. ).where(list_propusk.c.id_propusk == id_propusk)
  68. ).fetchone()
  69. pdm = PropuskDataMethods(*row)
  70. path_photo = os.path.join(os.environ.get('PHOTO_DIR'), pdm.get_value("face_photo"))
  71. pdm.set_value("face_photo", rotate_image(path_photo))
  72. render_text = str(TemplatePropusk(pdm._propusk_data.__dict__,
  73. os.path.join(os.path.dirname(
  74. os.path.abspath(__package__)), 'docs')
  75. ))
  76. logger.info(render_text)
  77. self.browser.setText(render_text)