Commit 7b39a2ce authored by colmoneill's avatar colmoneill

the bulk of today's work: Flask-Security & Flask-Login;

* sets the settings for the encryption algorythm used to
hash the passwords
* sets dummy config of the mailer component on register
(works!)
* implements a user management specific menu to the main menu
based off the forked templates/security/_menu.html
* studies the user object loading methods in the /dashboard
route
* fleshes out the dashboard page with current user info
* demoes flask flashing methods because why not
parent 4ba14f32
from flask import Flask, render_template, redirect
from flask import Flask, render_template, redirect, flash
from flask_flatpages import FlatPages
import markdown
import os
import glob
from flask_mongoengine import MongoEngine
from flask_security import Security, MongoEngineUserDatastore, UserMixin, RoleMixin, login_required, current_user
from flask_mail import Mail
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'
FLATPAGES_MARKDOWN_EXTENSIONS = ['markdown.extensions.extra', 'headerid']
app = Flask(__name__)
#app.config.from_pyfile('mysettings.cfg')
app.config.from_object(__name__)
pages = FlatPages(app)
pages.get('foo')
@app.route("/")
@app.route("/index")
def index():
#return "Hello, World!"
return render_template("home.html")
@app.route("/learn")
def learn():
#return "Hello, World!"
articles = (p for p in pages if 'published' in p.meta)
print(articles)
# Show the 10 most recent articles, most recent first.
......@@ -33,7 +36,6 @@ def learn():
def page(path):
page = pages.get_or_404(path)
print(page)
# template = page.meta.get('template', 'flatpage.html')
return render_template('flatpage.html', page=page)
......@@ -60,21 +62,34 @@ def event():
# /
# ______ _ _ _____ _ _
# | ___| | | | / ___| (_) |
# | |_ | | __ _ ___| | __ \ `--. ___ ___ _ _ _ __ _| |_ _ _
# | _| | |/ _` / __| |/ / `--. \/ _ \/ __| | | | '__| | __| | | |
# | | | | (_| \__ \ < /\__/ / __/ (__| |_| | | | | |_| |_| |
# \_| |_|\__,_|___/_|\_\ \____/ \___|\___|\__,_|_| |_|\__|\__, |
# ______ __/ |
# |______| |___/
# ______ _ _ _____ _ _
# | ___| | | | / ___| (_) |
# | |_ | | __ _ ___| | __ \ `--. ___ ___ _ _ _ __ _| |_ _ _
# | _| | |/ _` / __| |/ / `--. \/ _ \/ __| | | | '__| | __| | | |
# | | | | (_| \__ \ < /\__/ / __/ (__| |_| | | | | |_| |_| |
# \_| |_|\__,_|___/_|\_\ \____/ \___|\___|\__,_|_| |_|\__|\__, |
# ______ __/ |
# |______| |___/
# /
from flask_mongoengine import MongoEngine
from flask_security import Security, MongoEngineUserDatastore, UserMixin, RoleMixin, login_required
# Create app
app.config['SECRET_KEY'] = 'super-secret'
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_RECOVERABLE'] = True
#app.config['SECURITY_TRACKABLE'] = True
app.config['SECURITY_PASSWORD_HASH'] = 'bcrypt'
app.config['SECURITY_PASSWORD_SALT'] = '$2a$16$PnnIgfMwkOjGX4SkHqSOPO'
app.config['MAIL_SERVER'] = 'mail.gandi.net'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'automatic@colm.be'
app.config['MAIL_PASSWORD'] = 'testing#123'
app.config['MAIL_DEFAULT_SENDER'] = 'automatic@colm.be'
mail = Mail(app)
# MongoDB Config
app.config['MONGODB_DB'] = 'mydatabase'
......@@ -101,18 +116,23 @@ user_datastore = MongoEngineUserDatastore(db, User, Role)
security = Security(app, user_datastore)
# Create a user to test with
@app.before_first_request
def create_user():
user_datastore.create_user(email='erreur401@gmail.com', password='password')
# Note the decorator, this runs only once on startup of the server.
# @app.before_first_request
# def create_user():
# user_datastore.create_user(email='test@gmail.com', password='password')
# Views
@app.route('/dashboard')
@login_required
def dashboard():
return render_template('dashboard.html')
user = current_user.get_id()
print(current_user.get_id())
message = 'Welcome to your dashboard ' + current_user.get_id()
flash(message)
return render_template('dashboard.html', user=user)
# APP RUN
# APP RUN
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
{% extends "base.html" %}
{% block content %}
LOGGED BEBE
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<h1>dashboard for user {{current_user.email}}</h1>
LOGGED BEBE<br>
{{ current_user.email }}<br>
{{ current_user.badges }}
{% endblock %}
......@@ -19,7 +19,10 @@
<ul id="menuSecond">
<li class="item"><a href="/login">inscription</a></li>
<li class="item"><a href="/register">Login</a></li>
{% if current_user.is_authenticated %}
<li><a href="/dashboard">logged in as: {{ current_user.email }}</a></li>
{% endif %}
{% include "security/_menu.html" %}
<li class="item"><a href="#">FR/NL</a></li>
</ul>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment