A, generating a dynamic menu of API
1、API
#菜单信息 url(r'^menus$', views.MenuModelView.as_view({"get": "list", }), name="menus-list"),
2、MenuModelView
from collections import OrderedDictclass MenuModelView(GenericViewSet): authentication_classes = [AuthToken] def list(self,request,*args,**kwargs): ret = {"data": {}, "meta": {"message": "获取菜单成功", "code": 2000}} try: user = request.user menus_dict = InitPermission(request, user).init_menus_dict() od = OrderedDict() if menus_dict: for key in sorted(menus_dict): od[key] = menus_dict[key] ret["data"] = {"menus_list":od.values()} except Exception as e: ret["meta"]["message"] = "获取菜单失败" ret["meta"]["code"] = 2001 return Response(ret)
In the framework of authentication component restframework authenticated by authentication_classes = [AuthToken], to obtain user, so that the incoming user, thereby initializing the menu data, the menu can then be sorted according to the database positionid.
Certified Reference: https://www.cnblogs.com/shenjianping/p/11387324.html
Initialization Menu Reference: https://www.cnblogs.com/shenjianping/p/11448427.html
Second, the authority API generation
1、API
# Distal acquiring rights to the test button level permissions URL (R & lt ' ^ Roles / Rights $ ' , views.RightsView.as_view ({ " GET " : " List " }), name = " Roles-Rights-List " ) ,
2、RightsView
class RightsView (GenericViewSet): DEF List (Self, Request, * args, ** kwargs): "" " obtaining permission from the relevant redis for permission to test the reception level button { '/ crm / dept $' : [ 'get '],' Rights / (P <type> \\ W +?) $ ': [' GET '],' / CRM / Menus': [ 'GET'] : param Request: : param args: : param kwargs: : return: "" " # Get the permission_dict from redis RET = { " Data " : {}, " Meta " : { " Message " : "Get Permissions information success " , " code ": 2000}} try: permission_bytes = SessionStore().get_session(settings.PERMISSION_SESSION_KEY) permission_dict = eval(permission_bytes) ret["data"] = permission_dict except Exception as e: ret["meta"]["message"] = "获取权限信息失败" ret["meta"]["code"] = 2001 return Response(ret)
After initialization will save user rights in redis in return here is stored in redis user permissions.