自動獲取專案中的url
阿新 • • 發佈:2022-02-12
Example/settings.py
# 自動獲取專案中URL所剔除的名單 AUTO_DISCOVER_EXCLUDE = [ '/admin/.*', '/login/', ]
rbac/service/routes.py
"""自動獲取專案中的URL""" from collections import OrderedDict from django.conf import settings from django.utils.module_loading import import_string from django.urls import URLResolver, URLPatternimport re def check_url_exclude(url): """ 排除一些特定的URL :param url: :return: """ for regex in settings.AUTO_DISCOVER_EXCLUDE: if re.match(regex, url): return True def recursion_urls(pre_namespace, pre_url, urlpatterns, url_ordered_dict): """ 遞迴的去獲取URL :param pre_namespace: namespace字首,以後用於拼接name :param pre_url: url字首,以後用於拼接url :param urlpatterns: 路由關係列表 :param url_ordered_dict: 用於儲存遞迴中獲取的所有路由 :return:""" # re_path(r"^", include("app01.urls")), 路由分發URLResolver # re_path(r"^/xxx/xx", lambda x: x), 非路由分發,對應的是檢視函式,那麼是URLPattern for item in urlpatterns: if isinstance(item, URLPattern): # 非路由分發,將路由新增到url_ordered_dict if not item.name: continue ifpre_namespace: name = "%s:%s" % (pre_namespace, item.name) else: name = item.name url = pre_url + item.pattern.regex.pattern # /^rbac/^user/edit/(?P<pk>\d+)/$ url = url.replace('^', '').replace('$', '') # /rbac/user/edit/(?P<pk>\d+)/ if check_url_exclude(url): continue url_ordered_dict[name] = {"name": name, "url": url} elif isinstance(item, URLResolver): # 路由分發,遞迴操作 if pre_namespace: if item.namespace: namespace = "%s:%s" % (pre_namespace, item.namespace) else: namespace = item.namespace else: if item.namespace: namespace = item.namespace else: namespace = None recursion_urls(namespace, pre_url + item.pattern.regex.pattern, item.url_patterns, url_ordered_dict) def get_all_url_dict(): """ 獲取專案中所有的URL(必須有name別名) :return: """ url_ordered_dict = OrderedDict() md = import_string(settings.ROOT_URLCONF) # from Example.Example import urls recursion_urls(None, '/', md.urlpatterns, url_ordered_dict) # 遞迴去獲取所有的路由 return url_ordered_dict
rbac/views/menu.py
from rbac.service.routes import get_all_url_dict def multi_permissions(request): """ 批量操作許可權 :param request: :return: """ # 獲取專案中所有的URL all_url_dict = get_all_url_dict() for k, v in all_url_dict.items(): print(k, '---', v) # rbac:role_list --- {'name': 'rbac:role_list', 'url': '/rbac/role/list/'} return HttpResponse("...") """ rbac:role_list --- {'name': 'rbac:role_list', 'url': '/rbac/role/list/'} rbac:role_add --- {'name': 'rbac:role_add', 'url': '/rbac/role/add/'} rbac:role_edit --- {'name': 'rbac:role_edit', 'url': '/rbac/role/edit/(?P<pk>\\d+)/'} rbac:role_del --- {'name': 'rbac:role_del', 'url': '/rbac/role/del/(?P<pk>\\d+)/'} rbac:user_list --- {'name': 'rbac:user_list', 'url': '/rbac/user/list/'} rbac:user_add --- {'name': 'rbac:user_add', 'url': '/rbac/user/add/'} rbac:user_edit --- {'name': 'rbac:user_edit', 'url': '/rbac/user/edit/(?P<pk>\\d+)/'} rbac:user_del --- {'name': 'rbac:user_del', 'url': '/rbac/user/del/(?P<pk>\\d+)/'} rbac:user_reset_pwd --- {'name': 'rbac:user_reset_pwd', 'url': '/rbac/user/reset/password/(?P<pk>\\d+)/'} rbac:menu_list --- {'name': 'rbac:menu_list', 'url': '/rbac/menu/list/'} rbac:menu_add --- {'name': 'rbac:menu_add', 'url': '/rbac/menu/add/'} rbac:menu_edit --- {'name': 'rbac:menu_edit', 'url': '/rbac/menu/edit/(?P<pk>\\d+)/'} rbac:menu_del --- {'name': 'rbac:menu_del', 'url': '/rbac/menu/del/(?P<pk>\\d+)/'} rbac:second_menu_add --- {'name': 'rbac:second_menu_add', 'url': '/rbac/second/menu/add/(?P<menu_id>\\d+)/'} rbac:second_menu_edit --- {'name': 'rbac:second_menu_edit', 'url': '/rbac/second/menu/edit/(?P<pk>\\d+)/'} rbac:second_menu_del --- {'name': 'rbac:second_menu_del', 'url': '/rbac/second/menu/del/(?P<pk>\\d+)/'} rbac:permission_add --- {'name': 'rbac:permission_add', 'url': '/rbac/permissions/add/(?P<second_menu_id>\\d+)/'} rbac:permission_edit --- {'name': 'rbac:permission_edit', 'url': '/rbac/permissions/edit/(?P<pk>\\d+)/'} rbac:permission_del --- {'name': 'rbac:permission_del', 'url': '/rbac/permissions/del/(?P<pk>\\d+)/'} rbac:multi_permissions --- {'name': 'rbac:multi_permissions', 'url': '/rbac/multi/permissions/'} """while True: print('studying...')