From b0c4d32b7741b0cd6b2d7640560c41b49d1ce6eb Mon Sep 17 00:00:00 2001 From: nickmwangemi Date: Wed, 4 Oct 2023 16:55:06 +0300 Subject: [PATCH 1/5] overturning --- peachjam/admin.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/peachjam/admin.py b/peachjam/admin.py index 3e62a247b..c6471cdb9 100644 --- a/peachjam/admin.py +++ b/peachjam/admin.py @@ -716,6 +716,14 @@ class JudgmentAdmin(ImportExportMixin, DocumentAdmin): fieldsets[2][1]["classes"] = ["collapse"] fieldsets[3][1]["fields"].extend(["case_summary", "flynote"]) + + fieldsets = fieldsets + [ + ( + gettext_lazy("Related judgments"), + {"fields": []}, + ), + ] + readonly_fields = [ "mnc", "serial_number", @@ -739,6 +747,7 @@ class JudgmentAdmin(ImportExportMixin, DocumentAdmin): "Document topics", "Work identification", "Advanced", + "Related judgments", ) class Media: From d8fefaa65faa8393ff932852ff78478dad67abb3 Mon Sep 17 00:00:00 2001 From: nickmwangemi Date: Mon, 30 Oct 2023 12:52:10 +0300 Subject: [PATCH 2/5] Overturned --- peachjam/admin.py | 26 ++++++++++++++++++++------ peachjam/forms.py | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/peachjam/admin.py b/peachjam/admin.py index c6471cdb9..cefe2a35c 100644 --- a/peachjam/admin.py +++ b/peachjam/admin.py @@ -28,6 +28,7 @@ AttachedFilesForm, IngestorForm, NewDocumentFormMixin, + RelatedJudgmentWidget, SourceFileForm, ) from peachjam.models import ( @@ -676,11 +677,29 @@ class BenchInline(admin.TabularInline): class JudgmentAdminForm(DocumentForm): hearing_date = forms.DateField(widget=DateSelectorWidget(), required=False) + related_judgments = forms.ChoiceField( + widget=RelatedJudgmentWidget(), + required=False, + ) class Meta: model = Judgment fields = ("hearing_date",) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields["related_judgments"] = forms.ChoiceField( + required=False, widget=RelatedJudgmentWidget() + ) + + @classmethod + def adjust_fieldsets(cls, fieldsets): + # add the upload_file to the first set of fields to include on the page + fieldsets = copy.deepcopy(fieldsets) + fieldsets[0][2]["fields"].append("related_judgments") + + return fieldsets + def save(self, *args, **kwargs): if ( "serial_number_override" in self.changed_data @@ -717,12 +736,7 @@ class JudgmentAdmin(ImportExportMixin, DocumentAdmin): fieldsets[2][1]["classes"] = ["collapse"] fieldsets[3][1]["fields"].extend(["case_summary", "flynote"]) - fieldsets = fieldsets + [ - ( - gettext_lazy("Related judgments"), - {"fields": []}, - ), - ] + fieldsets.append(("Related judgments", {"fields": ["related_judgments"]})) readonly_fields = [ "mnc", diff --git a/peachjam/forms.py b/peachjam/forms.py index 34fbc9083..2b016d20b 100644 --- a/peachjam/forms.py +++ b/peachjam/forms.py @@ -10,7 +10,14 @@ from django.utils.text import slugify from django.utils.translation import gettext as _ -from peachjam.models import AttachedFiles, CoreDocument, Ingestor, SourceFile +from peachjam.models import ( + AttachedFiles, + CoreDocument, + Ingestor, + Predicate, + SourceFile, + Work, +) from peachjam.plugins import plugins from peachjam.storage import clean_filename @@ -84,6 +91,37 @@ def adjust_fields(cls, fields): return [f for f in fields if f != "upload_file"] +class RelatedJudgmentWidget(forms.MultiWidget): + def __init__(self, attrs=None): + widgets = ( + forms.Select(attrs=attrs, choices=self.work_choices()), + forms.Select(attrs=attrs, choices=self.predicate_choices()), + ) + super().__init__(widgets, attrs) + + def decompress(self, value): + return (value.pk, value.title) if value else (None, None) + + # def format_output(self, rendered_widgets): + # return f""" + # + # """ + + def work_choices(self): + return [("", "---")] + [ + (work.pk, work.frbr_uri) + for work in Work.objects.filter(documents__doc_type="judgment").distinct() + ] + + def predicate_choices(self): + return [("", "---")] + [ + (p.pk, p.name) for p in Predicate.objects.filter(slug="overturned-by") + ] + + class BaseDocumentFilterForm(forms.Form): """This is the main form used for filtering Document ListViews, using facets such as year and alphabetical title. From 6564468f190a1a45e6adfd7899b093313dcebd89 Mon Sep 17 00:00:00 2001 From: nickmwangemi Date: Tue, 14 Nov 2023 12:19:50 +0300 Subject: [PATCH 3/5] Data migration to add overturned and upheld predicates --- ...09_add_overturned_and_upheld_predicates.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 peachjam/migrations/0109_add_overturned_and_upheld_predicates.py diff --git a/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py b/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py new file mode 100644 index 000000000..ac15d2691 --- /dev/null +++ b/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py @@ -0,0 +1,32 @@ +# Generated by Django 3.2.19 on 2023-11-14 09:13 + +from django.db import migrations + + +def add_overturned_and_upheld_predicates(apps, schema_editor): + """Add the predicates for overturned and upheld.""" + Predicate = apps.get_model("peachjam", "Predicate") + Predicate.objects.create( + name="overturns", + slug="overturns", + verb="overturns", + reverse_verb="is overturned by", + ) + Predicate.objects.create( + name="upholds", + slug="upholds", + verb="upholds", + reverse_verb="is upheld by", + ) + + +class Migration(migrations.Migration): + dependencies = [ + ("peachjam", "0108_update_ingestor_api_url"), + ] + + operations = [ + migrations.RunPython( + add_overturned_and_upheld_predicates, migrations.RunPython.noop + ), + ] From 7905bc5b6e70b6319157e0bddc20b01020cfac28 Mon Sep 17 00:00:00 2001 From: nickmwangemi Date: Fri, 17 Nov 2023 05:14:28 +0300 Subject: [PATCH 4/5] Tolerate case where predicate already exists --- .../migrations/0109_add_overturned_and_upheld_predicates.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py b/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py index ac15d2691..85f497e1a 100644 --- a/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py +++ b/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py @@ -6,13 +6,13 @@ def add_overturned_and_upheld_predicates(apps, schema_editor): """Add the predicates for overturned and upheld.""" Predicate = apps.get_model("peachjam", "Predicate") - Predicate.objects.create( + Predicate.objects.update_or_create( name="overturns", slug="overturns", verb="overturns", reverse_verb="is overturned by", ) - Predicate.objects.create( + Predicate.objects.update_or_create( name="upholds", slug="upholds", verb="upholds", From cbd2b22f7328878bf0a5afab005b5db070eef0f5 Mon Sep 17 00:00:00 2001 From: nickmwangemi Date: Fri, 17 Nov 2023 09:58:36 +0300 Subject: [PATCH 5/5] Add defaults to update_or_create --- ...09_add_overturned_and_upheld_predicates.py | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py b/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py index 85f497e1a..fcf13ce21 100644 --- a/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py +++ b/peachjam/migrations/0109_add_overturned_and_upheld_predicates.py @@ -6,17 +6,25 @@ def add_overturned_and_upheld_predicates(apps, schema_editor): """Add the predicates for overturned and upheld.""" Predicate = apps.get_model("peachjam", "Predicate") + + overturned_predicate = { + "name": "overturns", + "slug": "overturns", + "verb": "overturns", + "reverse_verb": "is overturned by", + } Predicate.objects.update_or_create( - name="overturns", - slug="overturns", - verb="overturns", - reverse_verb="is overturned by", + name=overturned_predicate["name"], defaults=overturned_predicate ) + + upheld_predicate = { + "name": "upholds", + "slug": "upholds", + "verb": "upholds", + "reverse_verb": "is upheld by", + } Predicate.objects.update_or_create( - name="upholds", - slug="upholds", - verb="upholds", - reverse_verb="is upheld by", + name=upheld_predicate["name"], defaults=upheld_predicate )