Skip to content

Commit

Permalink
tighten up index_student, UI tweaks, 404page, title
Browse files Browse the repository at this point in the history
  • Loading branch information
sondregronas committed Aug 8, 2023
1 parent fb4dc42 commit 7993e2d
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 11 deletions.
7 changes: 6 additions & 1 deletion BookingSystem/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import flask
import requests

import groups
import inventory
import mail
import user
Expand Down Expand Up @@ -170,11 +171,15 @@ def register_student() -> flask.Response:
con = sqlite3.connect(DATABASE)
cur = con.cursor()

selected_classroom = flask.request.form.get('classroom')
if selected_classroom not in groups.get_all():
return flask.abort(418) # I'm a teapot

data = {
'name': flask.session.get('user').name,
'email': flask.session.get('user').email,
'userid': flask.session.get('user').userid,
'classroom': flask.request.form.get('classroom'),
'classroom': selected_classroom,
'updated_at': datetime.now().isoformat(),
'expires_at': next_july().isoformat()
}
Expand Down
16 changes: 14 additions & 2 deletions BookingSystem/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os
from datetime import datetime

import api
import flask
from dateutil import parser
from flask_session import Session
from werkzeug.middleware.proxy_fix import ProxyFix

import api
import audits
import feide
import groups
Expand All @@ -16,6 +15,7 @@
import user
from __init__ import logger
from db import init_db
from flask_session import Session


def create_app() -> flask.Flask:
Expand Down Expand Up @@ -45,6 +45,10 @@ def _jinja2_filter_datetime(date, fmt='%d.%m.%Y') -> str:
def _jinja2_filter_strftime(date, fmt='%d.%m.%Y') -> str:
return datetime.fromtimestamp(float(date)).strftime(fmt)

@app.template_filter('split')
def _jinja2_filter_split(string, split_char=',') -> list:
return string.split(split_char)

@app.context_processor
def context_processor() -> dict:
return dict(regex_item=r'^(?:(?![\s])[ÆØÅæøåa-zA-Z0-9_\s\-]*[ÆØÅæøåa-zA-Z0-9_\-]+)$',
Expand All @@ -69,6 +73,14 @@ def unauthorized(_) -> flask.Response:
logger.warning(f'Unauthorized access: {flask.request.url} from {flask.request.remote_addr}')
return flask.redirect(flask.url_for('app.login'))

@app.errorhandler(404)
def page_not_found(_) -> str:
return flask.render_template('404.html')

@app.errorhandler(418)
def teapot(_) -> flask.Response:
return flask.redirect('https://www.youtube.com/watch?v=dQw4w9WgXcQ')

return app


Expand Down
12 changes: 12 additions & 0 deletions BookingSystem/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ def lender_association(self) -> str:
return 'Sjekk historikk'
return self.user.get('classroom') or 'Lærer'

@property
def classroom(self) -> str:
if '(' in self.lender_association:
return self.lender_association.split('(')[0].strip()
return self.lender_association

@property
def teacher(self) -> str:
if '(' in self.lender_association:
return self.lender_association.split('(')[1].strip(')')
return None

@property
def lender(self) -> str:
return f'{self.lender_name} ({self.lender_association})'
Expand Down
8 changes: 8 additions & 0 deletions BookingSystem/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends 'layout.html' %}

{% block content %}
<hgroup>
<h2>404 - Page not found</h2>
<h3>Siden du prøvde å nå finnes ikke, eller er fjernet.</h3>
</hgroup>
{% endblock %}
3 changes: 2 additions & 1 deletion BookingSystem/templates/booking.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ <h3>Dersom du ikke finner eleven i listen, må eleven logge inn på nettstedet o
<option value=""></option>
{% for user in users %}
<option value="{{ user.userid }}"
data-classroom="{% if user.classroom %}{{ user.classroom }}{% else %}Lærer{% endif %}">
data-classroom="
{% if user.classroom %}{{ user.classroom }}{% if user.teacher %} - {{ user.teacher }}{% endif %}{% else %}Lærer{% endif %}">
{{ user.name }}
</option>
{% endfor %}
Expand Down
3 changes: 2 additions & 1 deletion BookingSystem/templates/index_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ <h4>
<tr class="item-row item-row--overdue">
<td>{{ item.id }}</td>
<td>{{ item.name }} ({{ item.category }})</td>
<td>{{ item.lender }}</td>
<td>{{ item.lender_name }} <small> ({{ item.classroom }}{% if item.teacher %},
{{ item.teacher }}{% endif %})</small></td>
<td>{{ item.order_due_date|strftime }}</td>
</tr>
{% endfor %}
Expand Down
13 changes: 11 additions & 2 deletions BookingSystem/templates/index_student.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ <h3>Velkommen til utstyrsrommet! Før du kan bruke utstyrsrommet må du velge kl
</hgroup>

<form action="{{ url_for('api.register_student') }}" method="post">
<select name="classroom" id="classroom_select">
<option value="" disabled selected>
<select name="classroom" id="classroom_select" required
data-placeholder="">
<option value="" placeholder>
{% if session.user.classroom %}
Nåværende: {{ session.user.classroom }}
{% else %}
Expand Down Expand Up @@ -52,6 +53,14 @@ <h4>Ansvarsavtalen inkluderer økonomisk ansvar for utstyr som blir ødelagt ell
</div>
{% endif %}

<script>
$(document).ready(function () {
$('form').submit(function () {
cueAlert('Registrering fullført', 'Din bruker er blitt opprettet, og du har nå låst opp muligheten for å låne utstyr ut skoleåret.<br><br>Du trenger ikke gjøre mer på denne siden.', 'success');
});
});
</script>

{% endblock %}


3 changes: 2 additions & 1 deletion BookingSystem/templates/innlevering.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ <h3>Sjekk at alt er med - bruk søkefeltet under til å dobbeltsjekke alle aktiv
{% endif %}
<td>{{ item.id }}</td>
<td>{{ item.name }} ({{ item.category }})</td>
<td>{{ item.lender }}</td>
<td>{{ item.lender_name }} <small> ({{ item.classroom }}{% if item.teacher %},
{{ item.teacher }}{% endif %})</small></td>
<td>{{ item.order_due_date|strftime }}</td>
</tr>
{% endfor %}
Expand Down
7 changes: 6 additions & 1 deletion BookingSystem/templates/partials/head.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<title>Utstyrbase</title>
<title>
Utstyrbase
{% if request.path != '/' %}
- {{ request.path|split('/')|last|title }}
{% endif %}
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">

Expand Down
15 changes: 13 additions & 2 deletions BookingSystem/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class User:
name: str
email: str
userid: str
affiliations: list[str]
affiliations: list[str] = list

def update(self) -> None:
"""Update the user's information from login provider. (OAuth)"""
Expand All @@ -36,7 +36,7 @@ def exists(self) -> bool:
@property
def classroom(self) -> str:
"""Get the classroom the user is associated with."""
return get(self.userid).get('classroom')
return get(self.userid).get('classroom', '')

@property
def active(self) -> bool:
Expand All @@ -63,6 +63,15 @@ def is_admin(self) -> bool:
return request.headers.get('Host') == KIOSK_FQDN


def separate_classroom_teacher(classroom: str) -> tuple[str, str]:
"""Separate the classroom and teacher from a string."""
if not classroom:
return '', ''
if '(' in classroom:
return classroom.split('(')[0].strip(), classroom.split('(')[1].strip(')')
return classroom, ''


def get_all_active_users() -> list[dict]:
"""Return a list of all active users in the database."""
con = sqlite3.connect(DATABASE)
Expand All @@ -71,6 +80,8 @@ def get_all_active_users() -> list[dict]:
columns = [description[0] for description in cur.description]
data = [{columns[i]: user[i] for i in range(len(columns))} for user in cur.fetchall()]
con.close()
for user in data:
user['classroom'], user['teacher'] = separate_classroom_teacher(user['classroom'])
return data


Expand Down

0 comments on commit 7993e2d

Please sign in to comment.