Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
flask-admin
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Python-Dev
flask-admin
Commits
4a48fa42
Commit
4a48fa42
authored
Jun 14, 2015
by
Petrus J.v.Rensburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updte auth example to use Bootstrap 3.
parent
207d23fd
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
100 additions
and
70 deletions
+100
-70
app.py
examples/auth/app.py
+13
-2
index.html
examples/auth/templates/admin/index.html
+25
-24
my_master.html
examples/auth/templates/my_master.html
+9
-10
_macros.html
examples/auth/templates/security/_macros.html
+23
-12
login_user.html
examples/auth/templates/security/login_user.html
+15
-11
register_user.html
examples/auth/templates/security/register_user.html
+15
-11
No files found.
examples/auth/app.py
View file @
4a48fa42
...
@@ -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
))
...
...
examples/auth/templates/admin/index.html
View file @
4a48fa42
{% 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
examples/auth/templates/my_master.html
View file @
4a48fa42
...
@@ -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>
...
...
examples/auth/templates/security/_macros.html
View file @
4a48fa42
{% 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
examples/auth/templates/security/login_user.html
View file @
4a48fa42
{% 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
examples/auth/templates/security/register_user.html
View file @
4a48fa42
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment