gitscope
https://dev.kronoz.win/r/marzneshin.git
app/db/crud.py · v0.3.1
Source
1 12b255b Dawsh 2y ago import json
2 34a7612 Dawsh 2y ago import secrets
3 f446990 Dawsh 2y ago from datetime import datetime, timedelta
4 a47eb4c Dawsh 2y ago from enum import Enum
5 2de9d72 Dawsh 2y ago from typing import List, Optional, Tuple, Union
6 a47eb4c Dawsh 2y ago
7 512d183 Mrdn 2y ago from sqlalchemy import and_, delete, update, select
8 a47eb4c Dawsh 2y ago from sqlalchemy.orm import Session
9 a47eb4c Dawsh 2y ago
10 01cdf2e Dawsh 2y ago from app.db.models import (
11 01cdf2e Dawsh 2y ago JWT,
12 01cdf2e Dawsh 2y ago TLS,
13 01cdf2e Dawsh 2y ago Admin,
14 01cdf2e Dawsh 2y ago Node,
15 01cdf2e Dawsh 2y ago NodeUsage,
16 01cdf2e Dawsh 2y ago NodeUserUsage,
17 01cdf2e Dawsh 2y ago NotificationReminder,
18 01cdf2e Dawsh 2y ago InboundHost,
19 01cdf2e Dawsh 2y ago Service,
20 01cdf2e Dawsh 2y ago Inbound,
21 01cdf2e Dawsh 2y ago System,
22 01cdf2e Dawsh 2y ago User,
23 01cdf2e Dawsh 2y ago )
24 a47eb4c Dawsh 2y ago from app.models.admin import AdminCreate, AdminModify, AdminPartialModify
25 f1ab16c Dawsh 2y ago from app.models.node import (
26 f1ab16c Dawsh 2y ago NodeCreate,
27 f1ab16c Dawsh 2y ago NodeModify,
28 f1ab16c Dawsh 2y ago NodeStatus,
29 f1ab16c Dawsh 2y ago NodeUsageResponse,
30 f1ab16c Dawsh 2y ago )
31 a47eb4c Dawsh 2y ago from app.models.proxy import InboundHost as InboundHostModify
32 2de9d72 Dawsh 2y ago from app.models.service import Service as ServiceModify, ServiceCreate
33 01cdf2e Dawsh 2y ago from app.models.user import (
34 01cdf2e Dawsh 2y ago ReminderType,
35 01cdf2e Dawsh 2y ago UserCreate,
36 d377c4c Dawsh 2y ago UserDataUsageResetStrategy,
37 01cdf2e Dawsh 2y ago UserModify,
38 01cdf2e Dawsh 2y ago UserStatus,
39 01cdf2e Dawsh 2y ago UserUsageResponse,
40 d377c4c Dawsh 2y ago UserExpireStrategy,
41 f1ab16c Dawsh 2y ago )
42 a47eb4c Dawsh 2y ago
43 a47eb4c Dawsh 2y ago
44 a47eb4c Dawsh 2y ago def add_default_hosts(db: Session, inbounds: List[Inbound]):
45 01cdf2e Dawsh 2y ago hosts = [
46 01cdf2e Dawsh 2y ago InboundHost(
47 01cdf2e Dawsh 2y ago remark="🚀 Marz ({USERNAME}) [{PROTOCOL} - {TRANSPORT}]",
48 01cdf2e Dawsh 2y ago address="{SERVER_IP}",
49 01cdf2e Dawsh 2y ago inbound=i,
50 01cdf2e Dawsh 2y ago )
51 01cdf2e Dawsh 2y ago for i in inbounds
52 01cdf2e Dawsh 2y ago ]
53 a47eb4c Dawsh 2y ago db.add_all(hosts)
54 a47eb4c Dawsh 2y ago db.commit()
55 a47eb4c Dawsh 2y ago
56 a47eb4c Dawsh 2y ago
57 a47eb4c Dawsh 2y ago def assure_node_inbounds(db: Session, inbounds: List[Inbound], node_id: int):
58 01cdf2e Dawsh 2y ago current_tags = [
59 01cdf2e Dawsh 2y ago i[0]
60 01cdf2e Dawsh 2y ago for i in db.execute(
61 01cdf2e Dawsh 2y ago select(Inbound.tag).filter(Inbound.node_id == node_id)
62 01cdf2e Dawsh 2y ago ).all()
63 01cdf2e Dawsh 2y ago ]
64 512d183 Mrdn 2y ago updated_tags = set(i.tag for i in list(inbounds))
65 512d183 Mrdn 2y ago inbound_additions, tag_deletions = list(), set()
66 512d183 Mrdn 2y ago for tag in current_tags:
67 512d183 Mrdn 2y ago if tag not in updated_tags:
68 512d183 Mrdn 2y ago tag_deletions.add(tag)
69 01cdf2e Dawsh 2y ago db_inbounds = db.query(Inbound).where(
70 01cdf2e Dawsh 2y ago and_(Inbound.node_id == node_id, Inbound.tag.in_(tag_deletions))
71 01cdf2e Dawsh 2y ago )
72 512d183 Mrdn 2y ago for i in db_inbounds:
73 512d183 Mrdn 2y ago db.delete(i)
74 512d183 Mrdn 2y ago
75 512d183 Mrdn 2y ago for inb in inbounds:
76 512d183 Mrdn 2y ago if inb.tag in current_tags:
77 01cdf2e Dawsh 2y ago stmt = (
78 01cdf2e Dawsh 2y ago update(Inbound)
79 f1ab16c Dawsh 2y ago .where(
80 f1ab16c Dawsh 2y ago and_(Inbound.node_id == node_id, Inbound.tag == inb.tag)
81 f1ab16c Dawsh 2y ago )
82 f1ab16c Dawsh 2y ago .values(
83 f1ab16c Dawsh 2y ago protocol=json.loads(inb.config)["protocol"],
84 f1ab16c Dawsh 2y ago config=inb.config,
85 f1ab16c Dawsh 2y ago )
86 a47eb4c Dawsh 2y ago )
87 512d183 Mrdn 2y ago db.execute(stmt)
88 512d183 Mrdn 2y ago else:
89 512d183 Mrdn 2y ago inbound_additions.append(inb)
90 01cdf2e Dawsh 2y ago new_inbounds = [
91 01cdf2e Dawsh 2y ago Inbound(
92 01cdf2e Dawsh 2y ago tag=inb.tag,
93 01cdf2e Dawsh 2y ago protocol=json.loads(inb.config)["protocol"],
94 01cdf2e Dawsh 2y ago config=inb.config,
95 01cdf2e Dawsh 2y ago node_id=node_id,
96 01cdf2e Dawsh 2y ago )
97 01cdf2e Dawsh 2y ago for inb in inbound_additions
98 01cdf2e Dawsh 2y ago ]
99 a47eb4c Dawsh 2y ago db.add_all(new_inbounds)
100 a47eb4c Dawsh 2y ago db.flush()
101 a47eb4c Dawsh 2y ago for i in new_inbounds:
102 2de9d72 Dawsh 2y ago db.refresh(i)
103 a47eb4c Dawsh 2y ago add_default_hosts(db, new_inbounds)
104 a47eb4c Dawsh 2y ago db.commit()
105 a47eb4c Dawsh 2y ago
106 a47eb4c Dawsh 2y ago
107 01cdf2e Dawsh 2y ago def get_node_users(
108 f1ab16c Dawsh 2y ago db: Session,
109 d377c4c Dawsh 2y ago node_id: int,
110 01cdf2e Dawsh 2y ago ):
111 01cdf2e Dawsh 2y ago query = (
112 01cdf2e Dawsh 2y ago db.query(User.id, User.username, User.key, Inbound)
113 01cdf2e Dawsh 2y ago .distinct()
114 01cdf2e Dawsh 2y ago .join(Inbound.services)
115 01cdf2e Dawsh 2y ago .join(Service.users)
116 01cdf2e Dawsh 2y ago .filter(Inbound.node_id == node_id)
117 d377c4c Dawsh 2y ago .filter(User.activated == True)
118 01cdf2e Dawsh 2y ago )
119 a47eb4c Dawsh 2y ago return query.all()
120 a47eb4c Dawsh 2y ago
121 12b255b Dawsh 2y ago
122 a47eb4c Dawsh 2y ago def get_user_hosts(db: Session, user_id: int):
123 01cdf2e Dawsh 2y ago return (
124 01cdf2e Dawsh 2y ago db.query(InboundHost)
125 01cdf2e Dawsh 2y ago .distinct()
126 01cdf2e Dawsh 2y ago .join(User.services)
127 01cdf2e Dawsh 2y ago .join(Service.inbounds)
128 01cdf2e Dawsh 2y ago .join(Inbound.hosts)
129 01cdf2e Dawsh 2y ago .filter(User.id == user_id)
130 01cdf2e Dawsh 2y ago .all()
131 01cdf2e Dawsh 2y ago )
132 a47eb4c Dawsh 2y ago
133 12b255b Dawsh 2y ago
134 a47eb4c Dawsh 2y ago def get_inbound_hosts(db: Session, inbound_id: int) -> List[InboundHost]:
135 f1ab16c Dawsh 2y ago return (
136 f1ab16c Dawsh 2y ago db.query(InboundHost)
137 f1ab16c Dawsh 2y ago .filter(InboundHost.inbound_id == inbound_id)
138 f1ab16c Dawsh 2y ago .all()
139 f1ab16c Dawsh 2y ago )
140 a47eb4c Dawsh 2y ago
141 12b255b Dawsh 2y ago
142 a47eb4c Dawsh 2y ago def get_all_inbounds(db: Session):
143 a47eb4c Dawsh 2y ago return db.query(Inbound).all()
144 a47eb4c Dawsh 2y ago
145 a47eb4c Dawsh 2y ago
146 2de9d72 Dawsh 2y ago def get_inbound(db: Session, inbound_id: int) -> Inbound | None:
147 2de9d72 Dawsh 2y ago return db.query(Inbound).filter(Inbound.id == inbound_id).first()
148 a47eb4c Dawsh 2y ago
149 a47eb4c Dawsh 2y ago
150 2de9d72 Dawsh 2y ago def get_host(db: Session, host_id) -> InboundHost:
151 2de9d72 Dawsh 2y ago return db.query(InboundHost).filter(InboundHost.id == host_id).first()
152 a47eb4c Dawsh 2y ago
153 a47eb4c Dawsh 2y ago
154 a47eb4c Dawsh 2y ago def add_host(db: Session, inbound: Inbound, host: InboundHostModify):
155 2de9d72 Dawsh 2y ago host = InboundHost(
156 2de9d72 Dawsh 2y ago remark=host.remark,
157 2de9d72 Dawsh 2y ago address=host.address,
158 2de9d72 Dawsh 2y ago port=host.port,
159 2de9d72 Dawsh 2y ago path=host.path,
160 2de9d72 Dawsh 2y ago sni=host.sni,
161 2de9d72 Dawsh 2y ago host=host.host,
162 2de9d72 Dawsh 2y ago inbound=inbound,
163 2de9d72 Dawsh 2y ago security=host.security,
164 2de9d72 Dawsh 2y ago alpn=host.alpn,
165 01cdf2e Dawsh 2y ago fingerprint=host.fingerprint,
166 0b152b5 Mrdn 2y ago fragment=host.fragment.model_dump() if host.fragment else None,
167 0b152b5 Mrdn 2y ago mux=host.mux,
168 a47eb4c Dawsh 2y ago )
169 2de9d72 Dawsh 2y ago inbound.hosts.append(host)
170 a47eb4c Dawsh 2y ago db.commit()
171 2de9d72 Dawsh 2y ago db.refresh(host)
172 2de9d72 Dawsh 2y ago return host
173 2de9d72 Dawsh 2y ago
174 2de9d72 Dawsh 2y ago
175 2de9d72 Dawsh 2y ago def update_host(db: Session, db_host: InboundHost, host: InboundHostModify):
176 2de9d72 Dawsh 2y ago db_host.remark = host.remark
177 2de9d72 Dawsh 2y ago db_host.address = host.address
178 2de9d72 Dawsh 2y ago db_host.port = host.port
179 2de9d72 Dawsh 2y ago db_host.path = host.path
180 2de9d72 Dawsh 2y ago db_host.sni = host.sni
181 2de9d72 Dawsh 2y ago db_host.host = host.host
182 2de9d72 Dawsh 2y ago db_host.security = host.security
183 2de9d72 Dawsh 2y ago db_host.alpn = host.alpn
184 2de9d72 Dawsh 2y ago db_host.fingerprint = host.fingerprint
185 0b152b5 Mrdn 2y ago db_host.fragment = host.fragment.model_dump() if host.fragment else None
186 0b152b5 Mrdn 2y ago db_host.mux = host.mux
187 2de9d72 Dawsh 2y ago db.commit()
188 2de9d72 Dawsh 2y ago db.refresh(db_host)
189 2de9d72 Dawsh 2y ago return db_host
190 a47eb4c Dawsh 2y ago
191 a47eb4c Dawsh 2y ago
192 a47eb4c Dawsh 2y ago def get_user(db: Session, username: str):
193 a47eb4c Dawsh 2y ago return db.query(User).filter(User.username == username).first()
194 a47eb4c Dawsh 2y ago
195 a47eb4c Dawsh 2y ago
196 a47eb4c Dawsh 2y ago def get_user_by_id(db: Session, user_id: int):
197 a47eb4c Dawsh 2y ago return db.query(User).filter(User.id == user_id).first()
198 a47eb4c Dawsh 2y ago
199 a47eb4c Dawsh 2y ago
200 01cdf2e Dawsh 2y ago UsersSortingOptions = Enum(
201 01cdf2e Dawsh 2y ago "UsersSortingOptions",
202 01cdf2e Dawsh 2y ago {
203 01cdf2e Dawsh 2y ago "username": User.username.asc(),
204 01cdf2e Dawsh 2y ago "used_traffic": User.used_traffic.asc(),
205 01cdf2e Dawsh 2y ago "data_limit": User.data_limit.asc(),
206 d377c4c Dawsh 2y ago "expire": User.expire_date.asc(),
207 01cdf2e Dawsh 2y ago "created_at": User.created_at.asc(),
208 01cdf2e Dawsh 2y ago "-username": User.username.desc(),
209 01cdf2e Dawsh 2y ago "-used_traffic": User.used_traffic.desc(),
210 01cdf2e Dawsh 2y ago "-data_limit": User.data_limit.desc(),
211 d377c4c Dawsh 2y ago "-expire": User.expire_date.desc(),
212 01cdf2e Dawsh 2y ago "-created_at": User.created_at.desc(),
213 01cdf2e Dawsh 2y ago },
214 01cdf2e Dawsh 2y ago )
215 01cdf2e Dawsh 2y ago
216 01cdf2e Dawsh 2y ago
217 01cdf2e Dawsh 2y ago def get_users(
218 01cdf2e Dawsh 2y ago db: Session,
219 01cdf2e Dawsh 2y ago offset: Optional[int] = None,
220 01cdf2e Dawsh 2y ago limit: Optional[int] = None,
221 01cdf2e Dawsh 2y ago usernames: Optional[List[str]] = None,
222 01cdf2e Dawsh 2y ago sort: Optional[List[UsersSortingOptions]] = None,
223 01cdf2e Dawsh 2y ago admin: Optional[Admin] = None,
224 d377c4c Dawsh 2y ago reset_strategy: Optional[Union[UserDataUsageResetStrategy, list]] = None,
225 d377c4c Dawsh 2y ago expire_strategy: (
226 d377c4c Dawsh 2y ago UserExpireStrategy | list[UserExpireStrategy] | None
227 d377c4c Dawsh 2y ago ) = None,
228 d377c4c Dawsh 2y ago is_active: bool | None = None,
229 d377c4c Dawsh 2y ago activated: bool | None = None,
230 d377c4c Dawsh 2y ago expired: bool | None = None,
231 d377c4c Dawsh 2y ago data_limit_reached: bool | None = None,
232 01cdf2e Dawsh 2y ago ) -> Union[List[User], Tuple[List[User], int]]:
233 a47eb4c Dawsh 2y ago query = db.query(User)
234 a47eb4c Dawsh 2y ago
235 a47eb4c Dawsh 2y ago if usernames:
236 a47eb4c Dawsh 2y ago if len(usernames) == 1:
237 a47eb4c Dawsh 2y ago query = query.filter(User.username.ilike(f"%{usernames[0]}%"))
238 a47eb4c Dawsh 2y ago else:
239 a47eb4c Dawsh 2y ago query = query.filter(User.username.in_(usernames))
240 a47eb4c Dawsh 2y ago
241 a47eb4c Dawsh 2y ago if reset_strategy:
242 a47eb4c Dawsh 2y ago if isinstance(reset_strategy, list):
243 f1ab16c Dawsh 2y ago query = query.filter(
244 f1ab16c Dawsh 2y ago User.data_limit_reset_strategy.in_(reset_strategy)
245 f1ab16c Dawsh 2y ago )
246 a47eb4c Dawsh 2y ago else:
247 f1ab16c Dawsh 2y ago query = query.filter(
248 f1ab16c Dawsh 2y ago User.data_limit_reset_strategy == reset_strategy
249 f1ab16c Dawsh 2y ago )
250 a47eb4c Dawsh 2y ago
251 d377c4c Dawsh 2y ago if expire_strategy:
252 d377c4c Dawsh 2y ago if isinstance(expire_strategy, list):
253 d377c4c Dawsh 2y ago query = query.filter(User.expire_strategy.in_(expire_strategy))
254 d377c4c Dawsh 2y ago else:
255 d377c4c Dawsh 2y ago query = query.filter(User.expire_strategy == expire_strategy)
256 d377c4c Dawsh 2y ago
257 d377c4c Dawsh 2y ago if isinstance(is_active, bool):
258 d377c4c Dawsh 2y ago query = query.filter(User.is_active == is_active)
259 d377c4c Dawsh 2y ago
260 d377c4c Dawsh 2y ago if isinstance(activated, bool):
261 d377c4c Dawsh 2y ago query = query.filter(User.activated == activated)
262 d377c4c Dawsh 2y ago
263 d377c4c Dawsh 2y ago if isinstance(expired, bool):
264 d377c4c Dawsh 2y ago query = query.filter(User.expired == expired)
265 d377c4c Dawsh 2y ago
266 d377c4c Dawsh 2y ago if isinstance(data_limit_reached, bool):
267 d377c4c Dawsh 2y ago query = query.filter(User.data_limit_reached == data_limit_reached)
268 d377c4c Dawsh 2y ago
269 a47eb4c Dawsh 2y ago if admin:
270 a47eb4c Dawsh 2y ago query = query.filter(User.admin == admin)
271 a47eb4c Dawsh 2y ago
272 a47eb4c Dawsh 2y ago if sort:
273 a47eb4c Dawsh 2y ago query = query.order_by(*(opt.value for opt in sort))
274 a47eb4c Dawsh 2y ago
275 a47eb4c Dawsh 2y ago if offset:
276 a47eb4c Dawsh 2y ago query = query.offset(offset)
277 d377c4c Dawsh 2y ago
278 a47eb4c Dawsh 2y ago if limit:
279 a47eb4c Dawsh 2y ago query = query.limit(limit)
280 a47eb4c Dawsh 2y ago
281 a47eb4c Dawsh 2y ago return query.all()
282 a47eb4c Dawsh 2y ago
283 a47eb4c Dawsh 2y ago
284 01cdf2e Dawsh 2y ago def get_user_usages(
285 01cdf2e Dawsh 2y ago db: Session,
286 01cdf2e Dawsh 2y ago dbuser: User,
287 01cdf2e Dawsh 2y ago start: datetime,
288 01cdf2e Dawsh 2y ago end: datetime,
289 01cdf2e Dawsh 2y ago ) -> List[UserUsageResponse]:
290 eb74041 Dawsh 2y ago usages = dict()
291 a47eb4c Dawsh 2y ago
292 a47eb4c Dawsh 2y ago for node in db.query(Node).all():
293 a47eb4c Dawsh 2y ago usages[node.id] = UserUsageResponse(
294 01cdf2e Dawsh 2y ago node_id=node.id, node_name=node.name, used_traffic=0
295 a47eb4c Dawsh 2y ago )
296 a47eb4c Dawsh 2y ago
297 01cdf2e Dawsh 2y ago cond = and_(
298 01cdf2e Dawsh 2y ago NodeUserUsage.user_id == dbuser.id,
299 01cdf2e Dawsh 2y ago NodeUserUsage.created_at >= start,
300 01cdf2e Dawsh 2y ago NodeUserUsage.created_at <= end,
301 01cdf2e Dawsh 2y ago )
302 a47eb4c Dawsh 2y ago
303 a47eb4c Dawsh 2y ago for v in db.query(NodeUserUsage).filter(cond):
304 a47eb4c Dawsh 2y ago try:
305 a47eb4c Dawsh 2y ago usages[v.node_id or 0].used_traffic += v.used_traffic
306 a47eb4c Dawsh 2y ago except KeyError:
307 a47eb4c Dawsh 2y ago pass
308 a47eb4c Dawsh 2y ago
309 a47eb4c Dawsh 2y ago return list(usages.values())
310 a47eb4c Dawsh 2y ago
311 a47eb4c Dawsh 2y ago
312 e5dcd82 Dawsh 2y ago def get_users_count(
313 e5dcd82 Dawsh 2y ago db: Session,
314 e5dcd82 Dawsh 2y ago admin: Admin | None = None,
315 e5dcd82 Dawsh 2y ago enabled: bool | None = None,
316 e5dcd82 Dawsh 2y ago online: bool | None = None,
317 d377c4c Dawsh 2y ago expire_strategy: UserExpireStrategy | None = None,
318 d377c4c Dawsh 2y ago is_active: bool | None = None,
319 d377c4c Dawsh 2y ago expired: bool | None = None,
320 d377c4c Dawsh 2y ago data_limit_reached: bool | None = None,
321 e5dcd82 Dawsh 2y ago ):
322 a47eb4c Dawsh 2y ago query = db.query(User.id)
323 a47eb4c Dawsh 2y ago if admin:
324 9ddfbc8 Mardin 2y ago query = query.filter(User.admin_id == admin.id)
325 d377c4c Dawsh 2y ago if is_active:
326 d377c4c Dawsh 2y ago query = query.filter(User.is_active == is_active)
327 d377c4c Dawsh 2y ago if expired:
328 d377c4c Dawsh 2y ago query = query.filter(User.expired == expired)
329 d377c4c Dawsh 2y ago if data_limit_reached:
330 d377c4c Dawsh 2y ago query = query.filter(User.data_limit_reached == data_limit_reached)
331 e5dcd82 Dawsh 2y ago if enabled:
332 e5dcd82 Dawsh 2y ago query = query.filter(User.enabled == enabled)
333 d377c4c Dawsh 2y ago if online is True:
334 e5dcd82 Dawsh 2y ago query = query.filter(
335 e5dcd82 Dawsh 2y ago User.online_at > (datetime.utcnow() - timedelta(seconds=30))
336 e5dcd82 Dawsh 2y ago )
337 d377c4c Dawsh 2y ago elif online is False:
338 d377c4c Dawsh 2y ago query = query.filter(
339 d377c4c Dawsh 2y ago User.online_at < (datetime.utcnow() - timedelta(seconds=30))
340 d377c4c Dawsh 2y ago )
341 d377c4c Dawsh 2y ago if expire_strategy:
342 d377c4c Dawsh 2y ago query = query.filter(User.expire_strategy == expire_strategy)
343 d377c4c Dawsh 2y ago
344 a47eb4c Dawsh 2y ago return query.count()
345 a47eb4c Dawsh 2y ago
346 a47eb4c Dawsh 2y ago
347 a47eb4c Dawsh 2y ago def create_user(db: Session, user: UserCreate, admin: Admin = None):
348 a47eb4c Dawsh 2y ago dbuser = User(
349 a47eb4c Dawsh 2y ago username=user.username,
350 a47eb4c Dawsh 2y ago key=user.key,
351 d377c4c Dawsh 2y ago expire_strategy=user.expire_strategy,
352 d377c4c Dawsh 2y ago expire_date=user.expire_date,
353 d377c4c Dawsh 2y ago usage_duration=user.usage_duration,
354 d377c4c Dawsh 2y ago activation_deadline=user.activation_deadline,
355 01cdf2e Dawsh 2y ago services=db.query(Service)
356 db68a58 Dawsh 2y ago .filter(Service.id.in_(user.service_ids))
357 01cdf2e Dawsh 2y ago .all(), # user.services,
358 a47eb4c Dawsh 2y ago data_limit=(user.data_limit or None),
359 a47eb4c Dawsh 2y ago admin=admin,
360 a47eb4c Dawsh 2y ago data_limit_reset_strategy=user.data_limit_reset_strategy,
361 a47eb4c Dawsh 2y ago note=user.note,
362 a47eb4c Dawsh 2y ago )
363 a47eb4c Dawsh 2y ago db.add(dbuser)
364 a47eb4c Dawsh 2y ago db.commit()
365 a47eb4c Dawsh 2y ago db.refresh(dbuser)
366 a47eb4c Dawsh 2y ago return dbuser
367 a47eb4c Dawsh 2y ago
368 a47eb4c Dawsh 2y ago
369 a47eb4c Dawsh 2y ago def remove_user(db: Session, dbuser: User):
370 a47eb4c Dawsh 2y ago db.delete(dbuser)
371 a47eb4c Dawsh 2y ago # db.query(User).filter_by(id=user_id).delete()
372 a47eb4c Dawsh 2y ago db.commit()
373 a47eb4c Dawsh 2y ago
374 a47eb4c Dawsh 2y ago
375 a47eb4c Dawsh 2y ago def update_user(db: Session, dbuser: User, modify: UserModify):
376 a47eb4c Dawsh 2y ago if modify.data_limit is not None:
377 d377c4c Dawsh 2y ago dbuser.data_limit = modify.data_limit or None
378 d377c4c Dawsh 2y ago
379 d377c4c Dawsh 2y ago if modify.expire_strategy is not None:
380 d377c4c Dawsh 2y ago dbuser.expire_strategy = modify.expire_strategy or None
381 d377c4c Dawsh 2y ago
382 d377c4c Dawsh 2y ago if modify.expire_date is not None:
383 d377c4c Dawsh 2y ago dbuser.expire_date = modify.expire_date or None
384 a47eb4c Dawsh 2y ago
385 a47eb4c Dawsh 2y ago if modify.note is not None:
386 a47eb4c Dawsh 2y ago dbuser.note = modify.note or None
387 a47eb4c Dawsh 2y ago
388 a47eb4c Dawsh 2y ago if modify.data_limit_reset_strategy is not None:
389 d377c4c Dawsh 2y ago dbuser.data_limit_reset_strategy = modify.data_limit_reset_strategy
390 a47eb4c Dawsh 2y ago
391 d377c4c Dawsh 2y ago if modify.activation_deadline is not None:
392 d377c4c Dawsh 2y ago dbuser.activation_deadline = modify.activation_deadline
393 a47eb4c Dawsh 2y ago
394 d377c4c Dawsh 2y ago if modify.usage_duration is not None:
395 d377c4c Dawsh 2y ago dbuser.usage_duration = modify.usage_duration
396 2de9d72 Dawsh 2y ago
397 db68a58 Dawsh 2y ago if modify.service_ids is not None:
398 01cdf2e Dawsh 2y ago dbuser.services = (
399 db68a58 Dawsh 2y ago db.query(Service).filter(Service.id.in_(modify.service_ids)).all()
400 01cdf2e Dawsh 2y ago )
401 a47eb4c Dawsh 2y ago dbuser.edit_at = datetime.utcnow()
402 a47eb4c Dawsh 2y ago
403 a47eb4c Dawsh 2y ago db.commit()
404 a47eb4c Dawsh 2y ago db.refresh(dbuser)
405 a47eb4c Dawsh 2y ago return dbuser
406 a47eb4c Dawsh 2y ago
407 a47eb4c Dawsh 2y ago
408 a47eb4c Dawsh 2y ago def reset_user_data_usage(db: Session, dbuser: User):
409 42a1073 Dawsh 2y ago dbuser.lifetime_used_traffic += dbuser.used_traffic
410 42a1073 Dawsh 2y ago dbuser.traffic_reset_at = datetime.utcnow()
411 a47eb4c Dawsh 2y ago
412 a47eb4c Dawsh 2y ago dbuser.used_traffic = 0
413 42a1073 Dawsh 2y ago
414 a47eb4c Dawsh 2y ago db.add(dbuser)
415 a47eb4c Dawsh 2y ago
416 a47eb4c Dawsh 2y ago db.commit()
417 a47eb4c Dawsh 2y ago db.refresh(dbuser)
418 a47eb4c Dawsh 2y ago return dbuser
419 a47eb4c Dawsh 2y ago
420 a47eb4c Dawsh 2y ago
421 a47eb4c Dawsh 2y ago def revoke_user_sub(db: Session, dbuser: User):
422 34a7612 Dawsh 2y ago dbuser.key = secrets.token_hex(16)
423 a47eb4c Dawsh 2y ago db.commit()
424 a47eb4c Dawsh 2y ago db.refresh(dbuser)
425 a47eb4c Dawsh 2y ago return dbuser
426 a47eb4c Dawsh 2y ago
427 a47eb4c Dawsh 2y ago
428 a47eb4c Dawsh 2y ago def update_user_sub(db: Session, dbuser: User, user_agent: str):
429 a47eb4c Dawsh 2y ago dbuser.sub_updated_at = datetime.utcnow()
430 a47eb4c Dawsh 2y ago dbuser.sub_last_user_agent = user_agent
431 a47eb4c Dawsh 2y ago
432 a47eb4c Dawsh 2y ago db.commit()
433 a47eb4c Dawsh 2y ago db.refresh(dbuser)
434 a47eb4c Dawsh 2y ago return dbuser
435 a47eb4c Dawsh 2y ago
436 a47eb4c Dawsh 2y ago
437 a47eb4c Dawsh 2y ago def reset_all_users_data_usage(db: Session, admin: Optional[Admin] = None):
438 a47eb4c Dawsh 2y ago query = db.query(User)
439 a47eb4c Dawsh 2y ago
440 a47eb4c Dawsh 2y ago if admin:
441 a47eb4c Dawsh 2y ago query = query.filter(User.admin == admin)
442 a47eb4c Dawsh 2y ago
443 d377c4c Dawsh 2y ago for db_user in query.all():
444 d377c4c Dawsh 2y ago db_user.used_traffic = 0
445 a47eb4c Dawsh 2y ago
446 a47eb4c Dawsh 2y ago db.commit()
447 a47eb4c Dawsh 2y ago
448 a47eb4c Dawsh 2y ago
449 a47eb4c Dawsh 2y ago def update_user_status(db: Session, dbuser: User, status: UserStatus):
450 a47eb4c Dawsh 2y ago dbuser.status = status
451 a47eb4c Dawsh 2y ago db.commit()
452 a47eb4c Dawsh 2y ago db.refresh(dbuser)
453 a47eb4c Dawsh 2y ago return dbuser
454 a47eb4c Dawsh 2y ago
455 a47eb4c Dawsh 2y ago
456 a47eb4c Dawsh 2y ago def set_owner(db: Session, dbuser: User, admin: Admin):
457 a47eb4c Dawsh 2y ago dbuser.admin = admin
458 a47eb4c Dawsh 2y ago db.commit()
459 a47eb4c Dawsh 2y ago db.refresh(dbuser)
460 a47eb4c Dawsh 2y ago return dbuser
461 a47eb4c Dawsh 2y ago
462 a47eb4c Dawsh 2y ago
463 a47eb4c Dawsh 2y ago def get_system_usage(db: Session):
464 a47eb4c Dawsh 2y ago return db.query(System).first()
465 a47eb4c Dawsh 2y ago
466 a47eb4c Dawsh 2y ago
467 a47eb4c Dawsh 2y ago def get_jwt_secret_key(db: Session):
468 a47eb4c Dawsh 2y ago return db.query(JWT).first().secret_key
469 a47eb4c Dawsh 2y ago
470 a47eb4c Dawsh 2y ago
471 a47eb4c Dawsh 2y ago def get_tls_certificate(db: Session):
472 a47eb4c Dawsh 2y ago return db.query(TLS).first()
473 a47eb4c Dawsh 2y ago
474 a47eb4c Dawsh 2y ago
475 a47eb4c Dawsh 2y ago def get_admin(db: Session, username: str):
476 a47eb4c Dawsh 2y ago return db.query(Admin).filter(Admin.username == username).first()
477 a47eb4c Dawsh 2y ago
478 a47eb4c Dawsh 2y ago
479 a47eb4c Dawsh 2y ago def create_admin(db: Session, admin: AdminCreate):
480 a47eb4c Dawsh 2y ago dbadmin = Admin(
481 a47eb4c Dawsh 2y ago username=admin.username,
482 a47eb4c Dawsh 2y ago hashed_password=admin.hashed_password,
483 01cdf2e Dawsh 2y ago is_sudo=admin.is_sudo,
484 a47eb4c Dawsh 2y ago )
485 a47eb4c Dawsh 2y ago db.add(dbadmin)
486 a47eb4c Dawsh 2y ago db.commit()
487 a47eb4c Dawsh 2y ago db.refresh(dbadmin)
488 a47eb4c Dawsh 2y ago return dbadmin
489 a47eb4c Dawsh 2y ago
490 a47eb4c Dawsh 2y ago
491 a47eb4c Dawsh 2y ago def update_admin(db: Session, dbadmin: Admin, modified_admin: AdminModify):
492 a47eb4c Dawsh 2y ago dbadmin.is_sudo = modified_admin.is_sudo
493 a47eb4c Dawsh 2y ago if dbadmin.hashed_password != modified_admin.hashed_password:
494 a47eb4c Dawsh 2y ago dbadmin.hashed_password = modified_admin.hashed_password
495 a47eb4c Dawsh 2y ago dbadmin.password_reset_at = datetime.utcnow()
496 a47eb4c Dawsh 2y ago db.commit()
497 a47eb4c Dawsh 2y ago db.refresh(dbadmin)
498 2de9d72 Dawsh 2y ago return dbadmin
499 a47eb4c Dawsh 2y ago
500 a47eb4c Dawsh 2y ago
501 01cdf2e Dawsh 2y ago def partial_update_admin(
502 01cdf2e Dawsh 2y ago db: Session, dbadmin: Admin, modified_admin: AdminPartialModify
503 01cdf2e Dawsh 2y ago ):
504 a47eb4c Dawsh 2y ago if modified_admin.is_sudo is not None:
505 a47eb4c Dawsh 2y ago dbadmin.is_sudo = modified_admin.is_sudo
506 01cdf2e Dawsh 2y ago if (
507 01cdf2e Dawsh 2y ago modified_admin.password is not None
508 01cdf2e Dawsh 2y ago and dbadmin.hashed_password != modified_admin.hashed_password
509 01cdf2e Dawsh 2y ago ):
510 a47eb4c Dawsh 2y ago dbadmin.hashed_password = modified_admin.hashed_password
511 a47eb4c Dawsh 2y ago dbadmin.password_reset_at = datetime.utcnow()
512 a47eb4c Dawsh 2y ago
513 a47eb4c Dawsh 2y ago db.commit()
514 a47eb4c Dawsh 2y ago db.refresh(dbadmin)
515 a47eb4c Dawsh 2y ago return dbadmin
516 a47eb4c Dawsh 2y ago
517 a47eb4c Dawsh 2y ago
518 a47eb4c Dawsh 2y ago def remove_admin(db: Session, dbadmin: Admin):
519 a47eb4c Dawsh 2y ago db.delete(dbadmin)
520 a47eb4c Dawsh 2y ago db.commit()
521 a47eb4c Dawsh 2y ago return dbadmin
522 a47eb4c Dawsh 2y ago
523 a47eb4c Dawsh 2y ago
524 01cdf2e Dawsh 2y ago def get_admins(
525 01cdf2e Dawsh 2y ago db: Session,
526 01cdf2e Dawsh 2y ago offset: Optional[int] = None,
527 01cdf2e Dawsh 2y ago limit: Optional[int] = None,
528 01cdf2e Dawsh 2y ago username: Optional[str] = None,
529 01cdf2e Dawsh 2y ago ):
530 a47eb4c Dawsh 2y ago query = db.query(Admin)
531 a47eb4c Dawsh 2y ago if username:
532 01cdf2e Dawsh 2y ago query = query.filter(Admin.username.ilike(f"%{username}%"))
533 a47eb4c Dawsh 2y ago if offset:
534 a47eb4c Dawsh 2y ago query = query.offset(offset)
535 a47eb4c Dawsh 2y ago if limit:
536 a47eb4c Dawsh 2y ago query = query.limit(limit)
537 a47eb4c Dawsh 2y ago return query.all()
538 a47eb4c Dawsh 2y ago
539 a47eb4c Dawsh 2y ago
540 ee1351a Dawsh 2y ago def create_service(db: Session, service: ServiceCreate) -> Service:
541 a47eb4c Dawsh 2y ago dbservice = Service(
542 a47eb4c Dawsh 2y ago name=service.name,
543 f1ab16c Dawsh 2y ago inbounds=db.query(Inbound)
544 f1ab16c Dawsh 2y ago .filter(Inbound.id.in_(service.inbound_ids))
545 f1ab16c Dawsh 2y ago .all(),
546 db68a58 Dawsh 2y ago users=[],
547 2de9d72 Dawsh 2y ago )
548 a47eb4c Dawsh 2y ago db.add(dbservice)
549 a47eb4c Dawsh 2y ago db.commit()
550 a47eb4c Dawsh 2y ago db.refresh(dbservice)
551 a47eb4c Dawsh 2y ago return dbservice
552 a47eb4c Dawsh 2y ago
553 a47eb4c Dawsh 2y ago
554 ee1351a Dawsh 2y ago def get_service(db: Session, service_id: id) -> Service:
555 ee1351a Dawsh 2y ago return db.query(Service).filter(Service.id == service_id).first()
556 a47eb4c Dawsh 2y ago
557 12b255b Dawsh 2y ago
558 a47eb4c Dawsh 2y ago def get_services(db: Session) -> List[Service]:
559 a47eb4c Dawsh 2y ago return db.query(Service).all()
560 a47eb4c Dawsh 2y ago
561 12b255b Dawsh 2y ago
562 f1ab16c Dawsh 2y ago def update_service(
563 f1ab16c Dawsh 2y ago db: Session, db_service: Service, modification: ServiceModify
564 f1ab16c Dawsh 2y ago ):
565 ee1351a Dawsh 2y ago if modification.name is not None:
566 ee1351a Dawsh 2y ago db_service.name = modification.name
567 ee1351a Dawsh 2y ago
568 db68a58 Dawsh 2y ago if modification.inbound_ids is not None:
569 01cdf2e Dawsh 2y ago db_service.inbounds = (
570 f1ab16c Dawsh 2y ago db.query(Inbound)
571 f1ab16c Dawsh 2y ago .filter(Inbound.id.in_(modification.inbound_ids))
572 f1ab16c Dawsh 2y ago .all()
573 01cdf2e Dawsh 2y ago )
574 ee1351a Dawsh 2y ago
575 ee1351a Dawsh 2y ago db.commit()
576 ee1351a Dawsh 2y ago db.refresh(db_service)
577 ee1351a Dawsh 2y ago return db_service
578 ee1351a Dawsh 2y ago
579 ee1351a Dawsh 2y ago
580 ee1351a Dawsh 2y ago def remove_service(db: Session, db_service: Service):
581 ee1351a Dawsh 2y ago db.delete(db_service)
582 ee1351a Dawsh 2y ago db.commit()
583 ee1351a Dawsh 2y ago return db_service
584 ee1351a Dawsh 2y ago
585 ee1351a Dawsh 2y ago
586 a47eb4c Dawsh 2y ago def get_node(db: Session, name: str):
587 a47eb4c Dawsh 2y ago return db.query(Node).filter(Node.name == name).first()
588 a47eb4c Dawsh 2y ago
589 a47eb4c Dawsh 2y ago
590 a47eb4c Dawsh 2y ago def get_node_by_id(db: Session, node_id: int):
591 a47eb4c Dawsh 2y ago return db.query(Node).filter(Node.id == node_id).first()
592 a47eb4c Dawsh 2y ago
593 a47eb4c Dawsh 2y ago
594 01cdf2e Dawsh 2y ago def get_nodes(
595 f1ab16c Dawsh 2y ago db: Session,
596 f1ab16c Dawsh 2y ago status: Optional[Union[NodeStatus, list]] = None,
597 f1ab16c Dawsh 2y ago enabled: bool = None,
598 01cdf2e Dawsh 2y ago ):
599 a47eb4c Dawsh 2y ago query = db.query(Node)
600 a47eb4c Dawsh 2y ago
601 a47eb4c Dawsh 2y ago if status:
602 a47eb4c Dawsh 2y ago if isinstance(status, list):
603 a47eb4c Dawsh 2y ago query = query.filter(Node.status.in_(status))
604 a47eb4c Dawsh 2y ago else:
605 a47eb4c Dawsh 2y ago query = query.filter(Node.status == status)
606 a47eb4c Dawsh 2y ago
607 a47eb4c Dawsh 2y ago if enabled:
608 a47eb4c Dawsh 2y ago query = query.filter(Node.status != NodeStatus.disabled)
609 a47eb4c Dawsh 2y ago
610 a47eb4c Dawsh 2y ago return query.all()
611 a47eb4c Dawsh 2y ago
612 a47eb4c Dawsh 2y ago
613 01cdf2e Dawsh 2y ago def get_nodes_usage(
614 01cdf2e Dawsh 2y ago db: Session, start: datetime, end: datetime
615 01cdf2e Dawsh 2y ago ) -> List[NodeUsageResponse]:
616 12b255b Dawsh 2y ago usages = dict()
617 a47eb4c Dawsh 2y ago
618 a47eb4c Dawsh 2y ago usages[0] = NodeUsageResponse( # Main Core
619 01cdf2e Dawsh 2y ago node_id=None, node_name="Master", uplink=0, downlink=0
620 a47eb4c Dawsh 2y ago )
621 a47eb4c Dawsh 2y ago for node in db.query(Node).all():
622 a47eb4c Dawsh 2y ago usages[node.id] = NodeUsageResponse(
623 01cdf2e Dawsh 2y ago node_id=node.id, node_name=node.name, uplink=0, downlink=0
624 a47eb4c Dawsh 2y ago )
625 a47eb4c Dawsh 2y ago
626 a47eb4c Dawsh 2y ago cond = and_(NodeUsage.created_at >= start, NodeUsage.created_at <= end)
627 a47eb4c Dawsh 2y ago
628 a47eb4c Dawsh 2y ago for v in db.query(NodeUsage).filter(cond):
629 a47eb4c Dawsh 2y ago try:
630 a47eb4c Dawsh 2y ago usages[v.node_id or 0].uplink += v.uplink
631 a47eb4c Dawsh 2y ago usages[v.node_id or 0].downlink += v.downlink
632 a47eb4c Dawsh 2y ago except KeyError:
633 a47eb4c Dawsh 2y ago pass
634 a47eb4c Dawsh 2y ago
635 a47eb4c Dawsh 2y ago return list(usages.values())
636 a47eb4c Dawsh 2y ago
637 a47eb4c Dawsh 2y ago
638 a47eb4c Dawsh 2y ago def create_node(db: Session, node: NodeCreate):
639 01cdf2e Dawsh 2y ago dbnode = Node(
640 01cdf2e Dawsh 2y ago name=node.name,
641 01cdf2e Dawsh 2y ago address=node.address,
642 01cdf2e Dawsh 2y ago port=node.port,
643 01cdf2e Dawsh 2y ago connection_backend=node.connection_backend,
644 01cdf2e Dawsh 2y ago )
645 a47eb4c Dawsh 2y ago
646 a47eb4c Dawsh 2y ago db.add(dbnode)
647 a47eb4c Dawsh 2y ago db.commit()
648 a47eb4c Dawsh 2y ago db.refresh(dbnode)
649 a47eb4c Dawsh 2y ago return dbnode
650 a47eb4c Dawsh 2y ago
651 a47eb4c Dawsh 2y ago
652 a47eb4c Dawsh 2y ago def remove_node(db: Session, dbnode: Node):
653 a47eb4c Dawsh 2y ago db.delete(dbnode)
654 a47eb4c Dawsh 2y ago db.commit()
655 a47eb4c Dawsh 2y ago return dbnode
656 a47eb4c Dawsh 2y ago
657 a47eb4c Dawsh 2y ago
658 a47eb4c Dawsh 2y ago def update_node(db: Session, dbnode: Node, modify: NodeModify):
659 a47eb4c Dawsh 2y ago if modify.name is not None:
660 a47eb4c Dawsh 2y ago dbnode.name = modify.name
661 a47eb4c Dawsh 2y ago
662 a47eb4c Dawsh 2y ago if modify.address is not None:
663 a47eb4c Dawsh 2y ago dbnode.address = modify.address
664 a47eb4c Dawsh 2y ago
665 a47eb4c Dawsh 2y ago if modify.port is not None:
666 a47eb4c Dawsh 2y ago dbnode.port = modify.port
667 a47eb4c Dawsh 2y ago
668 a47eb4c Dawsh 2y ago if modify.status is NodeStatus.disabled:
669 a47eb4c Dawsh 2y ago dbnode.status = modify.status
670 a47eb4c Dawsh 2y ago dbnode.xray_version = None
671 a47eb4c Dawsh 2y ago dbnode.message = None
672 a47eb4c Dawsh 2y ago else:
673 12b255b Dawsh 2y ago dbnode.status = NodeStatus.unhealthy
674 a47eb4c Dawsh 2y ago
675 a47eb4c Dawsh 2y ago if modify.usage_coefficient:
676 a47eb4c Dawsh 2y ago dbnode.usage_coefficient = modify.usage_coefficient
677 a47eb4c Dawsh 2y ago
678 5584d4a Mrdn 2y ago if modify.connection_backend:
679 5584d4a Mrdn 2y ago dbnode.connection_backend = modify.connection_backend
680 5584d4a Mrdn 2y ago
681 a47eb4c Dawsh 2y ago db.commit()
682 a47eb4c Dawsh 2y ago db.refresh(dbnode)
683 a47eb4c Dawsh 2y ago return dbnode
684 a47eb4c Dawsh 2y ago
685 a47eb4c Dawsh 2y ago
686 01cdf2e Dawsh 2y ago def update_node_status(
687 01cdf2e Dawsh 2y ago db: Session,
688 01cdf2e Dawsh 2y ago node_id: int,
689 01cdf2e Dawsh 2y ago status: NodeStatus,
690 01cdf2e Dawsh 2y ago message: str = None,
691 01cdf2e Dawsh 2y ago version: str = None,
692 01cdf2e Dawsh 2y ago ):
693 f6a32ea Dawsh 2y ago db_node = db.query(Node).where(Node.id == node_id).first()
694 f6a32ea Dawsh 2y ago db_node.status = status
695 f6a32ea Dawsh 2y ago if message:
696 f6a32ea Dawsh 2y ago db_node.message = message
697 f6a32ea Dawsh 2y ago db_node.last_status_change = datetime.utcnow()
698 a47eb4c Dawsh 2y ago db.commit()
699 a47eb4c Dawsh 2y ago
700 a47eb4c Dawsh 2y ago
701 a47eb4c Dawsh 2y ago def create_notification_reminder(
702 f1ab16c Dawsh 2y ago db: Session,
703 f1ab16c Dawsh 2y ago reminder_type: ReminderType,
704 f1ab16c Dawsh 2y ago expires_at: datetime,
705 f1ab16c Dawsh 2y ago user_id: int,
706 01cdf2e Dawsh 2y ago ) -> NotificationReminder:
707 01cdf2e Dawsh 2y ago reminder = NotificationReminder(
708 01cdf2e Dawsh 2y ago type=reminder_type, expires_at=expires_at, user_id=user_id
709 01cdf2e Dawsh 2y ago )
710 a47eb4c Dawsh 2y ago db.add(reminder)
711 a47eb4c Dawsh 2y ago db.commit()
712 a47eb4c Dawsh 2y ago db.refresh(reminder)
713 a47eb4c Dawsh 2y ago return reminder
714 a47eb4c Dawsh 2y ago
715 a47eb4c Dawsh 2y ago
716 a47eb4c Dawsh 2y ago def get_notification_reminder(
717 01cdf2e Dawsh 2y ago db: Session,
718 01cdf2e Dawsh 2y ago user_id: int,
719 01cdf2e Dawsh 2y ago reminder_type: ReminderType,
720 12b255b Dawsh 2y ago ) -> Optional[NotificationReminder]:
721 01cdf2e Dawsh 2y ago reminder = (
722 01cdf2e Dawsh 2y ago db.query(NotificationReminder)
723 01cdf2e Dawsh 2y ago .filter(NotificationReminder.user_id == user_id)
724 01cdf2e Dawsh 2y ago .filter(NotificationReminder.type == reminder_type)
725 01cdf2e Dawsh 2y ago .first()
726 01cdf2e Dawsh 2y ago )
727 a47eb4c Dawsh 2y ago if reminder is None:
728 a47eb4c Dawsh 2y ago return
729 a47eb4c Dawsh 2y ago if reminder.expires_at and reminder.expires_at < datetime.utcnow():
730 a47eb4c Dawsh 2y ago db.delete(reminder)
731 a47eb4c Dawsh 2y ago db.commit()
732 a47eb4c Dawsh 2y ago return
733 a47eb4c Dawsh 2y ago return reminder
734 a47eb4c Dawsh 2y ago
735 a47eb4c Dawsh 2y ago
736 01cdf2e Dawsh 2y ago def delete_notification_reminder_by_type(
737 01cdf2e Dawsh 2y ago db: Session, user_id: int, reminder_type: ReminderType
738 01cdf2e Dawsh 2y ago ) -> None:
739 a47eb4c Dawsh 2y ago """Deletes notification reminder filtered by user_id and type if exists"""
740 a47eb4c Dawsh 2y ago stmt = delete(NotificationReminder).where(
741 a47eb4c Dawsh 2y ago NotificationReminder.user_id == user_id,
742 a47eb4c Dawsh 2y ago NotificationReminder.type == reminder_type,
743 a47eb4c Dawsh 2y ago )
744 a47eb4c Dawsh 2y ago db.execute(stmt)
745 a47eb4c Dawsh 2y ago db.commit()
746 a47eb4c Dawsh 2y ago return
747 a47eb4c Dawsh 2y ago
748 a47eb4c Dawsh 2y ago
749 f1ab16c Dawsh 2y ago def delete_notification_reminder(
750 f1ab16c Dawsh 2y ago db: Session, dbreminder: NotificationReminder
751 f1ab16c Dawsh 2y ago ) -> None:
752 a47eb4c Dawsh 2y ago db.delete(dbreminder)
753 a47eb4c Dawsh 2y ago db.commit()
754 a47eb4c Dawsh 2y ago return