-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcursor.py
More file actions
153 lines (124 loc) · 4.67 KB
/
cursor.py
File metadata and controls
153 lines (124 loc) · 4.67 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
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
import json
from pathlib import Path
from typing import List, Optional, Tuple
from app.database.models import User, UserSettings
from app.dependencies import CURSORS_PATH, get_db, templates
from app.routers.profile import get_placeholder_user
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
router = APIRouter(
prefix="/cursor",
tags=["cursor"],
responses={404: {"description": "Not found"}},
)
@router.get("/settings")
def cursor_settings(
request: Request, 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(get_placeholder_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.
"""
user = get_user(session, "new_user", user)
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_cursor")
async def load_cursor(session: Session = Depends(get_db),) -> RedirectResponse:
"""loads cursors according to cursor settings.
Args:
session (Session): the database.
Returns:
RedirectResponse: redirect the user to the homepage.
"""
primary_cursor, secondary_cursor = get_cursor_settings(session)
return json.dumps(
{
"primary_cursor": primary_cursor,
"secondary_cursor": secondary_cursor,
})
def get_cursor_settings(
session: Session, user_id: int = 1
) -> Tuple[Optional[List[str]], Optional[int], Optional[str], Optional[int]]:
"""Retrieves cursor settings from the database.
Args:
session (Session): the database.
user_id (int, optional): the users' id.
Returns:
Tuple[str, Optional[List[str]], Optional[int],
str, Optional[str], Optional[int]]: the cursor settings.
"""
primary_cursor, secondary_cursor = None, None
cursor_settings = session.query(
UserSettings).filter_by(user_id=user_id).first()
if cursor_settings:
primary_cursor = cursor_settings.primary_cursor
secondary_cursor = cursor_settings.secondary_cursor
return primary_cursor, secondary_cursor
def save_cursor_settings(
session: Session, user: User, cursor_choices: List[str]):
"""Saves cursor choices in the db.
Args:
session (Session): the database.
user (User): current user.
cursor_choices (List[str]): primary and secondary cursors.
"""
cursor_settings = session.query(UserSettings).filter_by(
user_id=user.id).first()
if cursor_settings:
session.query(UserSettings).filter_by(
user_id=cursor_settings.user_id).update(cursor_choices)
session.commit()
else:
cursor_settings = UserSettings(user_id=user.id, **cursor_choices)
session.add(cursor_settings)
session.commit()
def get_user(session: Session, user_name: str, new_user: User) -> User:
"""returns a user with user_name, or new user if he doesn't exist.
Args:
session (Session): the database.
user_name (str): name of the user to look for in the db.
new_user (User): new user to insert in case user_name isn't found.
Returns:
User: the user.
"""
user = session.query(User).filter_by(username=user_name).first()
if not user:
session.add(new_user)
session.commit()
user = session.query(User).filter_by(
username=new_user.username).first()
return user