-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcursor.py
103 lines (85 loc) · 2.76 KB
/
cursor.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
import json
from pathlib import Path
from fastapi import APIRouter, Depends, Form, Request
from sqlalchemy.orm.session import Session
from starlette.responses import RedirectResponse
from starlette.status import HTTP_302_FOUND
from app.database.models import User
from app.dependencies import CURSORS_PATH, get_db, templates
from app.internal.cursor import get_cursor_settings, save_cursor_settings
from app.internal.security.dependencies import current_user
router = APIRouter(
prefix="/cursor",
tags=["cursor"],
responses={404: {"description": "Not found"}},
)
@router.get("/settings")
def cursor_settings(
request: Request,
user: User = Depends(current_user),
session: Session = Depends(get_db),
) -> templates.TemplateResponse:
"""A route to the cursor settings.
Args:
request (Request): the http request.
session (Session): the database.
Returns:
templates.TemplateResponse: renders the cursor_settings.html page
with the relevant information.
"""
cursors = ["default"] + [
path.stem for path in Path(CURSORS_PATH).glob("**/*.cur")
]
return templates.TemplateResponse(
"cursor_settings.html",
{
"request": request,
"cursors": cursors,
},
)
@router.post("/settings")
async def get_cursor_choices(
session: Session = Depends(get_db),
user: User = Depends(current_user),
primary_cursor: str = Form(...),
secondary_cursor: str = Form(...),
) -> RedirectResponse:
"""The form in which the user choses primary and secondary
cursors.
Args:
session (Session, optional): the database.
user (User, optional): [description]. temp user.
primary_cursor (str, optional): name of the primary cursor.
the primary cursor.
secondary_cursor (str, optional): name of the secondary cursor.
Returns:
RedirectResponse: redirects to the homepage.
"""
cursor_choices = {
"primary_cursor": primary_cursor,
"secondary_cursor": secondary_cursor,
}
save_cursor_settings(session, user, cursor_choices)
return RedirectResponse("/", status_code=HTTP_302_FOUND)
@router.get("/load")
async def load_cursor(
session: Session = Depends(get_db),
user: User = Depends(current_user),
) -> RedirectResponse:
"""loads cursors according to cursor settings.
Args:
session (Session): the database.
user (User): the user.
Returns:
RedirectResponse: redirect the user to the homepage.
"""
primary_cursor, secondary_cursor = get_cursor_settings(
session,
user.user_id,
)
return json.dumps(
{
"primary_cursor": primary_cursor,
"secondary_cursor": secondary_cursor,
},
)