-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathfeatures.py
217 lines (171 loc) · 5.72 KB
/
features.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from functools import wraps
from typing import Dict, List
from fastapi import Depends, Request
from sqlalchemy.orm import Session
from sqlalchemy.sql import exists
from starlette.responses import RedirectResponse
from app.database.models import Feature, UserFeature
from app.dependencies import SessionLocal, get_db
from app.internal.features_index import features, icons
from app.internal.security.dependencies import current_user
from app.internal.security.ouath2 import get_authorization_cookie
from app.internal.utils import create_model
def feature_access_filter(call_next):
@wraps(call_next)
async def wrapper(*args, **kwargs):
request = kwargs["request"]
if request.headers["user-agent"] == "testclient":
# in case it's a unit test.
return await call_next(*args, **kwargs)
# getting the url route path for matching with the database.
route = "/" + str(request.url).replace(str(request.base_url), "")
# getting access status.
access = await is_access_allowd(route=route, request=request)
if access:
# in case the feature is enabled or access is allowed.
return await call_next(*args, **kwargs)
elif "referer" not in request.headers:
# in case request come straight from address bar in browser.
return RedirectResponse(url="/")
# in case the feature is disabled or access isn't allowed.
return RedirectResponse(url=request.headers["referer"])
return wrapper
def create_features_at_startup(session: Session) -> bool:
for feat in features:
if not is_feature_exists(feature=feat, session=session):
icon = icons.get(feat["name"])
create_feature(**feat, icon=icon, db=session)
return True
def is_user_has_feature(
session: Session,
feature_id: int,
user_id: int,
) -> bool:
return session.query(
exists()
.where(UserFeature.user_id == user_id)
.where(UserFeature.feature_id == feature_id),
).scalar()
def delete_feature(
feature: Feature,
session: Session = Depends(get_db),
) -> None:
session.query(UserFeature).filter_by(feature_id=feature.id).delete()
session.query(Feature).filter_by(id=feature.id).delete()
session.commit()
def is_feature_exists(feature: Dict[str, str], session: Session) -> bool:
is_exists = session.query(
exists()
.where(Feature.name == feature["name"])
.where(Feature.route == feature["route"]),
).scalar()
return is_exists
def update_feature(
feature: Feature,
feature_dict: Dict[str, str],
session: Session = Depends(get_db),
) -> Feature:
feature.name = feature_dict["name"]
feature.route = feature_dict["route"]
feature.description = feature_dict["description"]
feature.creator = feature_dict["creator"]
feature.template = feature_dict["template"]
icon = icons.get(feature.name)
if icon is None:
icon = "extension-puzzle"
feature.icon = icon
session.commit()
return feature
async def is_access_allowd(request: Request, route: str) -> bool:
session = SessionLocal()
# Get current user.
# Note: can't use dependency beacause its designed for routes only.
# current_user return schema not an db model.
jwt = await get_authorization_cookie(request=request)
user = await current_user(request=request, jwt=jwt, db=session)
feature = session.query(Feature).filter_by(route=route).first()
if feature is None:
# in case there is no feature exists in the database that match the
# route that gived by to the request.
return True
user_feature = session.query(
exists().where(
(UserFeature.feature_id == feature.id)
& (UserFeature.user_id == user.user_id),
),
).scalar()
print(user_feature)
return user_feature
def create_feature(
db: Session,
name: str,
route: str,
description: str,
creator: str = None,
icon: str = None,
template: str = None,
) -> Feature:
"""Creates a feature."""
db = SessionLocal()
if icon is None:
icon = "extension-puzzle"
return create_model(
db,
Feature,
name=name,
route=route,
creator=creator,
description=description,
icon=icon,
template=template,
)
def create_user_feature_association(
db: Session,
feature_id: int,
user_id: int,
is_enable: bool,
) -> UserFeature:
"""Creates an association."""
add_follower(feature_id=feature_id, session=db)
return create_model(
db,
UserFeature,
user_id=user_id,
feature_id=feature_id,
is_enable=is_enable,
)
def get_user_installed_features(
user_id: int,
session: Session = Depends(get_db),
) -> List[Feature]:
return (
session.query(Feature)
.join(UserFeature)
.filter(UserFeature.user_id == user_id)
.all()
)
def get_user_uninstalled_features(
user_id: int,
session: Session = Depends(get_db),
) -> List[Feature]:
return (
session.query(Feature)
.filter(
Feature.id.notin_(
session.query(UserFeature.feature_id).filter(
UserFeature.user_id == user_id,
),
),
)
.all()
)
def remove_follower(feature_id: int, session: SessionLocal) -> None:
feat = session.query(Feature).filter_by(id=feature_id).first()
feat.followers -= 1
if feat.followers < 0:
feat.followers = 0
session.commit()
def add_follower(feature_id: int, session: SessionLocal) -> None:
feat = session.query(Feature).filter_by(id=feature_id).first()
feat.followers += 1
session.commit()