-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (86 loc) · 3.18 KB
/
main.py
File metadata and controls
110 lines (86 loc) · 3.18 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
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
from fastapi import Depends, FastAPI, Request
from fastapi.staticfiles import StaticFiles
from sqlalchemy.orm import Session
from starlette.responses import RedirectResponse
from app import config
from app.database import engine, models
from app.dependencies import (
get_db, logger, MEDIA_PATH, STATIC_PATH, templates, SessionLocal)
from app.internal import (
daily_quotes, json_data_loader, features as internal_features)
from app.internal.languages import set_ui_language
from app.routers.salary import routes as salary
def create_tables(engine, psql_environment):
if 'sqlite' in str(engine.url) and psql_environment:
raise models.PSQLEnvironmentError(
"You're trying to use PSQL features on SQLite env.\n"
"Please set app.config.PSQL_ENVIRONMENT to False "
"and run the app again."
)
else:
models.Base.metadata.create_all(bind=engine)
create_tables(engine, config.PSQL_ENVIRONMENT)
app = FastAPI()
app.mount("/static", StaticFiles(directory=STATIC_PATH), name="static")
app.mount("/media", StaticFiles(directory=MEDIA_PATH), name="media")
app.logger = logger
json_data_loader.load_to_db(next(get_db()))
# This MUST come before the app.routers imports.
set_ui_language()
from app.routers import ( # noqa: E402
agenda, calendar, categories, celebrity, currency, dayview,
email, event, export, four_o_four, invitation, profile, search,
weekview, telegram, whatsapp, features
)
json_data_loader.load_to_db(next(get_db()))
routers_to_include = [
agenda.router,
calendar.router,
categories.router,
celebrity.router,
currency.router,
dayview.router,
weekview.router,
email.router,
event.router,
export.router,
four_o_four.router,
invitation.router,
profile.router,
salary.router,
search.router,
telegram.router,
whatsapp.router,
features.router,
]
for router in routers_to_include:
app.include_router(router)
@app.middleware("http")
async def filter_access_to_features(request: Request, call_next):
# getting the url route path for matching with the database.
route = '/' + str(request.url).replace(str(request.base_url), '')
# getting access status.
is_enabled = internal_features.is_feature_enabled(route=route)
if is_enabled:
# in case the feature is enabled or access is allowed.
return await call_next(request)
elif 'referer' not in request.headers:
# in case request come straight from address bar in browser.
return RedirectResponse(url=app.url_path_for('home'))
# in case the feature is disabled or access isn't allowed.
return RedirectResponse(url=request.headers['referer'])
@app.on_event("startup")
async def startup_event():
session = SessionLocal()
internal_features.create_features_at_startup(session=session)
session.close()
# TODO: I add the quote day to the home page
# until the relavent calendar view will be developed.
@app.get("/")
@logger.catch()
async def home(request: Request, db: Session = Depends(get_db)):
quote = daily_quotes.quote_per_day(db)
return templates.TemplateResponse("index.html", {
"request": request,
"quote": quote,
})