Coverage for utilities/urls.py: 89%

21 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django.urls import path 

2from django.utils.module_loading import import_string 

3from django.views.generic import View 

4 

5from netbox.registry import registry 

6 

7__all__ = ( 

8 'get_model_urls', 

9) 

10 

11 

12def get_model_urls(app_label, model_name, detail=True): 

13 """ 

14 Return a list of URL paths for detail views registered to the given model. 

15 

16 Args: 

17 app_label: App/plugin name 

18 model_name: Model name 

19 detail: If True (default), return only URL views for an individual object. 

20 Otherwise, return only list views. 

21 """ 

22 paths = [] 

23 

24 # Retrieve registered views for this model 

25 try: 

26 views = [ 

27 view for view in registry['views'][app_label][model_name] 

28 if view['detail'] == detail 

29 ] 

30 except KeyError: 

31 # No views have been registered for this model 

32 return [] 

33 

34 for config in views: 

35 # Import the view class or function 

36 if type(config['view']) is str: 

37 view_ = import_string(config['view']) 

38 else: 

39 view_ = config['view'] 

40 if issubclass(view_, View): 40 ↛ 44line 40 didn't jump to line 44 because the condition on line 40 was always true

41 view_ = view_.as_view() 

42 

43 # Create a path to the view 

44 name = f"{model_name}_{config['name']}" if config['name'] else model_name 

45 url_path = f"{config['path']}/" if config['path'] else '' 

46 paths.append( 

47 path(url_path, view_, name=name, kwargs=config['kwargs']) 

48 ) 

49 

50 return paths