From 667bed6edf2652fa1fbb93632eef0623f5f6d5c9 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 9 Jan 2020 05:20:50 +0100 Subject: [PATCH] Add Bootstrap simple template and error handling --- Design.org | 18 ++++++---- code/Pipfile | 1 + code/Pipfile.lock | 22 +++++++++++- code/app/__init__.py | 4 ++- code/app/errors.py | 13 +++++++ code/app/routes.py | 6 ++++ code/app/templates/404.html | 6 ++++ code/app/templates/500.html | 8 +++++ code/app/templates/admin.html | 3 +- code/app/templates/base.html | 67 ++++++++++++++++++++++------------- code/app/templates/index.html | 4 +-- code/app/templates/login.html | 30 +++++----------- code/app/templates/nuked.html | 13 +++++++ 13 files changed, 138 insertions(+), 57 deletions(-) create mode 100644 code/app/errors.py create mode 100644 code/app/templates/404.html create mode 100644 code/app/templates/500.html create mode 100644 code/app/templates/nuked.html diff --git a/Design.org b/Design.org index d0ff030..8e7d2ab 100644 --- a/Design.org +++ b/Design.org @@ -45,7 +45,7 @@ CLOSED: [2019-11-01 Fri 00:34] - [X] Black box - [X] Entity-Relationship ** Implementation -*** TODO Backend [2/3] [66%] +*** TODO Backend [2/4] [50%] **** DONE Database [3/3] [100%] CLOSED: [2020-01-03 Fri 00:44] - [X] Connection @@ -56,11 +56,15 @@ CLOSED: [2020-01-08 Wed 03:18] - [X] Select useful fiels - [X] Convert PU to Country (ISO 3166) - [X] Insert into database -**** TODO Flask Application [0/2] [0%] -- [ ] Login for admin +**** TODO Flask Application [1/4] [25%] - [ ] Arithmetic operations for yearly changes +- [ ] Tables with pandas - [ ] Plots with pandas -*** NEXT Documentation -*** INACTIVE Frontend [0/1] [0%] -- [ ] [[https://adminlte.io/][Adminlte]] -- [ ] Bootstrap +- [X] Login for admin +**** TODO Possible additions [0/2] [0%] +- [ ] Text search for glaciers +- [ ] Form seach for a year +*** TODO Frontend [1/2] [50%] +- [X] Flask-Bootstrap +- [ ] Find CSS +*** TODO Documentation diff --git a/code/Pipfile b/code/Pipfile index b6fe635..f8644d3 100644 --- a/code/Pipfile +++ b/code/Pipfile @@ -13,6 +13,7 @@ pandas = "*" iso3166 = "*" flask-wtf = "*" flask-login = "*" +flask-bootstrap = "*" [requires] python_version = "3.8" diff --git a/code/Pipfile.lock b/code/Pipfile.lock index 646671d..484caa2 100644 --- a/code/Pipfile.lock +++ b/code/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "234ca1fc7cbb10534d17febc8a9d0320ecb5c6317351bcd97e161c860201b5a5" + "sha256": "b25fa5cfde97f34d3f4210b7c3e602df640d50105eb290daefa14c194b289936" }, "pipfile-spec": 6, "requires": { @@ -23,6 +23,13 @@ ], "version": "==7.0" }, + "dominate": { + "hashes": [ + "sha256:6e833aea505f0236a9fc692326bac575f8bd38ae0f3a1bdc73d20ca606ac75d5", + "sha256:a92474b4312bd8b4c1789792f3ec8c571cd8afa8e7502a2b1c64dd48cd67e59c" + ], + "version": "==2.4.0" + }, "flask": { "hashes": [ "sha256:13f9f196f330c7c2c5d7a5cf91af894110ca0215ac051b5844701f2bfd934d52", @@ -31,6 +38,13 @@ "index": "pypi", "version": "==1.1.1" }, + "flask-bootstrap": { + "hashes": [ + "sha256:cb08ed940183f6343a64e465e83b3a3f13c53e1baabb8d72b5da4545ef123ac8" + ], + "index": "pypi", + "version": "==3.3.7.1" + }, "flask-login": { "hashes": [ "sha256:c815c1ac7b3e35e2081685e389a665f2c74d7e077cb93cecabaea352da4752ec" @@ -195,6 +209,12 @@ ], "version": "==1.3.12" }, + "visitor": { + "hashes": [ + "sha256:2c737903b2b6864ebc6167eef7cf3b997126f1aa94bdf590f90f1436d23e480a" + ], + "version": "==0.1.3" + }, "werkzeug": { "hashes": [ "sha256:7280924747b5733b246fe23972186c6b348f9ae29724135a6dfc1e53cea433e7", diff --git a/code/app/__init__.py b/code/app/__init__.py index 94eb5a9..9bffec2 100644 --- a/code/app/__init__.py +++ b/code/app/__init__.py @@ -2,11 +2,13 @@ from flask import Flask from config import Config from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager +from flask_bootstrap import Bootstrap app = Flask(__name__) app.config.from_object(Config) db = SQLAlchemy(app) login = LoginManager(app) login.login_view = "login" +bootstrap = Bootstrap(app) -from app import routes, models +from app import routes, models, errors diff --git a/code/app/errors.py b/code/app/errors.py new file mode 100644 index 0000000..447c6cd --- /dev/null +++ b/code/app/errors.py @@ -0,0 +1,13 @@ +from flask import render_template +from app import app, db + + +@app.errorhandler(404) +def not_found_error(error): + return render_template("404.html"), 404 + + +@app.errorhandler(500) +def internal_error(error): + db.session.rollback() + return render_template("500.html"), 500 diff --git a/code/app/routes.py b/code/app/routes.py index 1eca20e..cc1a9c4 100644 --- a/code/app/routes.py +++ b/code/app/routes.py @@ -40,3 +40,9 @@ def logout(): @login_required def admin(): return render_template("admin.html", title="Admin Page") + + +@app.route("/nuke") +@login_required +def nuke(): + return render_template("nuked.html", title="NUKE THE SYSTEM!") diff --git a/code/app/templates/404.html b/code/app/templates/404.html new file mode 100644 index 0000000..0bac211 --- /dev/null +++ b/code/app/templates/404.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} + +{% block app_content %} +

Sorry, we couldn't find that

+

Back

+{% endblock %} diff --git a/code/app/templates/500.html b/code/app/templates/500.html new file mode 100644 index 0000000..984d38c --- /dev/null +++ b/code/app/templates/500.html @@ -0,0 +1,8 @@ +{% extends "base.html" %} + +{% block app_content %} +

An unexpected error has occurred

+

The administrator has been notified!

+

If he gets too many notifications, we might replace him with an AI

+

Back

+{% endblock %} diff --git a/code/app/templates/admin.html b/code/app/templates/admin.html index 2410e53..97dd31b 100644 --- a/code/app/templates/admin.html +++ b/code/app/templates/admin.html @@ -1,6 +1,7 @@ {% extends "base.html" %} {% block content %} -

Hi, {{ current_user.username }}!

+

Hey, {{ current_user.username }}!

Do you want to nuke the database? +
  • Yes, I want to burn down the world
  • {% endblock %} diff --git a/code/app/templates/base.html b/code/app/templates/base.html index b7c588c..4b07074 100644 --- a/code/app/templates/base.html +++ b/code/app/templates/base.html @@ -1,30 +1,49 @@ - - - {% if title %} - {{ title }} - IGDB - {% else %} - Welcome to IGDB - {% endif %} - - -
    IGDB: - Home - {% if current_user.is_anonymous %} - Login - {% else %} - Administration - Logout - {% endif %} -
    -
    +{% extends 'bootstrap/base.html' %} + +{% block title %} + IGDB +{% endblock %} + +{% block navbar %} + +{% endblock %} + +{% block content %} +
    {% with messages = get_flashed_messages() %} {% if messages %} - {% endif %} {% endwith %} - {% block content %}{% endblock %} - + + {# application content needs to be provided in the app_content block #} + {% block app_content %}{% endblock %} +
    +{% endblock %} diff --git a/code/app/templates/index.html b/code/app/templates/index.html index f7d761d..557988a 100644 --- a/code/app/templates/index.html +++ b/code/app/templates/index.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% block content %} -

    IGDB: International Glacier Database

    - The IGDB is a database, that uses data from the WGMS to illustrate the consequences of climate change. +

    IGDB: Internation Glacier Database

    +

    The IGDB is a database, that uses data from the WGMS to illustrate the consequences of climate change.

    {% endblock %} diff --git a/code/app/templates/login.html b/code/app/templates/login.html index 806e09a..92455e3 100644 --- a/code/app/templates/login.html +++ b/code/app/templates/login.html @@ -1,24 +1,12 @@ -{% extends "base.html" %} +{% extends 'base.html' %} +{% import 'bootstrap/wtf.html' as wtf %} -{% block content %} +{% block app_content %}

    Sign In

    -
    - {{ form.hidden_tag() }} -

    - {{ form.username.label }}
    - {{ form.username(size=32) }}
    - {% for error in form.username.errors %} - [{{ error }}] - {% endfor %} -

    -

    - {{ form.password.label }}
    - {{ form.password(size=32) }}
    - {% for error in form.password.errors %} - [{{ error }}] - {% endfor %} -

    -

    {{ form.remember_me() }} {{ form.remember_me.label }}

    -

    {{ form.submit() }}

    -
    +
    +
    + {{ wtf.quick_form(form) }} +
    +
    +
    {% endblock %} diff --git a/code/app/templates/nuked.html b/code/app/templates/nuked.html new file mode 100644 index 0000000..d3d7994 --- /dev/null +++ b/code/app/templates/nuked.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block content %} +

    You're the boss!

    +
      +
    1. Stop the web server, and then execute:
    2. +
      mysql -u root -p
      +
    3. Introduce MySQL's root password

    4. +
    5. Execute these statements:

    6. +
      DROP DATABASE IGDB;
      +DROP USER IGDB@LOCALHOST;
      +{% endblock %} +