Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve view functions in facilities blueprint #658

Merged
merged 22 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f5f7b18
[web] restructure room creation and implement custom error handling
lukasjuhrich Aug 1, 2023
225babd
[web] improve `determine_building_or_404` usage and logic
lukasjuhrich Aug 1, 2023
ac0c069
[web] extract address fields default filling to CreateAddressForm
lukasjuhrich Aug 1, 2023
bcc74ac
[web] restructure facilities.room_edit
lukasjuhrich Aug 2, 2023
4be5d6b
[ci] check for blueprint return type hint with semgrep rule
lukasjuhrich Aug 2, 2023
dff7042
[web] type hint `blueprint.facilities` endpoints
lukasjuhrich Aug 2, 2023
21371d2
[ci] remove temporary semgrep rule
lukasjuhrich Aug 10, 2023
90a032b
[web] restructure `facilities.patch_port_create`
lukasjuhrich Aug 10, 2023
4991e15
[web] restructure `facilities.patch_port_edit`
lukasjuhrich Aug 10, 2023
f2ad08b
[web] restructure `facilities.patch_port_delete`
lukasjuhrich Aug 10, 2023
fc98441
[web] add type hints to `app` and `finance` blueprint
lukasjuhrich Aug 10, 2023
f0c16da
[web] add type hints for `user` blueprint
lukasjuhrich Aug 10, 2023
d6f2712
[web] add type hints to `host` and `infrastructure` blueprints
lukasjuhrich Aug 10, 2023
90de086
[web] add type hints for remaining view functions
lukasjuhrich Aug 10, 2023
b966df4
[web] enable mypy checking on `facilities` blueprint
lukasjuhrich Aug 10, 2023
ae781a0
[web] enforce stricter typing rules for `blueprints.facilities`
lukasjuhrich Aug 10, 2023
dba5d21
[web] fix residual mypy errors
lukasjuhrich Aug 11, 2023
12b87bb
[ci] pin mypy to ~1.5.0 and fix unused suppression
lukasjuhrich Aug 11, 2023
b421077
[web] properly type sentry debug endpoint
lukasjuhrich Aug 11, 2023
af4da9e
[web] rename `handle_errors` → `abort_on_error`
lukasjuhrich Aug 11, 2023
51276a3
[web] extract room checks into `get_room_or_404`
lukasjuhrich Aug 11, 2023
7268312
Remove unused import
lukasjuhrich Aug 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pycroft/lib/facilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ def make_sort_key(building: Building) -> tuple[str, str | tuple[int, str]]:
return sorted(buildings, key=make_sort_key)


def determine_building(shortname: str | None = None, id: int | None = None) -> Building:
def determine_building(
shortname: str | None = None, id: int | None = None
) -> Building | None:
"""Determine building from shortname or id in this order.

:param shortname: The short name of the building
Expand All @@ -249,4 +251,4 @@ def determine_building(shortname: str | None = None, id: int | None = None) -> B
return t.cast(Building, session.session.scalars(stmt).one())
if id:
return session.session.get(Building, id)
raise ValueError("Either shortname or id must be given to identify the building!")
return None
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ files = [
"pycroft/external_services",
"ldap_sync",
"web/blueprints/finance",
"web/blueprints/facilities",
"web/blueprints/__init__.py",
"web/blueprints/access.py",
"web/blueprints/navigation.py",
Expand Down Expand Up @@ -54,6 +55,7 @@ module = [
"pycroft.helpers.utc",
"web.blueprints",
"web.blueprints.access",
"web.blueprints.facilities",
"web.blueprints.navigation",
]
disallow_untyped_defs = true
Expand Down
2 changes: 1 addition & 1 deletion requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ git+https://github.com/lukasjuhrich/sqlalchemy_schemadisplay.git@master#egg=sqla
sphinx~=5.1.1
sphinx-autobuild~=2021.3.14
git+https://github.com/agdsn/guzzle_sphinx_theme.git@977d49fcbdf2b3df9660d813d4b33369391923e1#egg=guzzle-sphinx-theme
mypy>=0.961
mypy~=1.5.0
celery-types~=0.9.3
types-jsonschema~=4.3.0
types-passlib~=1.7.7
Expand Down
7 changes: 3 additions & 4 deletions tests/frontend/test_facilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ def test_get_no_building(self, ep, client):
client.assert_ok(ep)

def test_get_wrong_id(self, ep, client, building):
with client.flashes_message("Gebäude.*nicht gefunden", "error"):
client.assert_url_redirects(url_for(ep, building_id=999))
client.assert_url_response_code(url_for(ep, building_id=999), code=404)

def test_get_correct_building_id(self, ep, client, building):
with client.renders_template("generic_form.html"):
Expand Down Expand Up @@ -253,8 +252,8 @@ def used_address(self, class_session, room):
f.UserFactory(address=room.address, room=room)

def test_get_no_room(self, ep, client):
with client.flashes_message("Raum.*nicht gefunden", "error"):
client.assert_url_redirects(url_for(ep, room_id=999))
with client.flashes_message("Raum.*existiert nicht", "error"):
client.assert_url_response_code(url_for(ep, room_id=999), code=404)

def test_get(self, url, client):
with client.renders_template("generic_form.html"), client.flashes_message(
Expand Down
32 changes: 16 additions & 16 deletions tools/semgrep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,6 @@ rules:
paths:
include: ["web"]

- id: explicit-session-begin_nested
patterns:
- pattern: |
with $CM: ...
- metavariable-pattern:
metavariable: "$CM"
pattern: handle_errors($SESS, $...ARGS)
- focus-metavariable: "$CM"
message: "Use `session.begin` explicitly instead of passing the session to `handle_errors`"
fix: |
handle_errors($...ARGS), $SESS.begin_nested()
languages: [python]
severity: ERROR
paths:
include: ["web"]

- id: commit-in-context-manager
patterns:
- pattern: |
Expand All @@ -214,3 +198,19 @@ rules:
severity: ERROR
paths:
include: ["web", "pycroft"]

- id: untyped-flask-endpount
patterns:
- pattern: |
@$APP.route($...ROUTEARGS)
def $EP($...ARGS): ...
- pattern-not: |
@$APP.route($...ROUTEARGS)
def $EP($...ARGS) -> $RET: ...
- focus-metavariable: "$EP"
# no fix, because we would lose the decorator (and I can't make `pattern-inside` work correctly)
message: blueprint functions should be annotated with `ResponseReturnValue`
languages: [ python ]
severity: ERROR
paths:
include: ["web"]
6 changes: 4 additions & 2 deletions web/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import typing as t

import jinja2.ext
import logging
Expand All @@ -8,6 +9,7 @@
g,
make_response,
)
from flask.typing import ResponseValue
from flask_babel import Babel
from flask_login import current_user
from jinja2 import select_autoescape
Expand Down Expand Up @@ -146,11 +148,11 @@ def errorpage(e):
return render_template('error.html', error=message), code

@app.route('/')
def redirect_to_index():
def redirect_to_index() -> ResponseValue:
return redirect(url_for('user.overview'))

@app.route('/debug-sentry')
def debug_sentry():
def debug_sentry() -> t.NoReturn:
app.logger.warning("Someone used the debug-sentry endpoint! Also, this is a test warning.")
app.logger.info("An info log for inbetween")
app.logger.error("Someone used the debug-sentry endpoint! Also, this is a test error.",
Expand Down
Loading
Loading