ListPersonal.py 4.0 KB

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