Skip to content
Snippets Groups Projects
home.py 1.93 KiB
Newer Older
Alex's avatar
Alex committed
from flask import Blueprint, render_template, request, redirect
from imperial_panopto import panopto_login_required

from config import url_for2
from config.config import ENV, APP_NAME
from database.db import db
from models.entity import Entity
from imperial_ldap.auth import login_required, get_user_from_session
from imperial_doc_materials import using_materials


home_blueprint = Blueprint('home', __name__, url_prefix='/')

@home_blueprint.route('')
def index():
    user = get_user_from_session()  # If there is no user logged, this returns None
    return render_template('index.html', user=user, dev=ENV, appname=APP_NAME)


@home_blueprint.route('/dashboard')
@login_required     # Indicate that the user must be logged in (via LDAP)
@using_materials    # Indicate that we want to access the Materials API
def dashboard(materials_client, user):
    my_courses = materials_client.get_courses_for_year("2122")
    return render_template('dashboard.html', user=user, my_courses=my_courses, dev=ENV, appname=APP_NAME)


@home_blueprint.route('/panopto_example')
@panopto_login_required
def panopto_example(panopto_client):
    video = panopto_client.get_partially_viewed_videos()[0]
    return render_template('panopto_example.html', video=video, iframe=panopto_client.get_iframe_of_video(video.id), dev=ENV, appname=APP_NAME)


@home_blueprint.route('/entities', methods=['GET', 'POST'])
@login_required
def entities(user):
    if request.method == "GET":
        try:
            rows = ""
            for e in db.session.query(Entity).all():
                rows += str(e) + "<br>"

            return rows
        except:
            return "App database error"
    else:
        name = request.form.get('name')
        age = request.form.get('age')

        try:
            db.session.add(Entity(name=name, age=int(age)))
            db.session.commit()
            return redirect(url_for2('.entities'))
        except:
            return "Could not add entity."