WorkWithDB.py 3.4 KB

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