File size: 8,854 Bytes
8ed8485
 
 
 
a0ee4b9
8ed8485
 
 
 
 
 
 
 
 
a0ee4b9
 
 
 
 
 
 
 
 
 
 
 
 
8ed8485
 
 
 
 
 
 
 
 
 
 
22b2a22
 
be0fc92
8ed8485
 
 
 
 
 
 
 
 
 
 
 
 
 
4e949ea
8ed8485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250b7bf
 
 
 
8ed8485
4c0f818
8ed8485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250b7bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c0f818
 
13b2f24
 
4c0f818
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from datetime import datetime, timedelta
from typing import Annotated
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from fastapi.middleware.cors import CORSMiddleware
from sqlmodel import Session, select
from config import settings

from models import Token, Site, Guest, Visit, User, UserPublicMe, UserCreate
from routers import users, sites, visits, guests, profile
from core import utils, crud

app = FastAPI()

origins = [
    "http://localhost",
    "http://localhost:3000",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(profile.router)
app.include_router(users.router)
app.include_router(sites.router)
app.include_router(visits.router)
app.include_router(guests.router)

@app.on_event("startup")
def on_startup():
    utils.create_db_and_tables()
    crud.create_su()

@app.get("/")
async def root():
    return {"message": "Pulsse spalsh screen!"}

@app.post("/login")
def login_for_access_token(
    form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token:
    user = crud.authenticate_user(form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = utils.create_access_token(
        data={"sub": user.username, "is_superuser": user.is_su}, expires_delta=access_token_expires
    )
    return Token(access_token=access_token, token_type="bearer")



@app.post("/register", response_model=UserPublicMe)
def register_user(*, session: Session = Depends(utils.get_session), user: UserCreate):
    return crud.add_user(session, user)



@app.get("/seed")
def seed_db(*,
    session: Session = Depends(utils.get_session), 
    user: Annotated[User, Depends(crud.get_current_super_user)],
    ):
    u1 = User(username="cs_admin", password=utils.get_password_hash("csa"), disabled=False)
    u2 = User(username="is_admin", password=utils.get_password_hash("isa"), disabled=False)
    u3 = User(username="cs_is_admin", password=utils.get_password_hash("csisa"), disabled=False)
    u4 = User(username="ee_admin", password=utils.get_password_hash("eea"), disabled=False)
    s1 = Site(name="CS Dept", location="MCS", in_camera="2a")
    s2 = Site(name="IS Dept", location="NRB", in_camera="AA4823505", out_camera="AB9438217")
    s3 = Site(name="EE Dept", location="MCS", in_camera="1a")
    u1.sites.append(s1)
    u2.sites.append(s2)
    u3.sites.extend([s1, s2])
    u4.sites.append(s3)
    session.add(u1)
    session.add(u2)
    session.add(u3)
    session.add(u4)
    session.add(s1)
    session.add(s2)
    session.add(s3)
    session.add(Guest(name="Arsalan", vector="9874676132", is_female=False,site_id=1))
    session.add(Guest(name="Zubeela", vector="8564245686", is_female=True, site_id=1))
    session.add(Guest(name="Zarnaab", vector="5458615856", is_female=False,site_id=2))
    session.add(Guest(name="Sarmaad", vector="1265645865", is_female=False,site_id=3))
    session.add(Guest(name="g1", vector="11111"))
    session.add(Guest(name="g2", vector="11112"))
    session.add(Guest(name="g3", vector="11113"))
    session.add(Guest(name="g4", vector="11114"))
    session.add(Guest(name="g5", vector="11115"))
    session.add(Guest(name="g6", vector="11116"))
    session.add(Guest(name="g7", vector="11117"))
    session.add(Guest(name="g8", vector="11118"))
    session.add(Guest(name="g9", vector="11119"))
    session.add(Guest(name="gA", vector="11120"))
    session.add(Guest(name="gB", vector="11121"))
    session.add(Visit(time_in=utils.diff_time_stamp(1).time(), time_out=utils.diff_time_stamp(5).time(), is_group=False, is_female=False, is_new=False, site_id=1, guest_id=1))
    session.add(Visit(time_in=utils.diff_time_stamp(3).time(), time_out=utils.diff_time_stamp(15).time(), is_group=True, is_female=False, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(4).time(), time_out=utils.diff_time_stamp(10).time(), is_group=True, is_female=False, site_id=1))
    session.add(Visit(time_in=utils.diff_time_stamp(5).time(), time_out=utils.diff_time_stamp(8).time(), is_group=False, is_female=True, is_new=False, site_id=1, guest_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(8).time(), time_out=utils.diff_time_stamp(6).time(), is_group=True, is_female=False, is_new=False, site_id=2, guest_id=1))
    session.add(Visit(time_in=utils.diff_time_stamp(10).time(), time_out=utils.diff_time_stamp(4).time(), is_group=True, is_female=False, is_new=False, site_id=2, guest_id=3))
    session.add(Visit(time_in=utils.diff_time_stamp(12).time(), time_out=utils.diff_time_stamp(15).time(), is_group=False, is_female=False, is_new=False, site_id=2, guest_id=4))
    session.add(Visit(time_in=utils.diff_time_stamp(18).time(), time_out=utils.diff_time_stamp(2).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(20).time(), time_out=utils.diff_time_stamp(6).time(), is_group=False, is_female=True, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(25).time(), time_out=utils.diff_time_stamp(4).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(29).time(), time_out=utils.diff_time_stamp(11).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(33).time(), time_out=utils.diff_time_stamp(13).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(40).time(), time_out=utils.diff_time_stamp(18).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(42).time(), time_out=utils.diff_time_stamp(4).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(43).time(), time_out=utils.diff_time_stamp(5).time(), is_group=False, is_female=True, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(43).time(), time_out=utils.diff_time_stamp(8).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(44).time(), time_out=utils.diff_time_stamp(7).time(), is_group=True, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(44).time(), time_out=utils.diff_time_stamp(7).time(), is_group=True, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(46).time(), time_out=utils.diff_time_stamp(9).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(49).time(), time_out=utils.diff_time_stamp(9).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(50).time(), time_out=utils.diff_time_stamp(8).time(), is_group=False, is_female=True, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(55).time(), time_out=utils.diff_time_stamp(6).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(59).time(), time_out=utils.diff_time_stamp(9).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(62).time(), time_out=utils.diff_time_stamp(11).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(63).time(), time_out=utils.diff_time_stamp(9).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(65).time(), time_out=utils.diff_time_stamp(10).time(), is_group=True, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(65).time(), time_out=utils.diff_time_stamp(10).time(), is_group=True, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(68).time(), time_out=utils.diff_time_stamp(9).time(), is_group=False, is_female=False, is_new=True, site_id=2))
    session.add(Visit(time_in=utils.diff_time_stamp(70).time(), time_out=utils.diff_time_stamp(7).time(), is_group=False, is_female=True, is_new=True, site_id=2))
    session.commit()

    return {"message": "DB Seeded!"}



# @app.get("/feed_url/")
# def get_feed(*,
#     session: Session = Depends(utils.get_session), 
#     user: Annotated[User, Depends(crud.get_current_super_user)],
#     ):