-
Notifications
You must be signed in to change notification settings - Fork 0
/
pages.py
114 lines (86 loc) · 2.88 KB
/
pages.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
import json
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from db.models import ProjectModel, ProjectTable, UserModel
from db.project import project_get
from db.user import user_get
from deps import user_by_token
from shared import settings, sqlx
templates = Jinja2Templates(
directory=settings.base_dir / 'mark/'
)
router = APIRouter(
include_in_schema=False,
)
@router.get('/', response_class=HTMLResponse)
async def index(request: Request):
return templates.TemplateResponse(
'home/index.html',
{'request': request, 'user': await user_by_token(request)}
)
@router.get('/admin/{_:path}', response_class=HTMLResponse)
async def admin(request: Request):
with open(settings.base_dir / 'static/dash/index.html', 'r') as f:
return f.read()
# @router.get('/dash/{_:path}', response_class=HTMLResponse)
# async def dash(request: Request):
# with open(settings.base_dir / 'static/dash/index.html', 'r') as f:
# return f.read()
@router.get('/about', response_class=HTMLResponse)
async def about(request: Request):
return templates.TemplateResponse(
'about/index.html',
{'request': request}
)
@router.get('/cyprus', response_class=HTMLResponse)
async def cyprus(request: Request):
return templates.TemplateResponse(
'cyprus/index.html',
{'request': request}
)
@router.get('/education', response_class=HTMLResponse)
async def education(request: Request):
return templates.TemplateResponse(
'education/index.html',
{'request': request}
)
@router.get('/projects/', response_class=HTMLResponse)
async def projects(request: Request, q: str = '', page: int = 0):
search = ''
values = {}
if q:
search = 'WHERE title LIKE :query'
values['query'] = '%' + q + '%'
rows = await sqlx.fetch_all(
f'''
SELECT * from projects {search}
LIMIT {settings.page_size} OFFSET {page * settings.page_size}
''',
values
)
result = []
for row in rows:
args = dict(row)
args['features'] = json.loads(args['features'])
args['prices'] = json.loads(args['prices'])
args['images'] = json.loads(args['images'])
result.append(ProjectModel(**args))
return templates.TemplateResponse(
'projects/index.html',
{
'request': request,
'search_query': q,
'page': page,
'projects': result
}
)
@router.get('/projects/{project_id}/', response_class=HTMLResponse)
async def project(request: Request, project_id: int):
project = await project_get(project_id)
if project is None:
return HTMLResponse('not found', 404)
return templates.TemplateResponse(
'project/index.html',
{'request': request, 'project': project}
)