Skip to content

Commit 153426b

Browse files
committed
pass ruff?
1 parent 47aa30f commit 153426b

File tree

13 files changed

+75
-162
lines changed

13 files changed

+75
-162
lines changed

auth/api.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ def create_api_key_ui() -> Response:
281281
return redirect(url_for("auth_ui.get_api_keys_ui"))
282282

283283

284-
def handle_api_key_action(
285-
action: Callable[[int], APIKey | None], verb: str
286-
) -> Response:
284+
def handle_api_key_action(action: Callable[[int], APIKey | None], verb: str) -> Response:
287285
key_id = request.form.get("id")
288286
if not key_id:
289287
flash("API key ID is required", "error")

auth/oauth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def logout() -> Response:
9393
session.clear()
9494
return redirect(
9595
"https://auth.uwcs.co.uk/realms/uwcs/protocol/openid-connect/logout"
96-
+ f"?post_logout_redirect_uri={url_for('index', _external=True, _scheme=scheme)}" # noqa: E501
96+
+ f"?post_logout_redirect_uri={url_for('index', _external=True, _scheme=scheme)}"
9797
+ f"&id_token_hint={id_token}"
9898
)
9999
# if no id token, just clear session and redirect to index

events/api.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
events_api_bp = Blueprint("events_api", __name__, url_prefix="/api/events")
2525

2626

27-
@events_api_bp.route(
28-
"/<int:year>/<int:term>/<sint:week>/<string:slug>/", methods=["GET"]
29-
)
27+
@events_api_bp.route("/<int:year>/<int:term>/<sint:week>/<string:slug>/", methods=["GET"])
3028
def get_event(year: int, term: int, week: int, slug: str) -> tuple[Response, int]:
3129
"""Get a specific event by year, term, week, and slug.
3230
---
@@ -98,9 +96,7 @@ def get_event_by_id_api(event_id: int) -> tuple[Response, int]:
9896
@events_api_bp.route("/<int:year>/<int:term>/<sint:week>/", methods=["GET"])
9997
@events_api_bp.route("/<int:year>/<int:term>/", methods=["GET"])
10098
@events_api_bp.route("/<int:year>/", methods=["GET"])
101-
def get_events(
102-
year: int, term: int | None = None, week: int | None = None
103-
) -> tuple[Response, int]:
99+
def get_events(year: int, term: int | None = None, week: int | None = None) -> tuple[Response, int]:
104100
"""Get all events for a specific year, term, and week.
105101
---
106102
parameters:
@@ -318,7 +314,7 @@ def create_event_api() -> tuple[Response, int]: # noqa: PLR0911
318314
description: Bad request, missing or invalid data.
319315
403:
320316
description: Forbidden.
321-
""" # noqa: E501
317+
"""
322318
data = request.get_json()
323319
if not data:
324320
return jsonify({"error": "No data provided"}), 400
@@ -443,7 +439,7 @@ def create_repeat_event_api() -> tuple[Response, int]: # noqa: PLR0911
443439
description: Bad request, missing or invalid data.
444440
403:
445441
description: Forbidden.
446-
""" # noqa: E501
442+
"""
447443
data = request.get_json()
448444
if not data:
449445
return jsonify({"error": "No data provided"}), 400
@@ -694,9 +690,7 @@ def get_tags() -> tuple[Response, int]:
694690
description: No tags found.
695691
"""
696692
query_string = request.args.get("query", "").lower()
697-
tags = (
698-
get_tags_by_string(query_string, limit=-1) if query_string else get_all_tags()
699-
)
693+
tags = get_tags_by_string(query_string, limit=-1) if query_string else get_all_tags()
700694
if not tags:
701695
return jsonify({"error": "No tags found"}), 404
702696
return jsonify([tag.to_dict() for tag in tags]), 200

events/ui.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,14 @@ def get_event_from_form(form_data: ImmutableMultiDict) -> dict:
3939
"description": form_data["description"],
4040
"draft": "draft" in form_data,
4141
"location": form_data["location"],
42-
"location_url": (
43-
form_data["location_url"] if form_data["location_url"] != "" else None
44-
),
42+
"location_url": (form_data["location_url"] if form_data["location_url"] != "" else None),
4543
"icon": form_data["icon"] if form_data["icon"] != "" else None,
46-
"text_colour": (
47-
form_data["text_colour"] if form_data["text_colour"] != "" else None
48-
),
49-
"color_colour": (
50-
form_data["color_colour"] if form_data["color_colour"] != "" else None
51-
),
44+
"text_colour": (form_data["text_colour"] if form_data["text_colour"] != "" else None),
45+
"color_colour": (form_data["color_colour"] if form_data["color_colour"] != "" else None),
5246
"times": zip(
53-
form_data.getlist("start_time[]"), form_data.getlist("end_time[]")
47+
form_data.getlist("start_time[]"),
48+
form_data.getlist("end_time[]"),
49+
strict=True,
5450
),
5551
"duration": (form_data["duration"] if form_data["duration"] != "" else None),
5652
"tags": [tag for tag in form_data.getlist("tags[]") if tag],
@@ -61,14 +57,10 @@ def parse_form_data(form_data: ImmutableMultiDict) -> dict | str:
6157
"""Parse event from form data"""
6258
# parse colour
6359
text_colour = (
64-
form_data["text_colour"].strip().lower()
65-
if form_data["text_colour"] != ""
66-
else None
60+
form_data["text_colour"].strip().lower() if form_data["text_colour"] != "" else None
6761
)
6862
color_colour = (
69-
form_data["color_colour"].strip().lower()
70-
if form_data["color_colour"] != ""
71-
else None
63+
form_data["color_colour"].strip().lower() if form_data["color_colour"] != "" else None
7264
)
7365

7466
if (error := validate_colour(text_colour, color_colour)) is not None:
@@ -78,9 +70,7 @@ def parse_form_data(form_data: ImmutableMultiDict) -> dict | str:
7870
colour = text_colour if text_colour else color_colour
7971

8072
# parse start and end times
81-
start_times = [
82-
get_datetime_from_string(t) for t in form_data.getlist("start_time[]")
83-
]
73+
start_times = [get_datetime_from_string(t) for t in form_data.getlist("start_time[]")]
8474
for start_time in start_times:
8575
if isinstance(start_time, str):
8676
return start_time
@@ -99,9 +89,7 @@ def parse_form_data(form_data: ImmutableMultiDict) -> dict | str:
9989
"description": form_data["description"],
10090
"draft": "draft" in form_data,
10191
"location": form_data["location"],
102-
"location_url": (
103-
form_data["location_url"] if form_data["location_url"] != "" else None
104-
),
92+
"location_url": (form_data["location_url"] if form_data["location_url"] != "" else None),
10593
"icon": form_data["icon"] if form_data["icon"] != "" else None,
10694
"colour": colour,
10795
"tags": tags,
@@ -153,10 +141,7 @@ def create() -> str | Response:
153141
)
154142

155143
# attempt to create the event
156-
if "start_time" in data:
157-
event = create_event(**data)
158-
else:
159-
event = create_repeat_event(**data)
144+
event = create_event(**data) if "start_time" in data else create_repeat_event(**data)
160145

161146
# if failed, return the form with an error
162147
if isinstance(event, str):
@@ -258,9 +243,7 @@ def edit(year: int, term: int, week: int, slug: str) -> str | Response:
258243
)
259244

260245

261-
@events_ui_bp.route(
262-
"/<int:year>/<int:term>/<sint:week>/<string:slug>/delete/", methods=["POST"]
263-
)
246+
@events_ui_bp.route("/<int:year>/<int:term>/<sint:week>/<string:slug>/delete/", methods=["POST"])
264247
@is_exec_wrapper
265248
def delete(year: int, term: int, week: int, slug: str) -> Response:
266249
"""Delete an event by its year, term, week, and, slug"""
@@ -307,9 +290,7 @@ def view_list(year: int, term: int | None = None, week: int | None = None) -> st
307290

308291
events = group_events(events)
309292

310-
return render_template(
311-
"events/list.html", events=events, year=year, term=term, week=week
312-
)
293+
return render_template("events/list.html", events=events, year=year, term=term, week=week)
313294

314295

315296
@events_ui_bp.route("/stardust/front/")

events/utils.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
LONDON = timezone("Europe/London")
1818

19+
1920
def get_datetime_from_string(date_str: str) -> datetime | str:
2021
"""
2122
Convert datetime str in the format 'YYYY-MM-DDTHH:MM'
@@ -250,7 +251,7 @@ def create_repeat_event( # noqa: PLR0913
250251
) -> list[Event] | str:
251252
"""Create multiple events at once"""
252253
events = [] # the created events
253-
for start_time, end_time in zip(start_times, end_times):
254+
for start_time, end_time in zip(start_times, end_times, strict=True):
254255
# iterate through start_times and create events
255256
event = create_event(
256257
name,
@@ -305,9 +306,9 @@ def get_event_by_id(event_id: int) -> Event | None:
305306
def get_event_by_slug(year: int, term: int, week: int, slug: str) -> Event | None:
306307
"""Get an event by slug"""
307308
return Event.query.filter(
308-
Event.week.has(academic_year=year, term=term, week=week),
309-
Event.slug == slug, # type: ignore
310-
).first()
309+
Event.week.has(academic_year=year, term=term, week=week),
310+
Event.slug == slug, # type: ignore
311+
).first()
311312

312313

313314
# shamelessly stolen from docs (https://python-markdown.github.io/extensions/api/#example_3)
@@ -405,9 +406,7 @@ def group_events(events: list[Event]) -> list[dict]:
405406
"events": [prepare_event(event) for event in day_events],
406407
}
407408
)
408-
week_list.append(
409-
{"week": week, "days": day_list, "start_date": start_date}
410-
)
409+
week_list.append({"week": week, "days": day_list, "start_date": start_date})
411410
term_list.append({"term": term, "weeks": week_list})
412411
year_list.append({"year": year, "terms": term_list})
413412
return year_list
@@ -430,9 +429,8 @@ def get_events_by_time(
430429
query = query.filter(Event.draft.is_(False)) # type: ignore
431430

432431
# order by start_time, end_time, and name
433-
return query.order_by(
434-
Event.start_time, Event.end_time, Event.name # type: ignore
435-
).all()
432+
return query.order_by(Event.start_time, Event.end_time, Event.name).all() # type: ignore
433+
436434

437435
def get_upcoming_events(include_drafts: bool = False) -> list[Event]:
438436
"""Get all events in this week, and future weeks"""
@@ -447,9 +445,7 @@ def get_upcoming_events(include_drafts: bool = False) -> list[Event]:
447445
if not include_drafts:
448446
query = query.filter(Event.draft.is_(False)) # type: ignore
449447

450-
return query.order_by(
451-
Event.start_time, Event.end_time, Event.name # type: ignore
452-
).all()
448+
return query.order_by(Event.start_time, Event.end_time, Event.name).all() # type: ignore
453449

454450

455451
def get_all_events(include_drafts: bool = False) -> list[Event]:
@@ -459,9 +455,7 @@ def get_all_events(include_drafts: bool = False) -> list[Event]:
459455
if not include_drafts:
460456
query = query.filter(Event.draft.is_(False)) # type: ignore
461457

462-
return query.order_by(
463-
Event.start_time, Event.end_time, Event.name # type: ignore
464-
).all()
458+
return query.order_by(Event.start_time, Event.end_time, Event.name).all() # type: ignore
465459

466460

467461
def get_week_events() -> list[Event]:
@@ -538,9 +532,7 @@ def edit_event( # noqa: PLR0913
538532
event.description = description if description is not _KEEP else event.description
539533
event.draft = draft if draft is not _KEEP else event.draft
540534
event.location = location if location is not _KEEP else event.location
541-
event.location_url = (
542-
location_url if location_url is not _KEEP else event.location_url
543-
)
535+
event.location_url = location_url if location_url is not _KEEP else event.location_url
544536

545537
if icon is not _KEEP:
546538
event.icon = icon.lower() if icon is not None else event.icon # type: ignore

exec/publicity.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ def poisson_disc_samples(
8585
if not (0 <= new_point[0] < width and 0 <= new_point[1] < height):
8686
continue
8787

88-
grid_x, grid_y = int(new_point[0] // cell_size), int(
89-
new_point[1] // cell_size
90-
)
88+
grid_x, grid_y = int(new_point[0] // cell_size), int(new_point[1] // cell_size)
9189
too_close = False
9290

9391
for i in range(max(0, grid_x - 2), min(grid_width, grid_x + 3)):
@@ -229,9 +227,7 @@ def num(num: str) -> float | int:
229227
elif cmd in "Aa":
230228
# arc commands have boolean parameters
231229
rx, ry, angle, la, sw, px, py = params
232-
path_data.append(
233-
command_map[cmd](rx, ry, angle, bool(la), bool(sw), px, py)
234-
)
230+
path_data.append(command_map[cmd](rx, ry, angle, bool(la), bool(sw), px, py))
235231
else:
236232
path_data.append(command_map[cmd](*params))
237233

@@ -259,9 +255,7 @@ def get_events(start: Week, end: Week) -> list[dict]:
259255
return group_events(events)
260256

261257

262-
def backtrack(
263-
groups: list[tuple[int, list[int]]], grid: list[list[int]]
264-
) -> list[list[int]] | None:
258+
def backtrack(groups: list[tuple[int, list[int]]], grid: list[list[int]]) -> list[list[int]] | None:
265259
"""backtracking algorithm to fit items into grid"""
266260

267261
# base case, just return grid, we good :)
@@ -277,9 +271,7 @@ def backtrack(
277271
for col in range(num_cols - width + 1):
278272
# check if space is free
279273
is_free = all(
280-
grid[i][j] == -1
281-
for i in range(row, row + height)
282-
for j in range(col, col + width)
274+
grid[i][j] == -1 for i in range(row, row + height) for j in range(col, col + width)
283275
)
284276

285277
# try again if not free
@@ -385,9 +377,7 @@ def get_event_circle(event: dict) -> svg.G:
385377
title_size = CIRCLE_SIZE / 8 if len(title) == 1 else CIRCLE_SIZE / 10
386378
location_size = CIRCLE_SIZE / 13 if len(location) == 1 else CIRCLE_SIZE / 16
387379
time_size = location_size
388-
font_sizes = (
389-
[title_size] * len(title) + [location_size] * len(location) + [time_size]
390-
)
380+
font_sizes = [title_size] * len(title) + [location_size] * len(location) + [time_size]
391381

392382
elements: list[svg.Element] = [svg.Circle(cx=0, cy=0, r=radius, fill=colour)]
393383

@@ -415,7 +405,7 @@ def get_event_circle(event: dict) -> svg.G:
415405

416406
lines = title + location + [time_str]
417407
current_y = text_top
418-
for line, size in zip(lines, font_sizes):
408+
for line, size in zip(lines, font_sizes, strict=True):
419409
elements.append(
420410
svg.Text(
421411
text=line,
@@ -472,10 +462,8 @@ def get_socials(width: float, height: float) -> svg.G:
472462
class_=["title"],
473463
),
474464
]
475-
for i, (icon, social_text) in enumerate(zip(icons, text), start=2):
476-
path, icon_width, icon_height, min_x, min_y = convert_path_to_list(
477-
icon_paths[icon]
478-
)
465+
for i, (icon, social_text) in enumerate(zip(icons, text, strict=True), start=2):
466+
path, icon_width, icon_height, min_x, min_y = convert_path_to_list(icon_paths[icon])
479467
scale = desired_icon_width / icon_width
480468
cx = (min_x + icon_width / 2) * scale
481469
cy = (min_y + icon_height / 2) * scale
@@ -550,9 +538,7 @@ def create_single_week( # noqa: PLR0912, PLR0915
550538
num_rows, num_cols = len(grid), len(grid[0])
551539

552540
# find position and sizing of grid
553-
grid_height = (
554-
(POST_HEIGHT / 1.5) if num_rows < 3 else (POST_HEIGHT / 1.35) # noqa: PLR2004
555-
)
541+
grid_height = (POST_HEIGHT / 1.5) if num_rows < 3 else (POST_HEIGHT / 1.35) # noqa: PLR2004
556542
grid_width = POST_WIDTH
557543
grid_top = POST_HEIGHT / 4 + (POST_HEIGHT - POST_HEIGHT / 4 - grid_height) / 2
558544

@@ -639,9 +625,7 @@ def create_single_week( # noqa: PLR0912, PLR0915
639625

640626
# work out extra translation required
641627
translate_x = base_x + row * cell_width + cell_width / 2
642-
translate_y = (
643-
base_y + col * cell_height + cell_height / 2 + DAY_TEXT_HEIGHT / 2
644-
)
628+
translate_y = base_y + col * cell_height + cell_height / 2 + DAY_TEXT_HEIGHT / 2
645629

646630
# if spanning multiple columns, pull towards centre
647631
pull_y = 0 if col_width == 1 else pull_factor
@@ -682,7 +666,7 @@ def create_multi_week(events: list[dict], start: Week, end: Week) -> list[svg.El
682666
]
683667

684668
# find the list of weeks in term
685-
term_weeks = events[start.academic_year]["terms"][0]["weeks"]
669+
term_weeks = events[start.academic_year]["terms"][0]["weeks"] # noqa: F841
686670

687671
return elements
688672

@@ -788,8 +772,4 @@ def create_svg(start: Week, end: Week) -> str:
788772
else:
789773
elements.extend(create_multi_week(events, start, end))
790774

791-
return str(
792-
svg.SVG(
793-
elements=elements, viewBox=svg.ViewBoxSpec(0, 0, POST_WIDTH, POST_HEIGHT)
794-
)
795-
)
775+
return str(svg.SVG(elements=elements, viewBox=svg.ViewBoxSpec(0, 0, POST_WIDTH, POST_HEIGHT)))

exec/ui.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@ def publicity() -> str:
3838
messgae += "end "
3939
messgae += "week"
4040
flash(messgae, "danger")
41-
return render_template(
42-
"exec/publicity.html", year=year, term=term, years=get_years()
43-
)
41+
return render_template("exec/publicity.html", year=year, term=term, years=get_years())
4442

4543
try:
4644
svg = create_svg(start_week, end_week)
4745
except ValueError as e:
4846
flash(f"Error creating publicity SVG: {e}", "danger")
49-
return render_template(
50-
"exec/publicity.html", year=year, term=term, years=get_years()
51-
)
47+
return render_template("exec/publicity.html", year=year, term=term, years=get_years())
5248

5349
return render_template(
5450
"exec/publicity.html",

0 commit comments

Comments
 (0)