Skip to content

Commit a6bae13

Browse files
committed
Remove deprecated calls to select(), insert(), etc
1 parent 10f1115 commit a6bae13

19 files changed

+274
-336
lines changed

rctab/crud/auth.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async def load_cache(oid: str) -> msal.SerializableTokenCache:
1818
"""Load a user's token cache from the database."""
1919
cache = msal.SerializableTokenCache()
2020
value = await database.fetch_val(
21-
select([user_cache.c.cache]).where(user_cache.c.oid == oid)
21+
select(user_cache.c.cache).where(user_cache.c.oid == oid)
2222
)
2323
if value:
2424
cache.deserialize(value)
@@ -56,12 +56,10 @@ async def check_user_access(
5656
raise_http_exception: Raise an exception if the user isn't found.
5757
"""
5858
statement = select(
59-
[
60-
user_rbac.c.oid,
61-
user_rbac.c.username,
62-
user_rbac.c.has_access,
63-
user_rbac.c.is_admin,
64-
]
59+
user_rbac.c.oid,
60+
user_rbac.c.username,
61+
user_rbac.c.has_access,
62+
user_rbac.c.is_admin,
6563
).where(user_rbac.c.oid == oid)
6664

6765
user_status = await database.fetch_one(statement)

rctab/routers/accounting/abolishment.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ async def get_inactive_subs() -> Optional[List[UUID]]:
3232
# The most recent time_created for each subscription's subscription_detail
3333
query_1 = (
3434
select(
35-
[
36-
subscription_details.c.subscription_id,
37-
func.max(subscription_details.c.time_created).label("time_created"),
38-
]
35+
subscription_details.c.subscription_id,
36+
func.max(subscription_details.c.time_created).label("time_created"),
3937
).group_by(subscription_details.c.subscription_id)
4038
).alias()
4139

@@ -54,7 +52,7 @@ async def get_inactive_subs() -> Optional[List[UUID]]:
5452
# subscriptions that have been inactive for more than 90 days
5553
# and have not been abolished yet
5654
query_2_result = await database.fetch_all(
57-
select([subscription_details.c.subscription_id])
55+
select(subscription_details.c.subscription_id)
5856
.select_from(query_2)
5957
.where(
6058
and_(
@@ -78,7 +76,7 @@ async def adjust_budgets_to_zero(admin_oid: UUID, sub_ids: List[UUID]) -> List[d
7876
sub_query = get_subscriptions_summary(execute=False).alias()
7977

8078
summaries = (
81-
select([sub_query])
79+
select(sub_query)
8280
.where(sub_query.c.subscription_id.in_([str(sub_id) for sub_id in sub_ids]))
8381
.alias()
8482
)

rctab/routers/accounting/cost_recovery.py

+18-22
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async def calc_cost_recovery(
6767
but not for this month.
6868
"""
6969
last_recovered_day = await database.fetch_one(
70-
select([cost_recovery_log]).order_by(desc(cost_recovery_log.c.month))
70+
select(cost_recovery_log).order_by(desc(cost_recovery_log.c.month))
7171
)
7272
last_recovered_month = (
7373
CostRecoveryMonth(first_day=last_recovered_day["month"])
@@ -86,9 +86,7 @@ async def calc_cost_recovery(
8686
subscription_ids = [
8787
r["sub_id"]
8888
for r in await database.fetch_all(
89-
select(
90-
[func.distinct(finance.c.subscription_id).label("sub_id")]
91-
).where(
89+
select(func.distinct(finance.c.subscription_id).label("sub_id")).where(
9290
between(
9391
recovery_month.first_day, finance.c.date_from, finance.c.date_to
9492
)
@@ -99,7 +97,7 @@ async def calc_cost_recovery(
9997
for subscription_id in subscription_ids:
10098

10199
usage_row = await database.fetch_one(
102-
select([func.sum(usage.c.total_cost).label("the_sum")])
100+
select(func.sum(usage.c.total_cost).label("the_sum"))
103101
.where(
104102
func.date_trunc(
105103
"month", sqlalchemy.cast(usage.c.date, sqlalchemy.Date)
@@ -118,7 +116,7 @@ async def calc_cost_recovery(
118116

119117
# The lower the value of priority, the higher importance
120118
finance_periods = await database.fetch_all(
121-
select([finance])
119+
select(finance)
122120
.where(
123121
and_(
124122
between(
@@ -136,7 +134,7 @@ async def calc_cost_recovery(
136134
for finance_period in finance_periods:
137135

138136
cost_recovery_row = await database.fetch_one(
139-
select([func.sum(cost_recovery.c.amount).label("the_sum")]).where(
137+
select(func.sum(cost_recovery.c.amount).label("the_sum")).where(
140138
cost_recovery.c.finance_id == finance_period["id"]
141139
)
142140
)
@@ -160,32 +158,30 @@ async def calc_cost_recovery(
160158
cost_recovery_id = await database.execute(
161159
insert(
162160
cost_recovery,
163-
{
164-
"subscription_id": finance_period["subscription_id"],
165-
"month": recovery_month.first_day,
166-
"finance_code": finance_period["finance_code"],
167-
"amount": recoverable_amount,
168-
"date_recovered": None,
169-
"finance_id": finance_period["id"],
170-
"admin": admin,
171-
},
172-
)
161+
),
162+
{
163+
"subscription_id": finance_period["subscription_id"],
164+
"month": recovery_month.first_day,
165+
"finance_code": finance_period["finance_code"],
166+
"amount": recoverable_amount,
167+
"date_recovered": None,
168+
"finance_id": finance_period["id"],
169+
"admin": admin,
170+
},
173171
)
174172
cost_recovery_ids.append(cost_recovery_id)
175173

176174
inserted_rows = await database.fetch_all(
177-
select([cost_recovery]).where(cost_recovery.c.id.in_(cost_recovery_ids))
175+
select(cost_recovery).where(cost_recovery.c.id.in_(cost_recovery_ids))
178176
)
179177

180178
# Note that we patch CostRecovery as a unit testing hack
181179
cost_recoveries = [CostRecovery(**dict(cr)) for cr in inserted_rows]
182180

183181
if commit_transaction:
184182
await database.execute(
185-
insert(
186-
cost_recovery_log,
187-
{"month": recovery_month.first_day, "admin": admin},
188-
)
183+
insert(cost_recovery_log),
184+
{"month": recovery_month.first_day, "admin": admin},
189185
)
190186
await transaction.commit()
191187
else:

rctab/routers/accounting/desired_states.py

+61-79
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,16 @@ async def get_desired_states(
8585
summaries = get_subscriptions_summary(execute=False).alias()
8686

8787
to_be_changed = select(
88-
[
89-
summaries.c.subscription_id,
90-
case(
91-
[
92-
# If the desired status is False, we want to disable it
93-
(
94-
summaries.c.desired_status == False,
95-
SubscriptionState("Disabled"),
96-
),
97-
# If the desired status is True, we want to enable it
98-
(summaries.c.desired_status == True, SubscriptionState("Enabled")),
99-
]
100-
).label("desired_state"),
101-
]
88+
summaries.c.subscription_id,
89+
case(
90+
# If the desired status is False, we want to disable it
91+
(
92+
summaries.c.desired_status == False,
93+
SubscriptionState("Disabled"),
94+
),
95+
# If the desired status is True, we want to enable it
96+
(summaries.c.desired_status == True, SubscriptionState("Enabled")),
97+
).label("desired_state"),
10298
).where(
10399
or_(
104100
and_(
@@ -151,7 +147,7 @@ async def refresh_desired_states(
151147

152148
if subscription_ids:
153149
summaries = (
154-
select([sub_query])
150+
select(sub_query)
155151
.where(
156152
sub_query.c.subscription_id.in_(
157153
[str(sub_id) for sub_id in subscription_ids]
@@ -165,7 +161,7 @@ async def refresh_desired_states(
165161
# Subscriptions without an approved_to date or whose approved_to
166162
# date is in the past, that currently have a desired_status of True
167163
over_time = (
168-
select([summaries]).where(
164+
select(summaries).where(
169165
and_(
170166
or_(
171167
summaries.c.approved_to == None,
@@ -206,7 +202,7 @@ async def refresh_desired_states(
206202
# Subscriptions with more usage than allocated budget
207203
# that currently have a desired_status of True
208204
over_budget = (
209-
select([summaries])
205+
select(summaries)
210206
.where(
211207
and_(
212208
# To gracelessly sidestep rounding errors, allow a tolerance
@@ -222,58 +218,50 @@ async def refresh_desired_states(
222218

223219
over_time_or_over_budget = (
224220
select(
225-
[
226-
literal_column("1").label("reason_enum"),
227-
over_time.c.subscription_id,
228-
literal_column("uuid('" + str(admin_oid) + "')").label("admin_oid"),
229-
literal_column("False").label("active"),
230-
over_time.c.desired_status,
231-
over_time.c.desired_status_info,
232-
]
221+
literal_column("1").label("reason_enum"),
222+
over_time.c.subscription_id,
223+
literal_column("uuid('" + str(admin_oid) + "')").label("admin_oid"),
224+
literal_column("False").label("active"),
225+
over_time.c.desired_status,
226+
over_time.c.desired_status_info,
233227
)
234228
.union(
235229
select(
236-
[
237-
literal_column("2").label("reason_enum"),
238-
over_budget.c.subscription_id,
239-
literal_column("uuid('" + str(admin_oid) + "')").label("admin_oid"),
240-
literal_column("False").label("active"),
241-
over_budget.c.desired_status,
242-
over_budget.c.desired_status_info,
243-
]
230+
literal_column("2").label("reason_enum"),
231+
over_budget.c.subscription_id,
232+
literal_column("uuid('" + str(admin_oid) + "')").label("admin_oid"),
233+
literal_column("False").label("active"),
234+
over_budget.c.desired_status,
235+
over_budget.c.desired_status_info,
244236
)
245237
)
246238
.alias()
247239
)
248240

249241
over_time_or_over_budget_reason = (
250242
select(
251-
[
252-
over_time_or_over_budget.c.subscription_id,
253-
over_time_or_over_budget.c.admin_oid,
254-
over_time_or_over_budget.c.active,
255-
case(
256-
[
257-
(
258-
func.sum(over_time_or_over_budget.c.reason_enum) == 1,
259-
cast(BillingStatus.EXPIRED, Enum(BillingStatus)),
260-
),
261-
(
262-
func.sum(over_time_or_over_budget.c.reason_enum) == 2,
263-
cast(BillingStatus.OVER_BUDGET, Enum(BillingStatus)),
264-
),
265-
(
266-
func.sum(over_time_or_over_budget.c.reason_enum) == 3,
267-
cast(
268-
BillingStatus.OVER_BUDGET_AND_EXPIRED,
269-
Enum(BillingStatus),
270-
),
271-
),
272-
],
273-
).label("reason"),
274-
over_time_or_over_budget.c.desired_status_info.label("old_reason"),
275-
over_time_or_over_budget.c.desired_status.label("old_desired_status"),
276-
]
243+
over_time_or_over_budget.c.subscription_id,
244+
over_time_or_over_budget.c.admin_oid,
245+
over_time_or_over_budget.c.active,
246+
case(
247+
(
248+
func.sum(over_time_or_over_budget.c.reason_enum) == 1,
249+
cast(BillingStatus.EXPIRED, Enum(BillingStatus)),
250+
),
251+
(
252+
func.sum(over_time_or_over_budget.c.reason_enum) == 2,
253+
cast(BillingStatus.OVER_BUDGET, Enum(BillingStatus)),
254+
),
255+
(
256+
func.sum(over_time_or_over_budget.c.reason_enum) == 3,
257+
cast(
258+
BillingStatus.OVER_BUDGET_AND_EXPIRED,
259+
Enum(BillingStatus),
260+
),
261+
),
262+
).label("reason"),
263+
over_time_or_over_budget.c.desired_status_info.label("old_reason"),
264+
over_time_or_over_budget.c.desired_status.label("old_desired_status"),
277265
)
278266
.group_by(
279267
over_time_or_over_budget.c.subscription_id,
@@ -289,13 +277,11 @@ async def refresh_desired_states(
289277
# desired status or has the wrong reason or a missing reason
290278
over_time_or_over_budget_desired_on = (
291279
select(
292-
[
293-
over_time_or_over_budget_reason.c.subscription_id,
294-
over_time_or_over_budget_reason.c.admin_oid,
295-
over_time_or_over_budget_reason.c.active,
296-
over_time_or_over_budget_reason.c.reason,
297-
over_time_or_over_budget_reason.c.old_reason,
298-
]
280+
over_time_or_over_budget_reason.c.subscription_id,
281+
over_time_or_over_budget_reason.c.admin_oid,
282+
over_time_or_over_budget_reason.c.active,
283+
over_time_or_over_budget_reason.c.reason,
284+
over_time_or_over_budget_reason.c.old_reason,
299285
)
300286
.where(
301287
or_(
@@ -319,12 +305,10 @@ async def refresh_desired_states(
319305
status_table.c.reason,
320306
],
321307
select(
322-
[
323-
over_time_or_over_budget_desired_on.c.subscription_id,
324-
over_time_or_over_budget_desired_on.c.admin_oid,
325-
over_time_or_over_budget_desired_on.c.active,
326-
over_time_or_over_budget_desired_on.c.reason,
327-
]
308+
over_time_or_over_budget_desired_on.c.subscription_id,
309+
over_time_or_over_budget_desired_on.c.admin_oid,
310+
over_time_or_over_budget_desired_on.c.active,
311+
over_time_or_over_budget_desired_on.c.reason,
328312
),
329313
)
330314

@@ -353,17 +337,15 @@ async def refresh_desired_states(
353337
# but aren't currently. These are all of our subscriptions
354338
# that are disabled but aren't over time or budget.
355339
should_be_enabled_but_are_not = select(
356-
[
357-
summaries.c.subscription_id,
358-
literal_column("uuid('" + str(admin_oid) + "')"),
359-
literal_column("True"),
360-
literal_column("NULL"),
361-
]
340+
summaries.c.subscription_id,
341+
literal_column("uuid('" + str(admin_oid) + "')"),
342+
literal_column("True"),
343+
literal_column("NULL"),
362344
).where(
363345
and_(
364346
not_(
365347
summaries.c.subscription_id.in_(
366-
select([over_time_or_over_budget.c.subscription_id])
348+
select(over_time_or_over_budget.c.subscription_id)
367349
)
368350
),
369351
or_(

0 commit comments

Comments
 (0)