-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmain.py
180 lines (157 loc) · 4.34 KB
/
main.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from fastapi import Depends, FastAPI, Request, status
from fastapi.openapi.docs import (
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from fastapi.staticfiles import StaticFiles
from sqlalchemy.orm import Session
import app.internal.features as internal_features
from app import config
from app.database import engine, models
from app.dependencies import (
MEDIA_PATH,
SOUNDS_PATH,
STATIC_PATH,
UPLOAD_PATH,
SessionLocal,
get_db,
logger,
templates,
)
from app.internal import daily_quotes, json_data_loader
from app.internal.languages import set_ui_language
from app.internal.security.dependencies import get_jinja_current_user
from app.internal.security.ouath2 import auth_exception_handler
from app.routers.notes import notes
from app.routers.salary import routes as salary
from app.utils.extending_openapi import custom_openapi
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(title="Pylander", docs_url=None)
app.mount("/static", StaticFiles(directory=STATIC_PATH), name="static")
app.mount("/media", StaticFiles(directory=MEDIA_PATH), name="media")
app.mount(
"/event_images",
StaticFiles(directory=UPLOAD_PATH),
name="event_images",
)
app.mount("/static/tracks", StaticFiles(directory=SOUNDS_PATH), name="sounds")
app.logger = logger
app.add_exception_handler(status.HTTP_401_UNAUTHORIZED, auth_exception_handler)
templates.env.globals["jinja_current_user"] = get_jinja_current_user
# This MUST come before the app.routers imports.
set_ui_language()
from app.routers import ( # noqa: E402
about_us,
agenda,
audio,
calendar,
categories,
celebrity,
credits,
currency,
dayview,
email,
event,
export,
features,
four_o_four,
friendview,
google_connect,
joke,
login,
logout,
meds,
menstrual_predictor,
notification,
profile,
register,
reset_password,
search,
settings,
telegram,
todo_list,
user,
weekview,
weight,
whatsapp,
)
json_data_loader.load_to_database(next(get_db()))
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
return get_swagger_ui_html(
openapi_url=app.openapi_url,
title=app.title + " - Swagger UI",
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
swagger_js_url="/static/swagger/swagger-ui-bundle.js",
swagger_css_url="/static/swagger/swagger-ui.css",
)
@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
return get_swagger_ui_oauth2_redirect_html()
routers_to_include = [
about_us.router,
agenda.router,
audio.router,
calendar.router,
categories.router,
celebrity.router,
credits.router,
currency.router,
dayview.router,
email.router,
event.router,
export.router,
features.router,
four_o_four.router,
friendview.router,
google_connect.router,
joke.router,
login.router,
logout.router,
meds.router,
menstrual_predictor.router,
notes.router,
notification.router,
profile.router,
register.router,
reset_password.router,
salary.router,
search.router,
settings.router,
telegram.router,
todo_list.router,
user.router,
weekview.router,
weight.router,
whatsapp.router,
]
for router in routers_to_include:
app.include_router(router)
@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 relevant calendar view will be developed.
@app.get("/", include_in_schema=False)
@logger.catch()
async def home(request: Request, db: Session = Depends(get_db)):
quote = daily_quotes.get_quote_of_day(db)
return templates.TemplateResponse(
"index.html",
{
"request": request,
"quote": quote,
},
)
custom_openapi(app)