119 lines
3.5 KiB
Python
Executable File
119 lines
3.5 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# -*- coding: utf-8
|
|
|
|
# Required modules
|
|
import inspect
|
|
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
|
|
from functools import wraps
|
|
|
|
# Optionnal modules
|
|
import psycopg2
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
########################################################################
|
|
# App settings
|
|
########################################################################
|
|
app = Flask(__name__)
|
|
# Path to static files
|
|
app.static_url_path='/static'
|
|
# Set debug mode to False for production
|
|
app.debug = True
|
|
# Various configuration settings belong here (optionnal)
|
|
app.config.from_pyfile('config.local.py')
|
|
# Generate a new key: head -n 40 /dev/urandom | md5sum | cut -d' ' -f1
|
|
app.secret_key = 'ce1d1c9ff0ff388a838b3a1e3207dd27'
|
|
# Feel free to use SQLAlchemy (or not)
|
|
db = SQLAlchemy(app)
|
|
|
|
########################################################################
|
|
# Menu management:
|
|
# ----------------
|
|
#
|
|
# The main menu is a list of lists in the followin format
|
|
# [string caption, string URL endpoint, int 0]
|
|
# - The URL end point MUST match the function called by the corresponding
|
|
# route.
|
|
# - The int 0 is used to determine which menu entry is actally called.
|
|
# The value MUST be 0.
|
|
########################################################################
|
|
|
|
def get_menu(page):
|
|
menu = [['Accueil', '/', 0],
|
|
['Articles', '/articles', 0],
|
|
['Basics', '/basics', 0],
|
|
['Inputs', '/inputs', 0],
|
|
['Database', '/database', 0],
|
|
['Todo', '/todo', 0],
|
|
]
|
|
# This is index page
|
|
if page == 'index':
|
|
menu[0][2] = 1
|
|
return menu
|
|
|
|
# Let's look for the actual page
|
|
page = '/%s' % page
|
|
for item in menu:
|
|
if item[1] == page:
|
|
item[2] = 1
|
|
return menu
|
|
# This should never happen
|
|
return menu
|
|
|
|
########################################################################
|
|
# Routes:
|
|
# -------
|
|
#
|
|
# Except for the index fonction, the function name MUST have the same
|
|
# name than the URL endpoint to make the menu work properly
|
|
########################################################################
|
|
@app.errorhandler(404)
|
|
def page_not_found(e):
|
|
""" 404 not found """
|
|
return render_template('error.html', menu=app.menu), 404
|
|
|
|
|
|
@app.route("/", methods=['GET', 'POST'])
|
|
def index():
|
|
page = inspect.currentframe().f_code.co_name
|
|
menu = get_menu(page)
|
|
return render_template('index.html', menu=menu)
|
|
|
|
@app.route("/articles", methods=['GET', 'POST'])
|
|
def articles():
|
|
page = inspect.currentframe().f_code.co_name
|
|
menu = get_menu(page)
|
|
return render_template('articles.html', menu=menu)
|
|
|
|
@app.route("/basics", methods=['GET', 'POST'])
|
|
def basics():
|
|
page = inspect.currentframe().f_code.co_name
|
|
menu = get_menu(page)
|
|
return render_template('basics.html', menu=menu)
|
|
|
|
|
|
@app.route("/inputs", methods=['GET', 'POST'])
|
|
def inputs():
|
|
page = inspect.currentframe().f_code.co_name
|
|
menu = get_menu(page)
|
|
return render_template('inputs.html', menu=menu)
|
|
|
|
@app.route("/database", methods=['GET', 'POST'])
|
|
def database():
|
|
page = inspect.currentframe().f_code.co_name
|
|
menu = get_menu(page)
|
|
return render_template('database.html', menu=menu)
|
|
|
|
@app.route("/todo", methods=['GET', 'POST'])
|
|
def todo():
|
|
page = inspect.currentframe().f_code.co_name
|
|
menu = get_menu(page)
|
|
return render_template('todo.html', menu=menu)
|
|
|
|
|
|
|
|
########################################################################
|
|
# Main
|
|
########################################################################
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0')
|