Permalink
Browse files

Incluido Flask-Admin

  • Loading branch information...
rochacbruno committed Oct 20, 2015
1 parent 3766bfa commit 1359c4cf9a31a0d471a3226a1bec2a672f9ffbbb
Showing with 76 additions and 5 deletions.
  1. +1 −0 requirements.txt
  2. +45 −0 wtf/admin.py
  3. +3 −2 wtf/development_instance/config.cfg
  4. +2 −0 wtf/news_app.py
  5. +0 −3 wtf/security_models.py
  6. +25 −0 wtf/templates/admin_base.html
View
@@ -4,3 +4,4 @@ nose
Flask-Bootstrap
Flask-Security
Flask-Login==0.2.11
Flask-Admin
View
@@ -0,0 +1,45 @@
# coding: utf-8
from flask import abort, redirect, request, url_for
from flask_security import current_user
from flask_admin.contrib.mongoengine import ModelView
from flask_admin import Admin
from .models import Noticia
from .security_models import User, Role
admin = Admin(name='Noticias', template_mode='bootstrap3',
base_template='admin_base.html')
# Create customized model view class
class SafeModelView(ModelView):
def is_accessible(self):
if not current_user.is_authenticated():
return False
# if not current_user.has_role('admin'):
# return False
return True
def _handle_view(self, name, **kwargs):
"""
Override builtin _handle_view in order to redirect users when a
view is not accessible.
"""
if not self.is_accessible():
if current_user.is_authenticated():
abort(403) # denied
else:
return redirect(url_for('security.login', next=request.url))
class UserModelView(SafeModelView):
column_list = ("name", "email", "active", "last_login_at", "login_count")
def configure_admin(app):
admin.init_app(app)
admin.add_view(SafeModelView(Noticia))
admin.add_view(UserModelView(User, category='accounts'))
admin.add_view(SafeModelView(Role, category='accounts'))
@@ -10,9 +10,10 @@ MONGODB_HOST = "localhost" # substitua se utilizar um server remoto
MONGODB_PORT = 27017
SECRET_KEY = 'super-secret'
SECURITY_PASSWORD_HASH = 'pbkdf2_sha512'
SECURITY_PASSWORD_SALT = SECRET_KEY
SECURITY_REGISTERABLE = True
SECURITY_TRACKABLE = True
# as opções abaixo devem ser removidas em ambiente de produção
SECURITY_SEND_REGISTER_EMAIL = False
View
@@ -3,6 +3,7 @@
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_security import Security, MongoEngineUserDatastore
from .admin import configure_admin
from .blueprints.noticias import noticias_blueprint
from .db import db
@@ -32,4 +33,5 @@ def create_app(mode):
Bootstrap(app)
db.init_app(app)
Security(app=app, datastore=MongoEngineUserDatastore(db, User, Role))
configure_admin(app)
return app
View
@@ -31,9 +31,6 @@ class User(db.Document, UserMixin):
last_login_ip = db.StringField(max_length=255)
current_login_ip = db.StringField(max_length=255)
login_count = db.IntField()
username = db.StringField(max_length=50, required=False, unique=True)
remember_token = db.StringField(max_length=255)
authentication_token = db.StringField(max_length=255)
@classmethod
def createuser(cls, name, email, password,
@@ -0,0 +1,25 @@
{% extends 'admin/base.html' %}
{% block access_control %}
<div class="navbar-text btn-group pull-right">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<i class="glyphicon glyphicon-user"></i>
{% if current_user.is_authenticated() %}
{% if current_user.name -%}
{{ current_user.name }}
{% else -%}
{{ current_user.email }}
{%- endif %}
<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="{{url_for_security('logout')}}">Log out</a></li>
</ul>
{% else %}
Access
<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="{{url_for_security('login')}}">Login</a></li>
</ul>
{% endif %}
</div>
{% endblock %}

0 comments on commit 1359c4c

Please sign in to comment.