1. 程式人生 > >我的第一個python web開發框架(39)——後臺介面許可權訪問控制處理

我的第一個python web開發框架(39)——後臺介面許可權訪問控制處理

 1 @get('/api/main/menu_info/')
 2 def callback():
 3     """
 4     主頁面獲取選單列表資料
 5     """
 6     # 獲取當前使用者許可權
 7     session = web_helper.get_session()
 8     if session:
 9         _positions_logic = positions_logic.PositionsLogic()
10         page_power = _positions_logic.get_page_power(session.get('
positions_id')) 11 else: 12 page_power = '' 13 if not page_power: 14 return web_helper.return_msg(-404, '您的登入已超時,請重新登入') 15 16 _menu_info_logic = menu_info_logic.MenuInfoLogic() 17 # 讀取記錄 18 result = _menu_info_logic.get_list('*', 'is_show and is_enabled', orderby='
sort') 19 if result: 20 # 定義最終輸出的html儲存變數 21 html = '' 22 for model in result.get('rows'): 23 # 檢查是否有許可權 24 if ',' + str(model.get('id')) + ',' in page_power: 25 # 提取出第一級選單 26 if model.get('parent_id') == 0: 27
# 新增一級選單 28 temp = """ 29 <dl id="menu-%(id)s"> 30 <dt><i class="Hui-iconfont">%(icon)s</i> %(name)s<i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i></dt> 31 <dd> 32 <ul> 33 """ % {'id': model.get('id'), 'icon': model.get('icon'), 'name': model.get('name')} 34 html = html + temp 35 36 # 從所有選單記錄中提取當前一級選單下的子選單 37 for sub_model in result.get('rows'): 38 # 檢查是否有許可權 39 if ',' + str(sub_model.get('id')) + ',' in page_power: 40 # 如果父id等於當前一級選單id,則為當前選單的子選單 41 if sub_model.get('parent_id') == model.get('id'): 42 temp = """ 43 <li><a data-href="%(page_url)s" data-title="%(name)s" href="javascript:void(0)">%(name)s</a></li> 44 """ % {'page_url': sub_model.get('page_url'), 'name': sub_model.get('name')} 45 html = html + temp 46 47 # 閉合選單html 48 temp = """ 49 </ul> 50 </dd> 51 </dl> 52 """ 53 html = html + temp 54 55 return web_helper.return_msg(0, '成功', {'menu_html': html}) 56 else: 57 return web_helper.return_msg(-1, "查詢失敗")