2022-08-09 12:46:48 +00:00
from flask import Blueprint , flash , redirect , request , url_for
2022-08-15 20:22:36 +00:00
from flask_login import current_user
from flask_security . decorators import auth_required
2021-04-03 00:11:26 +00:00
2022-06-01 20:53:23 +00:00
from . model_dependent_funs import active_semester_str
2022-08-09 12:46:48 +00:00
from . models import Semester , db
2021-03-18 13:53:55 +00:00
2022-08-09 12:46:48 +00:00
bp = Blueprint ( " main " , __name__ , root_path = " / " , template_folder = " templates " )
2021-03-31 23:04:21 +00:00
2022-08-09 12:46:48 +00:00
@bp.app_context_processor
2021-03-20 14:41:22 +00:00
def util_processor ( ) :
2022-06-17 20:24:23 +00:00
author_email = " mobitar@students.uni-mainz.de "
2022-06-27 20:09:17 +00:00
footer = f " <hr><p style= ' font-size:14px; ' >This website is still under development (beta release)! If you have any questions, find any bugs or want some feature, please write a formless email (german/english) to Mo Bitar: <a href= ' mailto: { author_email } ' > { author_email } </a>. Feedback is also welcome :)</p><br> "
2022-08-09 12:46:48 +00:00
2022-06-01 20:53:23 +00:00
return dict ( active_semester_str = active_semester_str , current_user = current_user , footer = footer )
2021-03-20 14:41:22 +00:00
2021-03-31 23:04:21 +00:00
2022-08-09 12:46:48 +00:00
@bp.route ( " / " )
2021-03-18 13:53:55 +00:00
def index ( ) :
2021-05-17 20:15:50 +00:00
if current_user . has_role ( " admin " ) :
2022-06-28 14:57:55 +00:00
endpoint_base = " admin "
2021-05-17 20:15:50 +00:00
elif current_user . has_role ( " assistant " ) :
2022-06-28 14:57:55 +00:00
endpoint_base = " assistant "
2021-05-17 20:15:50 +00:00
else :
2021-09-11 19:20:36 +00:00
return redirect ( url_for ( " security.login " ) )
2021-03-18 22:20:59 +00:00
2022-06-28 14:57:55 +00:00
if current_user . login_count == 1 :
2022-06-28 15:01:32 +00:00
url = url_for ( endpoint_base + " _docs.index " )
2022-06-28 14:57:55 +00:00
else :
url = url_for ( endpoint_base + " .index " )
return redirect ( url )
2021-05-17 20:36:24 +00:00
2022-08-09 12:46:48 +00:00
@bp.route ( " /set_semester " )
2021-04-17 00:19:38 +00:00
@auth_required ( )
def set_semester ( ) :
2021-07-11 11:48:13 +00:00
try :
semesterId = int ( request . args . get ( " semester_id " ) )
except Exception as ex :
flash ( str ( ex ) , " error " )
else :
2022-05-16 22:42:49 +00:00
semester = db . session . get ( Semester , semesterId )
2022-05-29 19:41:01 +00:00
current_user . setActiveSemester ( semester )
2021-07-11 11:48:13 +00:00
2022-08-09 12:46:48 +00:00
red = request . referrer or url_for ( " main.index " )
2021-05-18 12:54:30 +00:00
return redirect ( red )