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:
<ul> <ul>
<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-primary" href="{{ url_for('security.login') }}">login</a> <a class="btn btn-default" href="{{ url_for('security.register') }}">register</a>
<a class="btn btn-default" href="{{ url_for('security.login') }}">login</a> <a class="btn btn-default" href="{{ url_for('security.register') }}">register</a> </p>
</p> {% endif %}
{% endif %} <p>
</div> <a class="btn btn-primary" href="/"><i class="glyphicon glyphicon-chevron-left"></i> Back</a>
</p>
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> </div>
</div>
</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">
{% if field.errors %} {{ field.label }} {{ field(class_='form-control', **kwargs)|safe }}
<ul> {% if field.errors %}
{% for error in field.errors %} <ul>
<li>{{ error }}</li> {% for error in field.errors %}
{% endfor %} <li>{{ error }}</li>
</ul> {% endfor %}
{% endif %} </ul>
</p> {% endif %}
</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 %}
\ No newline at end of file
{% 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">
<h1>Login</h1> <div class="col-sm-8 col-sm-offset-2">
<form action="{{ url_for_security('login') }}" method="POST" name="login_user_form"> <h1>Login</h1>
{{ login_user_form.hidden_tag() }} <div class="well">
{{ render_field_with_errors(login_user_form.email) }} <form action="{{ url_for_security('login') }}" method="POST" name="login_user_form">
{{ render_field_with_errors(login_user_form.password) }} {{ login_user_form.hidden_tag() }}
{{ render_field_with_errors(login_user_form.remember) }} {{ render_field_with_errors(login_user_form.email) }}
{{ render_field(login_user_form.next) }} {{ render_field_with_errors(login_user_form.password) }}
{{ render_field(login_user_form.submit, class="btn btn-primary") }} {{ render_checkbox_field(login_user_form.remember) }}
</form> {{ render_field(login_user_form.next) }}
{% include "security/_menu.html" %} {{ render_field(login_user_form.submit, class="btn btn-primary") }}
</form>
<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,16 +4,20 @@ ...@@ -4,16 +4,20 @@
{% block body %} {% block body %}
{{ super() }} {{ super() }}
<div class="row-fluid"> <div class="row-fluid">
<h1>Register</h1> <div class="col-sm-8 col-sm-offset-2">
<form action="{{ url_for_security('register') }}" method="POST" name="register_user_form"> <h1>Register</h1>
{{ register_user_form.hidden_tag() }} <div class="well">
{{ render_field_with_errors(register_user_form.email) }} <form action="{{ url_for_security('register') }}" method="POST" name="register_user_form">
{{ render_field_with_errors(register_user_form.password) }} {{ register_user_form.hidden_tag() }}
{% if register_user_form.password_confirm %} {{ render_field_with_errors(register_user_form.email) }}
{{ render_field_with_errors(register_user_form.password_confirm) }} {{ render_field_with_errors(register_user_form.password) }}
{% endif %} {% if register_user_form.password_confirm %}
{{ render_field(register_user_form.submit, class="btn btn-primary") }} {{ render_field_with_errors(register_user_form.password_confirm) }}
</form> {% endif %}
{% include "security/_menu.html" %} {{ render_field(register_user_form.submit, class="btn btn-primary") }}
</form>
<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">
<h1>Flask-Admin example</h1> <div class="row">
<p class="lead"> <div class="col-sm-10 col-sm-offset-1">
Custom forms <h1>Flask-Admin example</h1>
</p> <p class="lead">
<p> Custom forms
This example shows how you can define your own custom forms by using form rendering rules. </p>
</p> <p>
<p> This example shows how you can define your own custom forms by using form rendering rules.
It also demonstrates general file handling as well as the handling of image files specifically. </p>
</p> <p>
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> It also demonstrates general file handling as well as the handling of image files specifically.
</p>
<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">
<h1>Flask-Admin example</h1> <div class="col-sm-10 col-sm-offset-1">
<p class="lead"> <h1>Flask-Admin example</h1>
Simple admin views, not related to models. <p class="lead">
</p> Simple admin views, not related to models.
<p> </p>
This example shows how to add your own views to the admin interface. The views do not have to be associated <p>
to any of your models, and you can fill them with whatever content you want. This example shows how to add your own views to the admin interface. The views do not have to be associated
</p> to any of your models, and you can fill them with whatever content you want.
<p> </p>
By adding custom views to the admin interface, they become accessible through the <em>navbar</em> at the top. <p>
</p> By adding custom views to the admin interface, they become accessible through the <em>navbar</em> at the top.
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> </p>
</div> <a class="btn btn-primary" href="/"><i class="glyphicon glyphicon-chevron-left"></i> Back</a>
</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">
<h1>Flask-Admin example</h1> <div class="col-sm-10 col-sm-offset-1">
<p class="lead"> <h1>Flask-Admin example</h1>
Basic SQLAlchemy model views. <p class="lead">
</p> Basic SQLAlchemy model views.
<p> </p>
This example shows how to add basic CRUD-views for your SQLAlchemy models. <p>
</p> This example shows how to add basic CRUD-views for your SQLAlchemy models.
<p> </p>
The views are generated automatically, but it is perfectly possible to customize them to suit your needs. <p>
</p> The views are generated automatically, but it is perfectly possible to customize them to suit your needs.
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a> </p>
</div> <a class="btn btn-primary" href="/"><i class="glyphicon glyphicon-chevron-left"></i> Back</a>
</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