Skip to content

Instantly share code, notes, and snippets.

@jak010
Created April 4, 2023 01:43
Show Gist options
  • Save jak010/fe4e5d94d0c38cfec59d7a4c102909eb to your computer and use it in GitHub Desktop.
Save jak010/fe4e5d94d0c38cfec59d7a4c102909eb to your computer and use it in GitHub Desktop.
django view print
from __future__ import annotations
import os
from functools import cached_property
from typing import List
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
import django
django.setup()
from django.urls import URLResolver
from django.urls.resolvers import RegexPattern
from django.conf import settings
class EndPointMeta:
def __init__(self, prefix, pattern, name):
self.prefix = prefix
self.pattern = pattern
self.name = name
def __repr__(self):
return f"[Prefix=]{self.prefix},\n" \
f"[Pattern=]{self.pattern},\n" \
f"[Name=]{self.name}"
class EndPoint:
ROOT_URL_CONF = settings.ROOT_URLCONF
@cached_property
def root(self) -> URLResolver:
return URLResolver(RegexPattern(r"^/"), self.ROOT_URL_CONF)
def collect(self) -> List[EndPointMeta]:
root_patterns: List[URLResolver] = [root_url_pattern for root_url_pattern in self.root.url_patterns]
objs = []
for root_pattern in root_patterns:
for sub_pattern in root_pattern.url_patterns:
objs.append(EndPointMeta(
prefix=str(root_pattern.pattern),
pattern=str(sub_pattern.pattern),
name=str(sub_pattern.name))
)
return objs
class Report:
def __init__(self, endpoint_meta: List[EndPointMeta]):
self.endpoint_meta = endpoint_meta
def completed_test_count(self):
count = 0
for endpoint_meta in self.endpoint_meta:
if endpoint_meta.name != 'None':
count += 1
return count
def all_endpoint_count(self):
return len(self.endpoint_meta)
if __name__ == '__main__':
endpoint = EndPoint()
all_endpoint = endpoint.collect()
report = Report(endpoint_meta=all_endpoint)
print(f"{'prefix':50s} | {'pattern':50s} | {'Name':50s}")
for endpoint in all_endpoint:
print(f"{endpoint.prefix:50s} | {endpoint.pattern:50s} | {endpoint.name:50s}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment