Django の URL 数を表示するコード

投稿者: ytyng 1年, 5ヶ月 前

IT監査の際、アプリケーションの規模を表す必要があったので、Django の総 URL 数を表示することでアプリケーションの規模とみなすことにした。

from django.urls.resolvers import get_resolver
root_resolver = get_resolver()


def _join_namespace(a, b):
    return (a + ':' if a else '') + (b or '')


def _count_urls(namespace, resolver):
    count = 0
    _namespace = _join_namespace(namespace, resolver.namespace)
    for ns, (prefix, res) in resolver.namespace_dict.items():
        count += _count_urls(_namespace, res)

    for n, l in resolver.reverse_dict.items():
        if callable(n):
            continue
        count += 1
    return count


print('URLS count:', _count_urls('', root_resolver))
現在未評価

コメント

アーカイブ

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011