Commit 8c4b988d authored by Petrus J.v.Rensburg's avatar Petrus J.v.Rensburg

Merge branch 'examples'

parents 6b6fe519 015ea597
...@@ -4,7 +4,7 @@ from flask_sqlalchemy import SQLAlchemy ...@@ -4,7 +4,7 @@ from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore, \ from flask_security import Security, SQLAlchemyUserDatastore, \
UserMixin, RoleMixin, login_required, current_user UserMixin, RoleMixin, login_required, current_user
from flask_security.utils import encrypt_password from flask_security.utils import encrypt_password
import flask_admin as admin import flask_admin
from flask_admin.contrib import sqla from flask_admin.contrib import sqla
from flask_admin import helpers as admin_helpers from flask_admin import helpers as admin_helpers
...@@ -28,6 +28,9 @@ class Role(db.Model, RoleMixin): ...@@ -28,6 +28,9 @@ class Role(db.Model, RoleMixin):
name = db.Column(db.String(80), unique=True) name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255)) description = db.Column(db.String(255))
def __str__(self):
return self.name
class User(db.Model, UserMixin): class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
...@@ -40,6 +43,9 @@ class User(db.Model, UserMixin): ...@@ -40,6 +43,9 @@ class User(db.Model, UserMixin):
roles = db.relationship('Role', secondary=roles_users, roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic')) backref=db.backref('users', lazy='dynamic'))
def __str__(self):
return self.email
# Setup Flask-Security # Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role) user_datastore = SQLAlchemyUserDatastore(db, User, Role)
...@@ -76,7 +82,12 @@ def index(): ...@@ -76,7 +82,12 @@ def index():
return render_template('index.html') return render_template('index.html')
# Create admin # Create admin
admin = admin.Admin(app, 'Example: Auth', base_template='my_master.html') admin = flask_admin.Admin(
app,
'Example: Auth',
base_template='my_master.html',
template_mode='bootstrap3',
)
# Add model views # Add model views
admin.add_view(MyModelView(Role, db.session)) admin.add_view(MyModelView(Role, db.session))
......
{% extends 'admin/master.html' %} {% extends 'admin/master.html' %}
{% block body %} {% block body %}
{{ super() }} {{ super() }}
<div class="row-fluid"> <div class="container">
<div class="row">
<div> <div class="col-sm-10 col-sm-offset-1">
<h1>Flask-Admin example</h1> <h1>Flask-Admin example</h1>
<p class="lead"> <p class="lead">
Authentication Authentication
</p> </p>
<p> <p>
This example shows how you can use Flask-Security for authentication. This example shows how you can use <a href="https://pythonhosted.org/Flask-Security/index.html" target="_blank">Flask-Security</a> for authentication.
</p> </p>
{% if not current_user.is_authenticated() %} {% if not current_user.is_authenticated() %}
<p>You can register as a regular user, or log in as a superuser with the following credentials: <p>You can register as a regular user, or log in as a superuser with the following credentials:
...@@ -17,13 +17,14 @@ ...@@ -17,13 +17,14 @@
<li>email: <b>admin</b></li> <li>email: <b>admin</b></li>
<li>password: <b>admin</b></li> <li>password: <b>admin</b></li>
</ul> </ul>
</p>
<p> <p>
<a class="btn btn-default" href="{{ url_for('security.login') }}">login</a> <a class="btn btn-default" href="{{ url_for('security.register') }}">register</a> <a class="btn btn-primary" href="{{ url_for('security.login') }}">login</a> <a class="btn btn-default" href="{{ url_for('security.register') }}">register</a>
</p> </p>
{% endif %} {% endif %}
<p>
<a class="btn btn-primary" href="/"><i class="glyphicon glyphicon-chevron-left"></i> Back</a>
</p>
</div>
</div> </div>
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a>
</div> </div>
{% endblock body %} {% endblock body %}
\ No newline at end of file
...@@ -2,16 +2,15 @@ ...@@ -2,16 +2,15 @@
{% block access_control %} {% block access_control %}
{% if current_user.is_authenticated() %} {% if current_user.is_authenticated() %}
<div class="btn-group pull-right"> <div class="navbar-text btn-group pull-right">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<i class="icon-user"></i> <i class="glyphicon glyphicon-user"></i>
{% if current_user.first_name -%} {% if current_user.first_name -%}
{{ current_user.first_name }} {{ current_user.first_name }}
{% else -%} {% else -%}
{{ current_user.email }} {{ current_user.email }}
{%- endif %} <span class="caret"></span> {%- endif %}<span class="caret"></span></a>
</a> <ul class="dropdown-menu" role="menu">
<ul class="dropdown-menu">
<li><a href="{{ url_for('security.logout') }}">Log out</a></li> <li><a href="{{ url_for('security.logout') }}">Log out</a></li>
</ul> </ul>
</div> </div>
......
{% macro render_field_with_errors(field) %} {% macro render_field_with_errors(field) %}
<p>
{{ field.label }} {{ field(**kwargs)|safe }} <div class="form-group">
{{ field.label }} {{ field(class_='form-control', **kwargs)|safe }}
{% if field.errors %} {% if field.errors %}
<ul> <ul>
{% for error in field.errors %} {% for error in field.errors %}
<li>{{ error }}</li> <li>{{ error }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
</p> </div>
{% endmacro %} {% endmacro %}
{% macro render_field(field) %} {% macro render_field(field) %}
<p>{{ field(**kwargs)|safe }}</p> <p>{{ field(class_='form-control', **kwargs)|safe }}</p>
{% endmacro %} {% endmacro %}
{% macro render_checkbox_field(field) -%}
<div class="form-group">
<div class="checkbox">
<label>
{{ field(type='checkbox', **kwargs) }} {{ field.label }}
</label>
</div>
</div>
{%- endmacro %}
\ No newline at end of file
{% extends 'admin/master.html' %} {% extends 'admin/master.html' %}
{% from "security/_macros.html" import render_field_with_errors, render_field %} {% from "security/_macros.html" import render_field, render_field_with_errors, render_checkbox_field %}
{% include "security/_messages.html" %} {% include "security/_messages.html" %}
{% block body %} {% block body %}
{{ super() }} {{ super() }}
<div class="row-fluid"> <div class="row-fluid">
<div class="col-sm-8 col-sm-offset-2">
<h1>Login</h1> <h1>Login</h1>
<div class="well">
<form action="{{ url_for_security('login') }}" method="POST" name="login_user_form"> <form action="{{ url_for_security('login') }}" method="POST" name="login_user_form">
{{ login_user_form.hidden_tag() }} {{ login_user_form.hidden_tag() }}
{{ render_field_with_errors(login_user_form.email) }} {{ render_field_with_errors(login_user_form.email) }}
{{ render_field_with_errors(login_user_form.password) }} {{ render_field_with_errors(login_user_form.password) }}
{{ render_field_with_errors(login_user_form.remember) }} {{ render_checkbox_field(login_user_form.remember) }}
{{ render_field(login_user_form.next) }} {{ render_field(login_user_form.next) }}
{{ render_field(login_user_form.submit, class="btn btn-primary") }} {{ render_field(login_user_form.submit, class="btn btn-primary") }}
</form> </form>
{% include "security/_menu.html" %} <p>Not yet signed up? Please <a href="{{ url_for('security.register') }}">register for an account</a>.</p>
</div>
</div>
</div> </div>
{% endblock body %} {% endblock body %}
\ No newline at end of file
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
{% block body %} {% block body %}
{{ super() }} {{ super() }}
<div class="row-fluid"> <div class="row-fluid">
<div class="col-sm-8 col-sm-offset-2">
<h1>Register</h1> <h1>Register</h1>
<div class="well">
<form action="{{ url_for_security('register') }}" method="POST" name="register_user_form"> <form action="{{ url_for_security('register') }}" method="POST" name="register_user_form">
{{ register_user_form.hidden_tag() }} {{ register_user_form.hidden_tag() }}
{{ render_field_with_errors(register_user_form.email) }} {{ render_field_with_errors(register_user_form.email) }}
...@@ -14,6 +16,8 @@ ...@@ -14,6 +16,8 @@
{% endif %} {% endif %}
{{ render_field(register_user_form.submit, class="btn btn-primary") }} {{ render_field(register_user_form.submit, class="btn btn-primary") }}
</form> </form>
{% include "security/_menu.html" %} <p>Already signed up? Please <a href="{{ url_for('security.login') }}">log in</a>.</p>
</div>
</div>
</div> </div>
{% endblock body %} {% endblock body %}
\ No newline at end of file
...@@ -157,7 +157,7 @@ def index(): ...@@ -157,7 +157,7 @@ def index():
return '<a href="/admin/">Click me to get to Admin!</a>' return '<a href="/admin/">Click me to get to Admin!</a>'
# Create admin # Create admin
admin = Admin(app, 'Example: Forms') admin = Admin(app, 'Example: Forms', template_mode='bootstrap3')
# Add views # Add views
admin.add_view(FileView(File, db.session)) admin.add_view(FileView(File, db.session))
......
Flask Flask
Flask-Admin Flask-Admin
Flask-SQLAlchemy Flask-SQLAlchemy
pillow
\ No newline at end of file
{% extends 'admin/master.html' %} {% extends 'admin/master.html' %}
{% block body %} {% block body %}
{{ super() }} {{ super() }}
<div class="row-fluid"> <div class="container">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<h1>Flask-Admin example</h1> <h1>Flask-Admin example</h1>
<p class="lead"> <p class="lead">
Custom forms Custom forms
...@@ -12,6 +14,8 @@ ...@@ -12,6 +14,8 @@
<p> <p>
It also demonstrates general file handling as well as the handling of image files specifically. It also demonstrates general file handling as well as the handling of image files specifically.
</p> </p>
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> <a class="btn btn-primary" href="/"><i class="glyphicon glyphicon-chevron-left"></i> Back</a>
</div>
</div>
</div> </div>
{% endblock body %} {% endblock body %}
\ No newline at end of file
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
<p> <p>
This is done by overriding some of the built-in templates. This is done by overriding some of the built-in templates.
</p> </p>
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> <a class="btn btn-primary" href="/"><i class="glyphicon glyphicon-chevron-left"></i> Back</a>
</div> </div>
{% endblock body %} {% endblock body %}
...@@ -30,7 +30,7 @@ def index(): ...@@ -30,7 +30,7 @@ def index():
return '<a href="/admin/">Click me to get to Admin!</a>' return '<a href="/admin/">Click me to get to Admin!</a>'
# Create admin interface # Create admin interface
admin = admin.Admin(name="Example: Simple Views") admin = admin.Admin(name="Example: Simple Views", template_mode='bootstrap3')
admin.add_view(MyAdminView(name="view1", category='Test')) admin.add_view(MyAdminView(name="view1", category='Test'))
admin.add_view(AnotherAdminView(name="view2", category='Test')) admin.add_view(AnotherAdminView(name="view2", category='Test'))
admin.init_app(app) admin.init_app(app)
......
{% extends 'admin/master.html' %} {% extends 'admin/master.html' %}
{% block body %} {% block body %}
{{ super() }} {{ super() }}
<div class="row"> <div class="container">
<div class="span10 offset1"> <div class="row">
<div class="col-sm-10 col-sm-offset-1">
<h1>Flask-Admin example</h1> <h1>Flask-Admin example</h1>
<p class="lead"> <p class="lead">
Simple admin views, not related to models. Simple admin views, not related to models.
...@@ -14,7 +15,8 @@ ...@@ -14,7 +15,8 @@
<p> <p>
By adding custom views to the admin interface, they become accessible through the <em>navbar</em> at the top. By adding custom views to the admin interface, they become accessible through the <em>navbar</em> at the top.
</p> </p>
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> <a class="btn btn-primary" href="/"><i class="glyphicon glyphicon-chevron-left"></i> Back</a>
</div>
</div> </div>
</div> </div>
{% endblock body %} {% endblock body %}
...@@ -144,7 +144,7 @@ class TreeView(sqla.ModelView): ...@@ -144,7 +144,7 @@ class TreeView(sqla.ModelView):
# Create admin # Create admin
admin = admin.Admin(app, name='Example: SQLAlchemy') admin = admin.Admin(app, name='Example: SQLAlchemy', template_mode='bootstrap3')
# Add views # Add views
admin.add_view(UserAdmin(User, db.session)) admin.add_view(UserAdmin(User, db.session))
......
...@@ -50,7 +50,7 @@ class TyreAdmin(sqla.ModelView): ...@@ -50,7 +50,7 @@ class TyreAdmin(sqla.ModelView):
form_columns = ['car', 'tyre_id', 'desc'] form_columns = ['car', 'tyre_id', 'desc']
# Create admin # Create admin
admin = admin.Admin(app, name='Example: SQLAlchemy2') admin = admin.Admin(app, name='Example: SQLAlchemy2', template_mode='bootstrap3')
admin.add_view(CarAdmin(Car, db.session)) admin.add_view(CarAdmin(Car, db.session))
admin.add_view(TyreAdmin(Tyre, db.session)) admin.add_view(TyreAdmin(Tyre, db.session))
......
{% extends 'admin/master.html' %} {% extends 'admin/master.html' %}
{% block body %} {% block body %}
{{ super() }} {{ super() }}
<div class="row"> <div class="container">
<div class="span10 offset1"> <div class="row">
<div class="col-sm-10 col-sm-offset-1">
<h1>Flask-Admin example</h1> <h1>Flask-Admin example</h1>
<p class="lead"> <p class="lead">
Basic SQLAlchemy model views. Basic SQLAlchemy model views.
...@@ -13,7 +14,8 @@ ...@@ -13,7 +14,8 @@
<p> <p>
The views are generated automatically, but it is perfectly possible to customize them to suit your needs. The views are generated automatically, but it is perfectly possible to customize them to suit your needs.
</p> </p>
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> <a class="btn btn-primary" href="/"><i class="glyphicon glyphicon-chevron-left"></i> Back</a>
</div>
</div> </div>
</div> </div>
{% endblock body %} {% endblock body %}
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