Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/urls/utils.py: 60%

33 statements  

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

1import functools 

2from importlib import import_module 

3 

4from django.core.exceptions import ViewDoesNotExist 

5from django.utils.module_loading import module_has_submodule 

6 

7 

8@functools.lru_cache(maxsize=None) 

9def get_callable(lookup_view): 

10 """ 

11 Return a callable corresponding to lookup_view. 

12 * If lookup_view is already a callable, return it. 

13 * If lookup_view is a string import path that can be resolved to a callable, 

14 import that callable and return it, otherwise raise an exception 

15 (ImportError or ViewDoesNotExist). 

16 """ 

17 if callable(lookup_view): 

18 return lookup_view 

19 

20 if not isinstance(lookup_view, str): 20 ↛ 21line 20 didn't jump to line 21, because the condition on line 20 was never true

21 raise ViewDoesNotExist( 

22 "'%s' is not a callable or a dot-notation path" % lookup_view 

23 ) 

24 

25 mod_name, func_name = get_mod_func(lookup_view) 

26 if not func_name: # No '.' in lookup_view 26 ↛ 27line 26 didn't jump to line 27, because the condition on line 26 was never true

27 raise ImportError( 

28 "Could not import '%s'. The path must be fully qualified." % lookup_view 

29 ) 

30 

31 try: 

32 mod = import_module(mod_name) 

33 except ImportError: 

34 parentmod, submod = get_mod_func(mod_name) 

35 if submod and not module_has_submodule(import_module(parentmod), submod): 

36 raise ViewDoesNotExist( 

37 "Could not import '%s'. Parent module %s does not exist." 

38 % (lookup_view, mod_name) 

39 ) 

40 else: 

41 raise 

42 else: 

43 try: 

44 view_func = getattr(mod, func_name) 

45 except AttributeError: 

46 raise ViewDoesNotExist( 

47 "Could not import '%s'. View does not exist in module %s." 

48 % (lookup_view, mod_name) 

49 ) 

50 else: 

51 if not callable(view_func): 51 ↛ 52line 51 didn't jump to line 52, because the condition on line 51 was never true

52 raise ViewDoesNotExist( 

53 "Could not import '%s.%s'. View is not callable." 

54 % (mod_name, func_name) 

55 ) 

56 return view_func 

57 

58 

59def get_mod_func(callback): 

60 # Convert 'django.views.news.stories.story_detail' to 

61 # ['django.views.news.stories', 'story_detail'] 

62 try: 

63 dot = callback.rindex(".") 

64 except ValueError: 

65 return callback, "" 

66 return callback[:dot], callback[dot + 1 :]