WorkWithDB.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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("selected_cam", String, nullable=False),
  25. Column('created', DateTime, default=func.now()),
  26. Column('update', DateTime,
  27. onupdate=func.current_timestamp())
  28. )
  29. list_personal = Table("list_personal", meta,
  30. Column('id', Integer, primary_key=True),
  31. Column('lastname', String, nullable=False),
  32. Column('firstname', String, nullable=False),
  33. Column('middlename', String, nullable=False),
  34. Column('created', DateTime, default=func.now()),
  35. Column('update', DateTime,
  36. onupdate=func.current_timestamp())
  37. )
  38. list_place = Table("list_place", meta,
  39. Column('id', Integer, primary_key=True),
  40. Column('name_place', String, nullable=False),
  41. Column('created', DateTime, default=func.now()),
  42. Column('update', DateTime, onupdate=func.current_timestamp())
  43. )
  44. list_propusk = Table("list_propusk", meta,
  45. Column("id", Integer, primary_key=True),
  46. Column("id_propusk", Integer, nullable=False),
  47. Column("date_from", DateTime, nullable=False),
  48. Column("date_to", DateTime, nullable=False),
  49. Column("personal", Integer, ForeignKey("list_personal.id"), nullable=False),
  50. Column("place", Integer, ForeignKey("list_place.id"), nullable=False),
  51. Column("receiving_man", Text, nullable=False),
  52. Column("purpose_visite", Text, nullable=False),
  53. Column("face_photo", Text, nullable=False),
  54. Column("pasport_photo", Text, nullable=False),
  55. Column("created", DateTime, default=func.now()),
  56. Column("update", DateTime, default=func.now(),
  57. onupdate=func.current_timestamp()))
  58. engine = create_engine(F"sqlite:///{FILE_NAME}", echo=False)
  59. engine.logging_name = 'PropuskLogger'
  60. def init_db():
  61. if not os.path.exists(os.path.dirname(FILE_NAME)):
  62. os.mkdir(os.path.dirname(FILE_NAME))
  63. meta.create_all(engine)
  64. def connect():
  65. return engine.connect()