ListPersonal.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from .ui_py.ui_DialogListPersonal import Ui_DialogListPersonal
  2. from module.WorkWithDB import connect, list_personal
  3. from module.MessageBox import showDialog
  4. from PySide6.QtWidgets import QDialog, QListWidgetItem, QMessageBox
  5. from PySide6.QtCore import Qt, Slot
  6. class ListPersonal(QDialog, Ui_DialogListPersonal):
  7. def __init__(self, update_listbox_func, parent=None) -> None:
  8. super(ListPersonal, self).__init__(parent)
  9. self.setAttribute(Qt.WA_DeleteOnClose)
  10. self.setupUi(self)
  11. self.update_listbox_func = update_listbox_func
  12. self.btn_save_personal.clicked.connect(self._save)
  13. self.btn_update_personal.clicked.connect(self._update)
  14. self.btn_delete_personal.clicked.connect(self._delete)
  15. self.list_personal_widget.itemSelectionChanged.connect(
  16. self._selected_item)
  17. self._load_data()
  18. @Slot()
  19. def _save(self) -> None:
  20. if any([
  21. self.lastname.text(),
  22. self.firstname.text(),
  23. self.middlename.text()]):
  24. with connect() as conn:
  25. insert_personal = list_personal.insert().values(
  26. lastname=self.lastname.text(),
  27. firstname=self.firstname.text(),
  28. middlename=self.middlename.text()
  29. )
  30. conn.execute(insert_personal)
  31. self._clean_line_edit()
  32. self._load_data()
  33. else:
  34. showDialog(
  35. QMessageBox.Warning,
  36. title="Пустые поля",
  37. massage="Заполните все поля!"
  38. )
  39. def _clean_line_edit(self):
  40. self.lastname.clear()
  41. self.firstname.clear()
  42. self.middlename.clear()
  43. @Slot()
  44. def _update(self) -> None:
  45. with connect() as conn:
  46. update_personal = list_personal.update().where(
  47. list_personal.c.id == self.selected_id
  48. ).values(
  49. lastname=self.lastname.text(),
  50. firstname=self.firstname.text(),
  51. middlename=self.middlename.text()
  52. )
  53. conn.execute(update_personal)
  54. self._clean_line_edit()
  55. self._load_data()
  56. @Slot()
  57. def _delete(self) -> None:
  58. with connect() as conn:
  59. if hasattr(self, 'selected_id'):
  60. conn.execute(
  61. list_personal.delete().where(
  62. list_personal.c.id == self.selected_id)
  63. )
  64. else:
  65. showDialog(
  66. QMessageBox.Warning,
  67. title="Не выбран элемент списка",
  68. massage="Выбирите элемент списка перед тем как удалять!"
  69. )
  70. self._clean_line_edit()
  71. self._load_data()
  72. @Slot()
  73. def _selected_item(self) -> None:
  74. try:
  75. id = self.list_personal_widget.selectedItems()[0].id
  76. self.selected_id = int(id)
  77. with connect() as conn:
  78. personal = list_personal.select().where(
  79. list_personal.c.id == id)
  80. personal_from_db = conn.execute(personal).fetchone()
  81. self.lastname.setText(personal_from_db.lastname)
  82. self.firstname.setText(personal_from_db.firstname)
  83. self.middlename.setText(personal_from_db.middlename)
  84. except IndexError:
  85. self._clean_line_edit()
  86. def _load_data(self) -> None:
  87. self.list_personal_widget.clear()
  88. with connect() as conn:
  89. for personal in conn.execute(list_personal.select()).all():
  90. self.list_personal_widget.addItem(self._create_item(
  91. id=personal.id,
  92. lastname=personal.lastname,
  93. firstname=personal.firstname,
  94. middlename=personal.middlename
  95. ))
  96. def _create_item(self, id, lastname, firstname, middlename) -> None:
  97. item = QListWidgetItem(F"{lastname} {firstname} {middlename}")
  98. item.id = id
  99. return item