1+ from collections import defaultdict
12from functools import cached_property
23
34from django .shortcuts import get_object_or_404
4- from django .views .generic import ListView
5-
6- from peachjam .helpers import chunks
7- from peachjam .models import LawReport , LawReportVolume
5+ from django .views .generic import DetailView , ListView
6+
7+ from peachjam .models import (
8+ ExtractedCitation ,
9+ Judgment ,
10+ LawReport ,
11+ LawReportVolume ,
12+ Legislation ,
13+ )
814from peachjam .views .courts import FilteredJudgmentView
915
1016
@@ -14,41 +20,33 @@ class LawReportListView(ListView):
1420 context_object_name = "law_reports"
1521
1622
17- class LawReportDetailView (FilteredJudgmentView ):
23+ class LawReportDetailView (DetailView ):
1824 template_name = "peachjam/law_report/law_report_detail.html"
25+ model = LawReport
26+ context_object_name = "law_report"
1927 navbar_link = "law_report"
2028
21- @cached_property
22- def law_report (self ):
23- return get_object_or_404 (
24- LawReport .objects .prefetch_related ("volumes" ), slug = self .kwargs .get ("slug" )
25- )
26-
27- def base_view_name (self ):
28- return self .law_report .title
29-
30- def get_base_queryset (self , exclude = None ):
31- qs = super ().get_base_queryset (exclude = exclude )
32- qs = qs .filter (
33- law_report_entries__law_report_volume__law_report = self .law_report
34- )
35- return qs
36-
3729 def get_context_data (self , ** kwargs ):
3830 context = super ().get_context_data (** kwargs )
39- context ["law_report" ] = self .law_report
40- context ["law_report_volumes" ] = self .law_report .volumes .exclude (
31+ context ["law_report_volumes" ] = self .object .volumes .exclude (
4132 law_report_entries__isnull = True
42- )
43- context ["law_report_volume_groups" ] = chunks (context ["law_report_volumes" ], 3 )
44- context ["entity_profile" ] = self .law_report .entity_profile .first ()
45- context ["entity_profile_title" ] = self .law_report .title
46- context ["hide_follow_button" ] = True
33+ ).order_by ("-title" )
34+ context ["entity_profile" ] = self .object .entity_profile .first ()
35+ context ["entity_profile_title" ] = self .object .title
4736 return context
4837
4938
50- class LawReportVolumeDetailView (LawReportDetailView ):
39+ class LawReportVolumeDetailView (FilteredJudgmentView ):
5140 template_name = "peachjam/law_report/law_report_volume_detail.html"
41+ navbar_link = "law_report"
42+
43+ CITATION_TABS = {"cases" : Judgment , "legislation" : Legislation }
44+
45+ @cached_property
46+ def law_report (self ):
47+ return get_object_or_404 (
48+ LawReport .objects .prefetch_related ("volumes" ), slug = self .kwargs .get ("slug" )
49+ )
5250
5351 def base_view_name (self ):
5452 return self .law_report_volume .title
@@ -61,12 +59,87 @@ def law_report_volume(self):
6159 slug = self .kwargs .get ("volume_slug" ),
6260 )
6361
62+ @cached_property
63+ def active_tab (self ):
64+ tab = self .request .GET .get ("tab" )
65+ return tab if tab in self .CITATION_TABS else "judgments"
66+
6467 def get_base_queryset (self , exclude = None ):
68+ if self .active_tab in self .CITATION_TABS :
69+ model = self .CITATION_TABS [self .active_tab ]
70+ volume_lookup = (
71+ "work__incoming_citations__citing_work__documents"
72+ "__judgment__law_report_entries__law_report_volume"
73+ )
74+ return (
75+ model .objects .filter (
76+ ** {volume_lookup : self .law_report_volume },
77+ published = True ,
78+ )
79+ .distinct ()
80+ .order_by ("title" )
81+ )
6582 qs = super ().get_base_queryset (exclude = exclude )
66- qs = qs .filter (law_report_entries__law_report_volume = self .law_report_volume )
67- return qs
83+ return qs .filter (law_report_entries__law_report_volume = self .law_report_volume )
6884
6985 def get_context_data (self , ** kwargs ):
7086 context = super ().get_context_data (** kwargs )
87+ context ["law_report" ] = self .law_report
7188 context ["law_report_volume" ] = self .law_report_volume
89+ context ["active_tab" ] = self .active_tab
90+
91+ if self .active_tab in self .CITATION_TABS :
92+ context ["doc_table_toggle" ] = True
93+ self ._attach_citing_judgments (context .get ("documents" , []))
94+
7295 return context
96+
97+ def add_facets (self , context ):
98+ if self .active_tab in self .CITATION_TABS :
99+ context ["facet_data" ] = {}
100+ self .add_alphabet_facet (context )
101+ else :
102+ super ().add_facets (context )
103+
104+ def _attach_citing_judgments (self , cited_docs ):
105+ """Attach citing judgments from this volume as .children for toggle."""
106+ work_ids = [d .work_id for d in cited_docs if hasattr (d , "work_id" )]
107+ if not work_ids :
108+ return
109+
110+ citations = (
111+ ExtractedCitation .objects .filter (
112+ citing_work__documents__judgment__law_report_entries__law_report_volume = self .law_report_volume ,
113+ target_work_id__in = work_ids ,
114+ )
115+ .values_list ("target_work_id" , "citing_work_id" )
116+ .distinct ()
117+ )
118+
119+ citing_map = defaultdict (set )
120+ for target_wid , citing_wid in citations :
121+ citing_map [target_wid ].add (citing_wid )
122+
123+ if not citing_map :
124+ return
125+
126+ all_citing_wids = set ().union (* citing_map .values ())
127+ citing_docs = {
128+ j .work_id : j
129+ for j in Judgment .objects .filter (
130+ work_id__in = all_citing_wids , published = True
131+ )
132+ .distinct ("work_frbr_uri" )
133+ .order_by ("work_frbr_uri" , "-date" )
134+ }
135+
136+ for doc in cited_docs :
137+ if hasattr (doc , "work_id" ):
138+ doc .children = sorted (
139+ [
140+ citing_docs [wid ]
141+ for wid in citing_map .get (doc .work_id , [])
142+ if wid in citing_docs
143+ ],
144+ key = lambda d : d .title ,
145+ )
0 commit comments