-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
76 lines (51 loc) · 2.06 KB
/
database.py
File metadata and controls
76 lines (51 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime, Float, ForeignKey, Date
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from datetime import datetime, date
import enum
SQLALCHEMY_DATABASE_URL = "sqlite:///./access_control.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
class ResultEnum(enum.Enum):
ACCEPT = "ACCEPT"
REJECT = "REJECT"
SUSPICIOUS = "SUSPICIOUS"
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
face_id = Column(String, unique=True, index=True)
is_active = Column(Boolean, default=True)
first_name = Column(String)
last_name = Column(String)
badges = relationship("Badge", back_populates="user")
access_logs = relationship("AccessLog", back_populates="user")
class Badge(Base):
__tablename__ = "badges"
id = Column(Integer, primary_key=True, index=True)
qr_code = Column(String, unique=True, index=True)
valid_until = Column(Date)
user_id = Column(Integer, ForeignKey("users.id"))
user = relationship("User", back_populates="badges")
access_logs = relationship("AccessLog", back_populates="badge")
class AccessLog(Base):
__tablename__ = "access_logs"
id = Column(Integer, primary_key=True, index=True)
timestamp = Column(DateTime, default=datetime.utcnow)
result = Column(String)
match_score = Column(Float)
badge_id = Column(Integer, ForeignKey("badges.id"), nullable=True)
user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
image_path = Column(String, nullable=True)
user = relationship("User", back_populates="access_logs")
badge = relationship("Badge", back_populates="access_logs")
def init_db():
Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()