WorkWithDB.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. meta = MetaData()
  8. FILE_NAME = None
  9. if os.environ.get("DB_DIR"):
  10. FILE_NAME = os.path.join(os.environ.get("DB_DIR"), "propusk.db")
  11. else:
  12. if os.environ.get("DEFAULT_PATH"):
  13. FILE_NAME = os.path.join(os.environ.get("DEFAULT_PATH"), "propusk.db")
  14. else:
  15. show_dialog(
  16. QMessageBox.Icon.Critical,
  17. "Путь к бд",
  18. "Не правильно указан путь к базе данных, или вообще отсутствует"
  19. )
  20. raise ValueError(F"Не правильно указан путь к базе данных, или вообще отсутствует")
  21. print("q", FILE_NAME)
  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=True)
  59. def init_db():
  60. print(FILE_NAME)
  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()