Skip to content

Commit

Permalink
[web] extract room checks into get_room_or_404
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Aug 14, 2023
1 parent 821750c commit f13afde
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions web/blueprints/facilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,7 @@ def default_response() -> ResponseReturnValue:
@bp.route('/room/<int:room_id>/create', methods=['GET', 'POST'])
@access.require('facilities_change')
def room_edit(room_id: int) -> ResponseReturnValue:
room = session.session.get(Room, room_id)

if not room:
flash(f"Raum mit ID {room_id} nicht gefunden!", "error")
return redirect(url_for('.overview'))
room = get_room_or_404(room_id)

form = EditRoomForm(building=room.building.short_name,
level=room.level,
Expand Down Expand Up @@ -498,15 +494,18 @@ def default_response() -> ResponseReturnValue:




@bp.route('/room/<int:room_id>', methods=['GET', 'POST'])
def room_show(room_id: int) -> ResponseReturnValue:
def get_room_or_404(room_id: int) -> Room:
room = session.session.get(Room, room_id)

if room is None:
flash("Zimmer existiert nicht!", 'error')
flash(f"Raum mit id {room_id} existiert nicht", "error")
abort(404)
return room



@bp.route('/room/<int:room_id>', methods=['GET', 'POST'])
def room_show(room_id: int) -> ResponseReturnValue:
room = get_room_or_404(room_id)
form = RoomLogEntry()

if form.validate_on_submit():
Expand Down Expand Up @@ -542,21 +541,15 @@ def room_show(room_id: int) -> ResponseReturnValue:

@bp.route('/room/<int:room_id>/logs/json')
def room_logs_json(room_id: int) -> ResponseReturnValue:
room = session.session.get(Room, room_id)
if room is None:
abort(404)
room = get_room_or_404(room_id)
return TableResponse[LogTableRow](
items=[format_room_log_entry(entry) for entry in reversed(room.log_entries)]
).model_dump()


@bp.route('/room/<int:room_id>/patchpanel/json')
def room_patchpanel_json(room_id: int) -> ResponseReturnValue:
room = session.session.get(Room, room_id)

if not room:
abort(404)

room = get_room_or_404(room_id)
if not room.is_switch_room:
abort(400)

Expand Down

0 comments on commit f13afde

Please sign in to comment.