-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
113 lines (86 loc) · 3.4 KB
/
app.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
from typing import Annotated
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi import FastAPI, Request, Response, HTTPException, Form, BackgroundTasks
import pytz
import uvicorn
from persiantools import digits
from persiantools.jdatetime import JalaliDateTime
from logger import logger
from excel_handler import write_excel, remove_file
from config import VEHICLES_DATA, POSITIONS_DATA, FinalData, generate_report_file_name
app = FastAPI()
# load static files
app.mount('/static', StaticFiles(directory='static'), name='static')
# load templates
templates = Jinja2Templates(directory='templates')
@app.exception_handler(404)
async def not_found_exception_handler(request: Request, exc: HTTPException) -> Response:
"""Custom exception handler for 404 Not Found."""
return templates.TemplateResponse('layouts/not-found.html', {'request': request})
@app.get('/')
async def index(request: Request):
context = {
'request': request,
'positions_data': POSITIONS_DATA,
}
logger.info(f'request: {request.url}')
return templates.TemplateResponse('pages/index.html', context)
@app.post('/report')
async def report(
request: Request,
address: Annotated[str, Form()],
position: Annotated[str, Form()],
):
logger.info(f'request: {request.url}')
start_datetime = JalaliDateTime.now(pytz.timezone('Asia/Tehran'))
context = {
'request': request,
'address': address,
'position': position,
'start_timestamp': start_datetime.to_gregorian().timestamp(),
'start_time': start_datetime.strftime('%H:%M'),
'vehicles_data': VEHICLES_DATA,
}
return templates.TemplateResponse('pages/report.html', context)
@app.post('/export')
async def export(
request: Request,
data: FinalData,
):
data = data.model_dump(mode='json')
start_datetime = JalaliDateTime.fromtimestamp(data['start_timestamp'], pytz.timezone("Asia/Tehran"))
context = {
'request': request,
'data': data,
'report_file_name': generate_report_file_name(start_datetime),
}
return templates.TemplateResponse('pages/export.html', context)
@app.post('/download')
async def download_excel_file(
request: Request,
data: FinalData,
background_tasks: BackgroundTasks
):
logger.info(f'request: {request.url}')
logger.info(f'body: {data.model_dump_json(indent=2)}')
data = data.model_dump(mode='json')
start_datetime = JalaliDateTime.fromtimestamp(data['start_timestamp'], pytz.timezone("Asia/Tehran"))
end_datetime = JalaliDateTime.now(pytz.timezone('Asia/Tehran'))
main_data_rows = [
[VEHICLES_DATA[vehicle]['alias'], data[vehicle]['right'], data[vehicle]['straight'], data[vehicle]['left']]
for vehicle in VEHICLES_DATA
]
general_info = [
data['address'],
POSITIONS_DATA[data['position']],
digits.en_to_fa(start_datetime.strftime('%Y/%m/%d')),
digits.en_to_fa(start_datetime.strftime('%H:%M')),
digits.en_to_fa(end_datetime.strftime('%H:%M'))
]
path = write_excel(general_info, main_data_rows)
background_tasks.add_task(remove_file, path)
return FileResponse(path, filename=generate_report_file_name(start_datetime))
if __name__ == '__main__':
uvicorn.run('app:app', host='0.0.0.0', port=5000, reload=True)