-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding the basic structures and functionalities for the user frontend
- Loading branch information
Showing
25 changed files
with
674 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
""" | ||
Route defintion of the User Frontend of the CodeGrader | ||
@author: mkaiser | ||
@version: 1.0 | ||
""" | ||
|
||
import flask_login | ||
from flask import Flask, request, render_template, url_for, redirect, flash, session | ||
from flask_login import LoginManager, login_user, login_required, logout_user | ||
from codeGrader.frontend.config import config | ||
from codeGrader.frontend.user import templates | ||
from codeGrader.frontend.user.handlers import UserSessionHandler, SessionUser, UserLoginHandler, HomeHandler | ||
|
||
app = Flask(config.adminAppName, template_folder=templates.__path__[0]) | ||
|
||
# configuration of the login_manager and attaching it to the app | ||
app.secret_key = config.userSecretKey | ||
login_manager = LoginManager() | ||
login_manager.init_app(app) | ||
|
||
|
||
@login_manager.user_loader | ||
def user_login(user_id: int): | ||
""" | ||
User Load of the Login Manager | ||
Returns the Frontend Representation of a User | ||
@param user_id: The id of the user | ||
@type user_id: int | ||
@return: The frontend user object | ||
@rtype: SessionUser | ||
""" | ||
user = SessionUser(user_id) | ||
return user | ||
|
||
|
||
@login_manager.unauthorized_handler | ||
def unauthorized(): | ||
""" | ||
Returns to login page if you are not properly logged in | ||
@return: Redirection to the login site. | ||
""" | ||
return redirect(url_for("login")) | ||
|
||
|
||
@app.route("/login", methods=['GET', 'POST']) | ||
def login(): | ||
""" | ||
Renders the login page on GET | ||
Tries to log the user in on POST | ||
@return: Rendered template or a redirection | ||
""" | ||
if request.method == 'GET': | ||
return render_template("login.html") | ||
|
||
elif request.method == 'POST': | ||
# try to log the user in | ||
user_id = UserLoginHandler(request).post() | ||
if user_id: | ||
user = user_login(user_id) | ||
login_user(user) | ||
else: | ||
flash("The provided Credentials are not valid") | ||
return redirect(url_for("home")) | ||
|
||
|
||
@app.route("/logout", methods=['GET', 'POST']) | ||
@login_required | ||
def logout(): | ||
""" | ||
Logout a user so he needs to reauthenticate | ||
@return: Redirect to the login page | ||
""" | ||
logout_user() | ||
return redirect(url_for("login")) | ||
|
||
|
||
@app.route("/") | ||
@login_required | ||
def home(): | ||
""" | ||
The Home site of the user frontend | ||
@return: Rendered Home Template | ||
""" | ||
return HomeHandler(request).get() | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(port=config.userPort) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Base Handler for the User part of the CodeGrader Frontend | ||
@author: mkaiser | ||
""" | ||
import flask | ||
|
||
from codeGrader.frontend.config import config | ||
from codeGrader.frontend.util import ApiHandler | ||
|
||
|
||
class BaseHandler: | ||
""" | ||
Base Handler Class | ||
Other Handlers will inherit from this class | ||
""" | ||
|
||
def __init__(self, requests: flask.Request): | ||
""" | ||
Constructor of the BaseHandler | ||
""" | ||
self.request = requests | ||
self.api = ApiHandler(config.apiHost, config.apiAuthentication, config.apiToken) | ||
|
||
def get_value(self, value: str, default: str = ""): | ||
""" | ||
Get a value from the form that has been provided by the user identified by a given key. | ||
The key is defined by the name in the html form | ||
@param value: Name of the object in the request | ||
@type value: str | ||
@param default: The default value that shall be returned when no value is found. Optional parameter | ||
@type default: str | ||
@return: The value of the requested key | ||
@rtype: str | ||
""" | ||
try: | ||
value = self.request.form[value] | ||
if value == '': | ||
return None | ||
else: | ||
return value | ||
except Exception as err: | ||
print(err) | ||
return default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
Handler Classes for the Home site of the user panel | ||
""" | ||
import flask | ||
import flask_login | ||
from flask import request, render_template, redirect | ||
from .Base import BaseHandler | ||
|
||
|
||
class HomeHandler(BaseHandler): | ||
""" | ||
Handles Operations for the Home site | ||
""" | ||
|
||
def __init__(self, request: flask.Request): | ||
""" | ||
Constructor of the Home Handler | ||
@param request: The request from the app route of flask | ||
@type request: flask.request | ||
""" | ||
super().__init__(request) | ||
|
||
def get(self): | ||
return render_template("home.html") |
Oops, something went wrong.