Coverage for app / services / knowledge / recommendations / dismissals.py: 91%

33 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1"""Setting a recommendation aside, for a workspace, while its evidence holds 

2(``KnowledgeRecommendationDismissal``, migration 114). 

3 

4A dismissal names a kind, a subject and the fingerprint the evidence read. 

5The same recommendation with a new fingerprint — the evidence has moved on 

6— shows again. A source map suggestion is set aside the same way 

7(:data:`MAP_SUGGESTION`). 

8""" 

9 

10from __future__ import annotations 

11 

12from typing import Any 

13from uuid import UUID 

14 

15from sqlalchemy.orm import Session 

16 

17from app.db.postgres.models import KnowledgeRecommendationDismissal, User 

18from app.services.knowledge.recommendations.base import kinds 

19from app.util.time import naive_utcnow 

20 

21#: A source map suggestion: the repository and the source, ``a|b``. 

22MAP_SUGGESTION = "map_suggestion" 

23 

24SUBJECT_LENGTH = 200 

25FINGERPRINT_LENGTH = 64 

26 

27 

28def dismissable() -> frozenset[str]: 

29 """The kinds a workspace may set aside.""" 

30 return frozenset({k.key for k in kinds()} | {MAP_SUGGESTION}) 

31 

32 

33def active(db: Session, ws: Any) -> dict[tuple[str, str], str]: 

34 """``(kind, subject) → fingerprint`` for what ``ws`` has set aside.""" 

35 return { 

36 (row.kind, row.subject): row.fingerprint 

37 for row in db.query( 

38 KnowledgeRecommendationDismissal.kind, 

39 KnowledgeRecommendationDismissal.subject, 

40 KnowledgeRecommendationDismissal.fingerprint, 

41 ) 

42 .filter(KnowledgeRecommendationDismissal.workspace_key == ws.key) 

43 .all() 

44 } 

45 

46 

47def dismiss( 

48 db: Session, 

49 ws: Any, 

50 actor: User, 

51 *, 

52 kind: str, 

53 subject: str, 

54 fingerprint: str, 

55) -> KnowledgeRecommendationDismissal: 

56 """Set ``kind`` about ``subject`` aside in ``ws`` while its evidence 

57 reads ``fingerprint``, replacing an earlier dismissal of it. Commits.""" 

58 row = ( 

59 db.query(KnowledgeRecommendationDismissal) 

60 .filter( 

61 KnowledgeRecommendationDismissal.workspace_key == ws.key, 

62 KnowledgeRecommendationDismissal.kind == kind, 

63 KnowledgeRecommendationDismissal.subject == subject, 

64 ) 

65 .first() 

66 ) 

67 if row is None: 

68 row = KnowledgeRecommendationDismissal( 

69 workspace_key=ws.key, kind=kind, subject=subject 

70 ) 

71 db.add(row) 

72 row.fingerprint = fingerprint 

73 row.dismissed_by_user_id = actor.id 

74 row.dismissed_at = naive_utcnow() 

75 db.commit() 

76 db.refresh(row) 

77 return row 

78 

79 

80def undo(db: Session, ws: Any, dismissal_id: UUID) -> bool: 

81 """Bring a dismissal of ``ws``'s back. Commits; False when it is not 

82 this workspace's (or is gone).""" 

83 row = db.get(KnowledgeRecommendationDismissal, dismissal_id) 

84 if row is None or row.workspace_key != ws.key: 

85 return False 

86 db.delete(row) 

87 db.commit() 

88 return True 

89 

90 

91__all__ = [ 

92 "FINGERPRINT_LENGTH", 

93 "MAP_SUGGESTION", 

94 "SUBJECT_LENGTH", 

95 "active", 

96 "dismiss", 

97 "dismissable", 

98 "undo", 

99]