diff --git a/peachjam/admin.py b/peachjam/admin.py index 5de38e01a..d8f596ac8 100644 --- a/peachjam/admin.py +++ b/peachjam/admin.py @@ -38,6 +38,7 @@ Author, Bench, Book, + CaseHistory, CaseNumber, CitationLink, CitationProcessing, @@ -711,6 +712,26 @@ class CaseNumberAdmin(admin.StackedInline): fields = ["matter_type", "number", "year", "string_override"] +class CaseHistoryInlineAdmin(admin.StackedInline): + model = CaseHistory + extra = 1 + verbose_name = gettext_lazy("case history") + verbose_name_plural = gettext_lazy("case history") + fk_name = "judgment" + + def get_formset(self, request, obj=None, **kwargs): + return super().get_formset( + request, + obj, + widgets={ + "historical_judgment": autocomplete.ModelSelect2( + url="autocomplete-judgments" + ) + }, + **kwargs, + ) + + class BenchInline(admin.TabularInline): # by using an inline, the ordering of the judges is preserved model = Bench @@ -776,6 +797,7 @@ class JudgmentAdmin(ImportExportMixin, DocumentAdmin): BenchInline, LowerBenchInline, CaseNumberAdmin, + CaseHistoryInlineAdmin, JudgmentRelationshipStackedInline, ] + DocumentAdmin.inlines filter_horizontal = ("judges", "attorneys", "outcomes") diff --git a/peachjam/migrations/0136_casehistory.py b/peachjam/migrations/0136_casehistory.py new file mode 100644 index 000000000..c0019a526 --- /dev/null +++ b/peachjam/migrations/0136_casehistory.py @@ -0,0 +1,88 @@ +# Generated by Django 3.2.21 on 2024-05-30 13:13 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("peachjam", "0135_backfill_citation_counts3"), + ] + + operations = [ + migrations.CreateModel( + name="CaseHistory", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "case_number", + models.CharField( + blank=True, + max_length=1024, + null=True, + verbose_name="case number", + ), + ), + ("date", models.DateField(blank=True, null=True, verbose_name="date")), + ( + "court", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="peachjam.court", + verbose_name="court", + ), + ), + ( + "historical_judgment", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="peachjam.judgment", + verbose_name="historical judgment", + ), + ), + ( + "judges", + models.ManyToManyField( + blank=True, to="peachjam.Judge", verbose_name="judges" + ), + ), + ( + "judgment", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="case_histories", + to="peachjam.judgment", + verbose_name="judgment", + ), + ), + ( + "outcome", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="peachjam.outcome", + verbose_name="outcome", + ), + ), + ], + options={ + "verbose_name": "case history", + "verbose_name_plural": "case histories", + "ordering": ["date"], + }, + ), + ] diff --git a/peachjam/models/judgment.py b/peachjam/models/judgment.py index 74ef48c3d..1b400edec 100644 --- a/peachjam/models/judgment.py +++ b/peachjam/models/judgment.py @@ -486,3 +486,42 @@ def get_case_number_string(self): def save(self, *args, **kwargs): self.string = self.get_case_number_string() return super().save(*args, **kwargs) + + +class CaseHistory(models.Model): + judgment = models.ForeignKey( + Judgment, + related_name="case_histories", + on_delete=models.CASCADE, + verbose_name=_("judgment"), + ) + case_number = models.CharField( + _("case number"), max_length=1024, null=True, blank=True + ) + historical_judgment = models.ForeignKey( + Judgment, + on_delete=models.PROTECT, + null=True, + blank=True, + verbose_name=_("historical judgment"), + ) + outcome = models.ForeignKey( + Outcome, + on_delete=models.PROTECT, + null=True, + blank=True, + verbose_name=_("outcome"), + ) + judges = models.ManyToManyField(Judge, verbose_name=_("judges"), blank=True) + court = models.ForeignKey( + Court, on_delete=models.PROTECT, null=True, blank=True, verbose_name=_("court") + ) + date = models.DateField(_("date"), null=True, blank=True) + + class Meta: + ordering = ["date"] + verbose_name = _("case history") + verbose_name_plural = _("case histories") + + def __str__(self): + return f"{self.case_number}" or f"{self.judgment} - {self.date}" diff --git a/peachjam/urls.py b/peachjam/urls.py index 677027791..0430c9e3b 100644 --- a/peachjam/urls.py +++ b/peachjam/urls.py @@ -57,6 +57,7 @@ GazetteYearView, HomePageView, JournalListView, + JudgmentAutocomplete, JudgmentListView, LegalInstrumentListView, LegislationListView, @@ -227,6 +228,11 @@ WorkAutocomplete.as_view(), name="autocomplete-works", ), + path( + "admin/autocomplete/judgments", + JudgmentAutocomplete.as_view(), + name="autocomplete-judgments", + ), path("admin/", admin.site.urls), path("accounts/", include("allauth.urls")), path("api/", include("peachjam_api.urls")), diff --git a/peachjam/views/autocomplete.py b/peachjam/views/autocomplete.py index aab944165..c34387169 100644 --- a/peachjam/views/autocomplete.py +++ b/peachjam/views/autocomplete.py @@ -1,6 +1,6 @@ from dal import autocomplete -from peachjam.models import Work +from peachjam.models import Judgment, Work class WorkAutocomplete(autocomplete.Select2QuerySetView): @@ -14,3 +14,15 @@ def get_queryset(self): qs = qs.filter(title__istartswith=self.q) return qs + + +class JudgmentAutocomplete(autocomplete.Select2QuerySetView): + def get_queryset(self): + if not self.request.user.is_staff: + return Judgment.objects.none() + + qs = Judgment.objects.all() + if self.q: + qs = qs.filter(title__istartswith=self.q) + + return qs