Coverage for src/couchers/helpers/clusters.py: 95%

22 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-20 18:03 +0000

1from geoalchemy2.shape import from_shape 

2 

3from couchers.models import Cluster, ClusterRole, ClusterSubscription, Node, Page, PageType, PageVersion, Thread 

4 

5DEFAULT_PAGE_CONTENT = "There is nothing here yet..." 

6DEFAULT_PAGE_TITLE_TEMPLATE = "Main page for the {name} {type}" 

7 

8 

9def create_node(session, geom, parent_node_id): 

10 node = Node(geom=from_shape(geom), parent_node_id=parent_node_id) 

11 session.add(node) 

12 session.flush() 

13 return node 

14 

15 

16def create_cluster( 

17 session, 

18 parent_node_id: int, 

19 name: str, 

20 description: str, 

21 creator_user_id: int, 

22 admin_ids: list, 

23 is_community: bool, 

24): 

25 cluster_type = "community" if is_community else "group" 

26 cluster = Cluster( 

27 name=name, 

28 description=description, 

29 parent_node_id=parent_node_id, 

30 is_official_cluster=is_community, 

31 ) 

32 session.add(cluster) 

33 session.flush() 

34 main_page = Page( 

35 parent_node=cluster.parent_node, 

36 creator_user_id=creator_user_id, 

37 owner_cluster=cluster, 

38 type=PageType.main_page, 

39 thread=Thread(), 

40 ) 

41 session.add(main_page) 

42 session.flush() 

43 page_version = PageVersion( 

44 page=main_page, 

45 editor_user_id=creator_user_id, 

46 title=DEFAULT_PAGE_TITLE_TEMPLATE.format(name=name, type=cluster_type), 

47 content=DEFAULT_PAGE_CONTENT, 

48 ) 

49 session.add(page_version) 

50 for admin_id in admin_ids: 

51 cluster.cluster_subscriptions.append( 

52 ClusterSubscription( 

53 user_id=admin_id, 

54 role=ClusterRole.admin, 

55 ) 

56 ) 

57 return cluster