-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud.py
143 lines (113 loc) · 4.49 KB
/
crud.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
from fastapi import HTTPException
from sqlalchemy.orm import Session
import models
import schemas
GLOBAL_SCOPES = [
schemas.QuotaScope.user,
schemas.QuotaScope.course,
schemas.QuotaScope.total
]
COURSE_SCOPES = [
schemas.QuotaScope.course,
schemas.QuotaScope.course_user,
]
# CRUD für QuotaDefinition
def get_quota_definitions(db: Session) -> list[models.QuotaDefinition]:
return db.query(models.QuotaDefinition).all()
# CRUD für Quota
def get_global_quotas(db: Session) -> list[models.Quota]:
return db.query(models.Quota).filter(
models.Quota.scope.in_(GLOBAL_SCOPES),
models.Quota.user_id == None,
models.Quota.course_id == None
).all()
def check_quota_definition(db: Session, quota: schemas.QuotaUpdate) -> models.QuotaDefinition:
quota_definition = db.query(models.QuotaDefinition).filter(
models.QuotaDefinition.scope == quota.scope,
models.QuotaDefinition.feature == quota.feature
).first()
if not quota_definition:
raise HTTPException(
status_code=404,
detail=f"QuotaDefinition with scope={quota.scope} and feature={quota.feature} not found"
)
return quota_definition
def update_or_create_global_quota(db: Session, global_quota: schemas.QuotaUpdate) -> models.Quota:
if global_quota.scope not in GLOBAL_SCOPES:
raise HTTPException(status_code=400, detail="Supported global scopes: " + ', '.join([s.value for s in GLOBAL_SCOPES]))
# Check if the quota definition exists
quota_definition = check_quota_definition(db, global_quota)
db_quota = db.query(models.Quota).filter(
models.Quota.scope == global_quota.scope,
models.Quota.feature == global_quota.feature,
models.Quota.user_id == None,
models.Quota.course_id == None
).first()
if db_quota:
# Update
db_quota.limit = global_quota.limit
else:
# Create
db_quota = models.Quota(
limit=global_quota.limit,
scope=global_quota.scope,
feature=global_quota.feature,
type=quota_definition.type, # TODO: Do we need to store the type in each quota
quota_definition_id=quota_definition.id # use the ID of the quota definition
)
db.add(db_quota)
db.commit()
db.refresh(db_quota)
return db_quota
def get_course_quotas(db: Session, course_id: str) -> list[models.Quota]:
return db.query(models.Quota).filter(
models.Quota.scope.in_(COURSE_SCOPES),
models.Quota.course_id == course_id,
models.Quota.user_id == None
).all()
def update_or_create_course_quota(db: Session, course_id: str, course_quota: schemas.QuotaUpdate) -> models.Quota:
if course_quota.scope not in COURSE_SCOPES:
raise HTTPException(status_code=400, detail="Supported course scopes: " + ', '.join([s.value for s in COURSE_SCOPES]))
# Check if the quota definition exists
quota_definition = check_quota_definition(db, course_quota)
db_quota = db.query(models.Quota).filter(
models.Quota.scope == course_quota.scope,
models.Quota.feature == course_quota.feature,
models.Quota.course_id == course_id,
models.Quota.user_id == None
).first()
if db_quota:
# Update
db_quota.limit = course_quota.limit
else:
# Create
db_quota = models.Quota(
limit=course_quota.limit,
scope=course_quota.scope,
feature=course_quota.feature,
course_id=course_id,
type=quota_definition.type, # TODO: Do we need to store the type in each quota
quota_definition_id=quota_definition.id # use the ID of the quota definition
)
db.add(db_quota)
db.commit()
db.refresh(db_quota)
return db_quota
def get_course_member_quotas(db: Session, course_id: str) -> list[models.Quota]:
return db.query(models.Quota).filter(
models.Quota.scope == schemas.QuotaScope.course_user,
models.Quota.course_id == course_id,
models.Quota.user_id != None
).all()
def get_course_member_quota(db: Session, course_id: str, user_id: str) -> models.Quota:
quota = db.query(models.Quota).filter(
models.Quota.scope == schemas.QuotaScope.course_user,
models.Quota.course_id == course_id,
models.Quota.user_id == user_id
).first()
if not quota:
raise HTTPException(
status_code=404,
detail=f"User quota with scope=course-user not found"
)
return quota