Coverage for src/mesh/views/components/tree_node.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.7.0, created at 2025-04-28 07:45 +0000

1from __future__ import annotations 

2 

3from dataclasses import dataclass, field 

4from typing import Generic, TypeVar 

5 

6from django.urls import reverse_lazy 

7 

8from .button import Button 

9 

10T = TypeVar("T") 

11 

12 

13@dataclass 

14class TreeNode(Generic[T]): 

15 # class TreeNode[T]: NEW SYNTAX (python 3.12) that discards the use of both TypeVar and Generic 

16 """ 

17 Represents a tree item. 

18 """ 

19 

20 item: T | None 

21 href: str = field(default="") 

22 buttons: list[Button] = field(default_factory=list) 

23 children: list[TreeNode[T]] = field(default_factory=list) 

24 

25 

26def build_tree_recursive(journal_section_manager, journal_section=None): 

27 """ 

28 Return the tree of JournalSection, starting from the given journal_section. 

29 """ 

30 tree_node = TreeNode(item=journal_section) 

31 if journal_section: 

32 tree_node.href = reverse_lazy( 

33 "mesh:submission_journal_section_update", kwargs={"pk": journal_section.pk} 

34 ) 

35 

36 children_dict = journal_section_manager.all_journal_sections_children() 

37 key = None if journal_section is None else journal_section.pk 

38 tree_node.children = [ 

39 build_tree_recursive(journal_section_manager, c) for c in children_dict[key] 

40 ] 

41 return tree_node