Coverage for postrfp/ref/handlers/readers.py: 89%

18 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-22 21:34 +0000

1""" 

2HTTP handler functions supporting a consumer of the content 

3managed by this system 

4""" 

5 

6from typing import Optional 

7 

8from sqlalchemy.orm import Session 

9 

10from postrfp.shared.pager import Pager 

11from postrfp.shared.decorators import http 

12from postrfp.model import User 

13from postrfp.model.ref import Content 

14from postrfp.shared.serial.refmodels import ( 

15 ListResponse, 

16 ContentDocument, 

17 ContentRelationshipDoc, 

18 ContentRelationships, 

19) 

20from postrfp.ref.service.search_service import search_content_by_subject 

21 

22 

23@http 

24def get_search( 

25 session: Session, 

26 user: User, 

27 q_subject_id: Optional[int] = None, 

28 q_term: Optional[str] = None, 

29 pager: Optional[Pager] = None, 

30) -> ListResponse: 

31 """ 

32 List content items related to a specific subject 

33 

34 Returns a list of content items that are associated with the specified subject 

35 and that the current user has permission to view. Results can be filtered 

36 by content title and are paginated. 

37 """ 

38 if pager is None: 

39 pager = Pager(page=1, page_size=20) 

40 

41 # Use service function to perform the search 

42 total_records, records = search_content_by_subject( 

43 session=session, 

44 user_org_id=user.org_id, 

45 subject_id=q_subject_id, 

46 search_term=q_term, 

47 pager=pager, 

48 ) 

49 

50 return ListResponse( 

51 items=[ContentDocument.model_validate(content) for content in records], 

52 pagination=pager.as_pagination(total_records, len(records)), 

53 ) 

54 

55 

56@http 

57def get_content_relationships( 

58 session: Session, 

59 user: User, 

60 content_id: int, 

61) -> ContentRelationshipDoc: 

62 """ 

63 Get all relationships for a specific content item 

64 

65 Returns content items that are related to the specified content, 

66 grouped by relationship type (parent, child, related, etc.). 

67 Only returns content the user has permission to view. 

68 """ 

69 content = session.get_one(Content, content_id) 

70 

71 # TODO Implement policy check 

72 

73 # Get related content grouped by relationship type 

74 # Note: This would use a service layer function in a real implementation 

75 # for complex permission filtering of the related content 

76 

77 # # Placeholder for actual implementation 

78 # relationships = { 

79 # "children": [], 

80 # "parents": [], 

81 # "references": [], 

82 # "related": [], 

83 # "superseded_by": [], 

84 # "supersedes": [], 

85 # "derived_from": [], 

86 # "derived": [], 

87 # } 

88 

89 # Return relationship data with proper permission filtering 

90 return ContentRelationshipDoc( 

91 content_id=content.id, 

92 content_title=content.title, 

93 relationships=ContentRelationships(), 

94 )