Coverage for apis_core/utils/utils.py: 18%

40 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-16 07:42 +0000

1from django.conf import settings 

2 

3 

4def access_for_all(self, viewtype="list"): 

5 if self.request.user.is_authenticated: 

6 return self.request.user.is_authenticated 

7 match viewtype: 

8 case "list": 

9 return getattr(settings, "APIS_LIST_VIEWS_ALLOWED", False) 

10 case "detail": 

11 return getattr(settings, "APIS_DETAIL_VIEWS_ALLOWED", False) 

12 return False 

13 

14 

15def access_for_all_function(user): 

16 if user.is_anonymous: 

17 return getattr(settings, "APIS_DETAIL_VIEWS_ALLOWED", False) 

18 else: 

19 return True 

20 

21 

22ENTITIES_DEFAULT_COLS = [ 

23 "start_date", 

24 "start_date_written", 

25 "end_date", 

26 "end_date_written", 

27 "text", 

28 "collection", 

29 "status", 

30 "source", 

31 "references", 

32 "notes", 

33] 

34 

35 

36def get_child_classes(objids, obclass, labels=False): 

37 """used to retrieve a list of primary keys of sub classes""" 

38 if labels: 

39 labels_lst = [] 

40 for obj in objids: 

41 obj = obclass.objects.get(pk=obj) 

42 p_class = list(obj.vocabsbaseclass_set.all()) 

43 p = p_class.pop() if len(p_class) > 0 else False 

44 while p: 

45 if p.pk not in objids: 

46 if labels: 

47 labels_lst.append((p.pk, p.label)) 

48 objids.append(p.pk) 

49 p_class += list(p.vocabsbaseclass_set.all()) 

50 p = p_class.pop() if len(p_class) > 0 else False 

51 if labels: 

52 return (objids, labels_lst) 

53 else: 

54 return objids 

55 

56 

57def get_python_safe_module_path(instance: object): 

58 """ 

59 return a python safe version of the full path of an object 

60 this can for example be used as a method name 

61 """ 

62 modulepath = get_module_path(instance) 

63 return modulepath.replace(".", "_") 

64 

65 

66def get_module_path(instance: object): 

67 """ 

68 return the full path to the class of an object 

69 """ 

70 instance_type = type(instance) 

71 module = instance_type.__module__ 

72 name = instance_type.__name__ 

73 return f"{module}.{name}"