Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

works with principal and flask-admin

  • Loading branch information...
commit d5266b440c6f96736de46fdae3eee42d2ae18318 1 parent 4653f2f
Hsiaoming Yang authored
View
32 june/app.py
@@ -8,9 +8,11 @@
from flask import Flask
from flask import request, g
from flask.ext.babel import Babel
+from flask.ext.principal import Principal, Identity, identity_loaded
+from flask.ext.principal import UserNeed, RoleNeed
from .helpers import get_current_user
from .models import db
-from .views import front, account
+from .views import front, account, admin
def create_app(config=None):
@@ -29,6 +31,8 @@ def create_app(config=None):
db.init_app(app)
db.app = app
+ admin.admin.init_app(app)
+
#: register blueprints
app.register_blueprint(account.bp, url_prefix='/account')
app.register_blueprint(front.bp, url_prefix='')
@@ -48,4 +52,30 @@ def get_locale():
default = app.config['BABEL_DEFAULT_LOCALE']
return request.accept_languages.best_match(match, default)
+ princi = Principal(app)
+
+ @princi.identity_loader
+ def load_identity():
+ return Identity('june')
+
+ @identity_loaded.connect_via(app)
+ def on_identity_loaded(sender, identity):
+ identity.user = g.user
+ if not g.user:
+ return
+ identity.provides.add(UserNeed(g.user.id))
+ if g.user.id == 1:
+ identity.provides.add(RoleNeed('superuser'))
+ identity.provides.add(RoleNeed('admin'))
+ identity.provides.add(RoleNeed('staff'))
+ identity.provides.add(RoleNeed('member'))
+
+ if g.user.role > 2:
+ identity.provides.add(RoleNeed('member'))
+
+ if g.user.role > 10:
+ identity.provides.add(RoleNeed('staff'))
+
+ if g.user.role > 20:
+ identity.provides.add(RoleNeed('admin'))
return app
View
19 june/models/account.py
@@ -4,7 +4,7 @@
import random
from datetime import datetime
from werkzeug import cached_property
-from flask.ext.principal import Permission, UserNeed
+from flask.ext.principal import Permission, UserNeed, RoleNeed
from ._base import db, JuneQuery, SessionMixin
__all__ = ['Account']
@@ -62,21 +62,12 @@ def avatar(self):
return db.app.config['GRAVATAR_BASE_URL'] + query
@cached_property
- def comment_service_name(self):
- if self.comment_service:
- return self.comment_service.split('-')[0]
- return None
-
- @cached_property
- def comment_service_id(self):
- if self.comment_service:
- bits = self.comment_service.split('-')
- return '-'.join(bits[1:])
- return None
+ def permission_write(self):
+ return Permission(UserNeed(self.id), RoleNeed('admin'))
@cached_property
- def permission_write(self):
- return Permission(UserNeed(self.id))
+ def permission_admin(self):
+ return Permission(RoleNeed('admin'))
@staticmethod
def create_password(raw):
View
42 june/views/admin.py
@@ -0,0 +1,42 @@
+# coding: utf-8
+
+from flask import g
+from flask.ext.admin import Admin, AdminIndexView, expose
+from flask.ext.admin.contrib.sqlamodel import ModelView
+from ..models import db, Account
+
+
+class BaseView(ModelView):
+ column_display_pk = True
+ can_create = False
+ can_edit = False
+
+ def is_accessible(self):
+ if not g.user:
+ return False
+ return g.user.permission_admin.can()
+
+
+class HomeView(AdminIndexView):
+ @expose('/')
+ def index(self):
+ return self.render('admin/index.html')
+
+ def is_accessible(self):
+ if not g.user:
+ return False
+ if g.user.id == 1:
+ return True
+ if g.user.account_type != 'user':
+ return False
+ return g.user.role > 40
+
+
+class UserView(BaseView):
+ can_edit = True
+ column_exclude_list = ('password', 'token', 'description')
+ form_excluded_columns = ('password', 'created', 'token')
+
+
+admin = Admin(name='Yuan', index_view=HomeView())
+admin.add_view(UserView(Account, db.session))
View
10 manager.py
@@ -6,20 +6,21 @@
from flask.ext.script import Manager
from june.app import create_app
+#CONFIG = os.path.abspath('./etc/config.py')
CONFIG = '_config/development.py'
-manager = Manager(create_app())
+app = create_app(CONFIG)
+manager = Manager(app)
@manager.command
-def runserver(port=5000, config=CONFIG):
+def runserver(port=5000):
"""Runs a development server."""
from gevent.wsgi import WSGIServer
from werkzeug.serving import run_with_reloader
from werkzeug.debug import DebuggedApplication
port = int(port)
- app = create_app(config)
@run_with_reloader
def run_server():
@@ -31,10 +32,9 @@ def run_server():
@manager.command
-def createdb(config=CONFIG):
+def createdb():
"""Create a database."""
from june.models import db
- create_app(config)
db.create_all()
Please sign in to comment.
Something went wrong with that request. Please try again.