WorkWithDB.py 3.7 KB

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