WorkWithDB.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from sqlalchemy import (MetaData, Table, Column, Integer,
  2. DateTime, String, Text, ForeignKey,
  3. create_engine, func)
  4. from sqlalchemy.exc import ProgrammingError
  5. import os
  6. from module.MyMessageBox import show_dialog
  7. from PySide6.QtWidgets import QMessageBox
  8. from logger import logger
  9. meta = MetaData()
  10. # /home/asidorov/Документы/propusk_db
  11. FILE_NAME = None
  12. if os.environ.get("DB_DIR"):
  13. FILE_NAME = os.path.join(os.environ.get("DB_DIR"), "propusk.db")
  14. else:
  15. if os.environ.get("DEFAULT_PATH"):
  16. FILE_NAME = os.path.join(os.environ.get("DEFAULT_PATH"), "propusk.db")
  17. else:
  18. logger.error("Не правильно указан путь к базе данных, или вообще отсутствует")
  19. show_dialog(
  20. QMessageBox.Icon.Critical,
  21. "Путь к бд",
  22. "Не правильно указан путь к базе данных, или вообще отсутствует"
  23. )
  24. cam_setting = Table("сam_setting", meta,
  25. Column('id', Integer, primary_key=True),
  26. Column('type', Integer, nullable=False),
  27. Column('mode', String, nullable=False),
  28. Column("selected_cam", String, nullable=False),
  29. Column('created', DateTime, default=func.now()),
  30. Column('update', DateTime,
  31. onupdate=func.current_timestamp())
  32. )
  33. list_personal = Table("list_personal", meta,
  34. Column('id', Integer, primary_key=True),
  35. Column('lastname', String, nullable=False),
  36. Column('firstname', String, nullable=False),
  37. Column('middlename', String, nullable=False),
  38. Column('created', DateTime, default=func.now()),
  39. Column('update', DateTime,
  40. onupdate=func.current_timestamp())
  41. )
  42. list_place = Table("list_place", meta,
  43. Column('id', Integer, primary_key=True),
  44. Column('name_place', String, nullable=False),
  45. Column('created', DateTime, default=func.now()),
  46. Column('update', DateTime, onupdate=func.current_timestamp())
  47. )
  48. list_propusk = Table("list_propusk", meta,
  49. Column("id", Integer, primary_key=True),
  50. Column("id_propusk", Integer, nullable=False),
  51. Column("date_from", DateTime, nullable=False),
  52. Column("date_to", DateTime, nullable=False),
  53. Column("personal", Integer, ForeignKey("list_personal.id"), nullable=False),
  54. Column("place", Integer, ForeignKey("list_place.id"), nullable=False),
  55. Column("receiving_man", Text, nullable=False),
  56. Column("purpose_visite", Text, nullable=False),
  57. Column("face", Text, nullable=False),
  58. Column("document", Text, nullable=False),
  59. Column("created", DateTime, default=func.now()),
  60. Column("update", DateTime, default=func.now(),
  61. onupdate=func.current_timestamp()))
  62. list_ussued_passes = Table("list_ussued_passes", meta,
  63. Column("id", Integer, primary_key=True),
  64. Column("used_pass", Integer, nullable=False),
  65. Column("id_propusk", Integer, ForeignKey("list_propusk.id_propusk"), nullable=False),
  66. Column("created", DateTime, default=func.now()),
  67. Column("update", DateTime, default=func.now(), onupdate=func.current_timestamp()))
  68. engine = create_engine(F"sqlite:///{FILE_NAME}", echo=False)
  69. engine.logging_name = 'PropuskLogger'
  70. def init_db():
  71. if not os.path.exists(os.path.dirname(FILE_NAME)):
  72. os.mkdir(os.path.dirname(FILE_NAME))
  73. meta.create_all(engine)
  74. def connect():
  75. return engine.connect()
  76. def check_error_sql(func):
  77. def wrapper(*arg, **args):
  78. try:
  79. return func(arg, args)
  80. except ProgrammingError as pe:
  81. logger.error(pe)
  82. return wrapper