2014-07-15 55 views
0

我有一個Flask-Admin的應用程序來管理模型視圖/admin/userview的用戶。 我希望它顯示頁面,當用戶在數據庫中正確的組導航到/admin/userview,並返回純文本「不管理」,如果他們不是。幸運的是,我得到了最後一部分工作,但不幸的是,我似乎無法得到第一部分(如果他們在正確的組中,繼續顯示該頁面)。下面是相關的代碼:Flask-Admin模型查看功能

class MyView(ModelView): 
    @expose('/', methods = ['GET', 'POST']) 
    def index(self): 
    ## grab kerberos username (PROD) 
     secuser = request.environ.get('REMOTE_USER') 

     adminresult = User.query.filter_by(usrlevel='admin').all() 
     adminresult = str(adminresult) 
     adminresult = adminresult.strip('[]') 

     managerresult = User.query.filter_by(usrlevel='manager').all() 
     managerresult = str(managerresult) 
     managerresult = managerresult.strip('[]')  

     if secuser in adminresult: 
      pass   <---------------\ 
     elif secuser in managerresult: |- if a user is apart of either, this gives a ValueError 
      pass   <---------------/ 
     else: 
      return "NOT ADMIN" <--------- This works! 

##ADMIN 
admin = Admin(flaskapp, index_view=MyHomeView(), name="test APP") 
admin.add_view(MyView(User, db.session, "Edit Users")) 

下面是我得到的,當用戶在adminresultmanagerresult回溯:

Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__ 
    return self.wsgi_app(environ, start_response) 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app 
    response = self.make_response(self.handle_exception(e)) 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception 
    reraise(exc_type, exc_value, tb) 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app 
    response = self.full_dispatch_request() 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1478, in full_dispatch_request 
    response = self.make_response(rv) 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1566, in make_response 
    raise ValueError('View function did not return a response') 
ValueError: View function did not return a response 

如何獲得ModelView「用戶」顯示,如果它的內容用戶在任何一個組中,並且如果它們不是則返回「不是管理員」文本?我想我已經完成了一半,只是繼續似乎是一個問題...

謝謝!

回答

1

你應該居然把支票在is_accessible

class MyView(ModelView): 
    def is_accessible(self): 
     # grab kerberos username (PROD) 
     secuser = request.environ.get('REMOTE_USER') 

     admins_and_managers = User.query 
            .filter(User.usrlevel.in_('admin', 'manager')) 
            # Better yet, filter again on `secuser` 
            # to avoid loading *every* admin and manager 
            # on *every* request for this resource 
            # and then use `.first` or `.one` 
            .all() 

     return secuser in admins_and_managers 
+0

不得不做字符串格式化得到它,看看'secuser'是太感謝你了,我一直工作在這幾天。 !我欠你一杯啤酒。 – asdfgthereisnocowlevel

+0

如果你必須做字符串格式化,那麼你可能依賴'User'的'__str__'方法 - 從長遠來看這不是一個好主意。最好是通過適當的字段進行過濾(添加一個'filter_by(username = secuser)',其中'username'是在User.__ str__'或'User .__ repr__'中使用的字段的名稱。)然後,您可以使用'.first'而不是'.all'並將支票更改爲'admins_and_managers不是None') –