Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/rest_framework/utils/breadcrumbs.py: 6%

28 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from django.urls import get_script_prefix, resolve 

2 

3 

4def get_breadcrumbs(url, request=None): 

5 """ 

6 Given a url returns a list of breadcrumbs, which are each a 

7 tuple of (name, url). 

8 """ 

9 from rest_framework.reverse import preserve_builtin_query_params 

10 from rest_framework.views import APIView 

11 

12 def breadcrumbs_recursive(url, breadcrumbs_list, prefix, seen): 

13 """ 

14 Add tuples of (name, url) to the breadcrumbs list, 

15 progressively chomping off parts of the url. 

16 """ 

17 try: 

18 (view, unused_args, unused_kwargs) = resolve(url) 

19 except Exception: 

20 pass 

21 else: 

22 # Check if this is a REST framework view, 

23 # and if so add it to the breadcrumbs 

24 cls = getattr(view, 'cls', None) 

25 initkwargs = getattr(view, 'initkwargs', {}) 

26 if cls is not None and issubclass(cls, APIView): 

27 # Don't list the same view twice in a row. 

28 # Probably an optional trailing slash. 

29 if not seen or seen[-1] != view: 

30 c = cls(**initkwargs) 

31 name = c.get_view_name() 

32 insert_url = preserve_builtin_query_params(prefix + url, request) 

33 breadcrumbs_list.insert(0, (name, insert_url)) 

34 seen.append(view) 

35 

36 if url == '': 

37 # All done 

38 return breadcrumbs_list 

39 

40 elif url.endswith('/'): 

41 # Drop trailing slash off the end and continue to try to 

42 # resolve more breadcrumbs 

43 url = url.rstrip('/') 

44 return breadcrumbs_recursive(url, breadcrumbs_list, prefix, seen) 

45 

46 # Drop trailing non-slash off the end and continue to try to 

47 # resolve more breadcrumbs 

48 url = url[:url.rfind('/') + 1] 

49 return breadcrumbs_recursive(url, breadcrumbs_list, prefix, seen) 

50 

51 prefix = get_script_prefix().rstrip('/') 

52 url = url[len(prefix):] 

53 return breadcrumbs_recursive(url, [], prefix, [])