WorkWithDB.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from sqlalchemy import (MetaData, Table, Column, Integer,
  2. DateTime, String, Text, ForeignKey,
  3. create_engine, func)
  4. import os
  5. import appdirs
  6. meta = MetaData()
  7. PATH = os.path.join(appdirs.user_data_dir(), "propusk").replace('\\', '\\\\')
  8. FILE_NAME = os.path.join(PATH, "propusk.db").replace('\\', '\\\\')
  9. cam_setting = Table("сam_setting", meta,
  10. Column('id', Integer, primary_key=True),
  11. Column("selected_cam", Integer, nullable=False),
  12. Column('created', DateTime, default=func.now()),
  13. Column('update', DateTime,
  14. onupdate=func.current_timestamp())
  15. )
  16. list_personal = Table("list_personal", meta,
  17. Column('id', Integer, primary_key=True),
  18. Column('lastname', String, nullable=False),
  19. Column('firstname', String, nullable=False),
  20. Column('middlename', String, nullable=False),
  21. Column('created', DateTime, default=func.now()),
  22. Column('update', DateTime,
  23. onupdate=func.current_timestamp())
  24. )
  25. list_place = Table("list_place", meta,
  26. Column('id', Integer, primary_key=True),
  27. Column('name_place', String, nullable=False),
  28. Column('created', DateTime, default=func.now()),
  29. Column('update', DateTime, onupdate=func.current_timestamp())
  30. )
  31. list_propusk = Table("list_propusk", meta,
  32. Column("id", Integer, primary_key=True),
  33. Column("id_propusk", Integer, nullable=False),
  34. Column("date_from", DateTime, nullable=False),
  35. Column("date_to", DateTime, nullable=False),
  36. Column("personal", Integer, ForeignKey("list_personal.id"), nullable=False),
  37. Column("place", Integer, ForeignKey("list_place.id"), nullable=False),
  38. Column("receiving_man", Text, nullable=False),
  39. Column("purpose_visite", Text, nullable=False),
  40. Column("face_photo", Text, nullable=False),
  41. Column("pasport_photo", Text, nullable=False),
  42. Column("created", DateTime, default=func.now()),
  43. Column("update", DateTime, default=func.now(),
  44. onupdate=func.current_timestamp()))
  45. engine = create_engine(F"sqlite:///{FILE_NAME}", echo=True)
  46. def init_db():
  47. if not os.path.exists(PATH):
  48. os.mkdir(PATH)
  49. meta.create_all(engine)
  50. def connect():
  51. return engine.connect()