Skip to content

Commit

Permalink
some linting and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sondregronas committed Oct 2, 2023
1 parent 6d83b60 commit fcd995c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
8 changes: 4 additions & 4 deletions BookingSystem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
REGEX_ID = r'^(?:(?![\s])[a-zA-Z0-9_\s\-]*[a-zA-Z0-9_\-]+)$'
REGEX_ITEM = r'^(?:(?![\s])[ÆØÅæøåa-zA-Z0-9_\s\-]*[ÆØÅæøåa-zA-Z0-9_\-]+)$'

MIN_DAYS = int(os.getenv('MIN_DAYS', 1))
MAX_DAYS = int(os.getenv('MAX_DAYS', 14))
MIN_LABELS = int(os.getenv('MIN_LABELS', 0))
MAX_LABELS = int(os.getenv('MAX_LABELS', 10))
MIN_DAYS = int(os.getenv('MIN_DAYS', '1'))
MAX_DAYS = int(os.getenv('MAX_DAYS', '14'))
MIN_LABELS = int(os.getenv('MIN_LABELS', '0'))
MAX_LABELS = int(os.getenv('MAX_LABELS', '10'))

# Debugging / development / testing
DEBUG = os.getenv('DEBUG', '').lower() == 'true'
Expand Down
14 changes: 6 additions & 8 deletions BookingSystem/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def add_item() -> flask.Response:
item_dict = sanitize(validation_map, flask.request.form)
# END: Validation

item = {key: val for key, val in item_dict.items()}
item = Item(**item)
item = Item(**item_dict)
inventory.add(item)
return flask.Response(f'La til {item.id} i databasen.', status=201)

Expand All @@ -99,8 +98,7 @@ def edit_item(item_id: str) -> flask.Response:
item_dict = sanitize(validation_map, flask.request.form, {'id': item_id})
# END: Validation

item = {key: val for key, val in item_dict.items()}
item = Item(**item)
item = Item(**item_dict)
inventory.edit(item_id, item)
return flask.Response(f'Redigerte {item_id} i databasen.', status=200)

Expand Down Expand Up @@ -142,7 +140,7 @@ def print_label(item_id: str) -> flask.Response: # pragma: no cover
item = inventory.get(item_id)
url = f'{LABEL_SERVER}/print?id={item.id}&name={item.name}&variant={variant}&count={count}'
try:
response = requests.post(url)
response = requests.post(url, timeout=5)
except requests.exceptions.RequestException as e:
return flask.Response(str(e), status=500)
return flask.Response(response.text, status=response.status_code)
Expand Down Expand Up @@ -239,7 +237,7 @@ def register_student() -> flask.Response:
@handle_api_exception
def update_groups() -> flask.Response:
"""Update a class in the database."""
new_groups = list()
new_groups = []
for group in flask.request.form.get('groups').split('\n'):
if not group.strip():
continue
Expand All @@ -261,7 +259,7 @@ def update_groups() -> flask.Response:
@handle_api_exception
def update_categories() -> flask.Response:
"""Update every category in the database."""
new_categories = list()
new_categories = []
for category in flask.request.form.get('categories').split('\n'):
if not category.strip():
continue
Expand Down Expand Up @@ -293,7 +291,7 @@ def registrer_avvik() -> flask.Response:

audits.audit('AVVIK', markupsafe.escape(txt))
teams.send_deviation(txt)
return flask.Response(f'Avvik ble sendt til videre oppfølging', status=200)
return flask.Response('Avvik ble sendt til videre oppfølging', status=200)


@api.route('/send_report', methods=['POST'])
Expand Down
4 changes: 2 additions & 2 deletions BookingSystem/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ def context_processor() -> dict:
FQDN=urlparse(flask.request.base_url).hostname, )

@app.errorhandler(401)
def unauthorized(_) -> tuple[flask.Response, int]:
def unauthorized_401(_) -> tuple[flask.Response, int]:
flask.session.clear()
if flask.request.url != flask.url_for('app.index', _external=True):
logger.warning(f'Unauthorized access: {flask.request.url} from {flask.request.remote_addr}')
return flask.redirect(flask.url_for('app.login')), 302

@app.errorhandler(403)
def unauthorized(_) -> tuple[flask.Response, int]:
def unauthorized_403(_) -> tuple[flask.Response, int]:
flask.session.clear()
if flask.request.url != flask.url_for('app.index', _external=True):
logger.warning(f'Unauthorized access: {flask.request.url} from {flask.request.remote_addr}')
Expand Down
6 changes: 3 additions & 3 deletions BookingSystem/sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ def groupname(text: str) -> bool:
case VALIDATORS.ITEM_LIST_EXISTS:
# Check if the item list exists
ids = form.getlist(key)
all_ids = [i for i in inventory.get_all_ids()]
all_ids = inventory.get_all_ids()
if not all(i in all_ids for i in ids):
raise APIException(f'En eller flere gjenstander finnes ikke ({form.getlist(key)})')
if not ids:
raise APIException(f'Ingen gjenstander valgt')
raise APIException('Ingen gjenstander valgt')

case VALIDATORS.EMAIL:
# Check if the email is valid
Expand Down Expand Up @@ -186,7 +186,7 @@ def sanitize(validation_map: dict[any: VALIDATORS | MINMAX],
"""
try:
_sanitize_form(validation_map, form, data)
sanitized = dict()
sanitized = {}

for key, sanitizer in validation_map.items():
# Remove keys that are not in the validation map
Expand Down
4 changes: 2 additions & 2 deletions BookingSystem/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def formatted_overdue_items(items: list) -> str:
'<table bordercolor="black" border="1" style="width: 100%;">' +
'<tr style="background-color: teal; color: white;">' +
f'<th>&nbsp;Tilhørighet: {association or "Ansatt"}</th>' +
f'</tr>\n' +
'</tr>\n' +
'\n'.join([f'<tr><td>&nbsp;{item}</td></tr>' for item in pairs[association]]) +
f'</table>'
'</table>'
for association in pairs.keys()]
return '<blockquote style="border-color: #FF0000;">' + \
'<br>'.join(strings) + \
Expand Down

0 comments on commit fcd995c

Please sign in to comment.