Skip to content

Commit

Permalink
Add labels for documents
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmwangemi committed Jul 27, 2023
1 parent fd19816 commit d127c72
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 5 deletions.
7 changes: 7 additions & 0 deletions peachjam/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
Journal,
Judge,
Judgment,
Label,
LegalInstrument,
Legislation,
Locality,
Expand Down Expand Up @@ -854,6 +855,12 @@ class UserAdminCustom(ImportExportMixin, UserAdmin):
resource_class = UserResource


@admin.register(Label)
class LabelAdmin(admin.ModelAdmin):
list_display = ("name", "code")
prepopulated_fields = {"code": ("name",)}


admin.site.register(
[
Locality,
Expand Down
42 changes: 42 additions & 0 deletions peachjam/migrations/0093_add_model_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 3.2.19 on 2023-07-25 14:19

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("peachjam", "0092_documentcontent_content_xml"),
]

operations = [
migrations.CreateModel(
name="Label",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"name",
models.CharField(max_length=1024, unique=True, verbose_name="name"),
),
(
"code",
models.SlugField(max_length=1024, unique=True, verbose_name="code"),
),
],
),
migrations.AddField(
model_name="coredocument",
name="labels",
field=models.ManyToManyField(
blank=True, to="peachjam.Label", verbose_name="labels"
),
),
]
33 changes: 28 additions & 5 deletions peachjam/models/core_document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@


class Label(models.Model):
name = models.CharField(_("name"), max_length=255, unique=True)
code = models.SlugField(_("code"), max_length=255, unique=True)
name = models.CharField(
_("name"), max_length=1024, unique=True, null=False, blank=False
)
code = models.SlugField(_("code"), max_length=1024, unique=True)

def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
if not self.code:
self.code = slugify(self.name)
return super().save(*args, **kwargs)

def __str__(self):
return f"{self.name}"


class DocumentNature(models.Model):
name = models.CharField(
Expand Down Expand Up @@ -407,7 +412,7 @@ class CoreDocument(PolymorphicModel):

# options for the FRBR URI doctypes
frbr_uri_doctypes = FRBR_URI_DOCTYPES
labels = models.ManyToManyField(Label, verbose_name=_("labels"))
labels = models.ManyToManyField(Label, verbose_name=_("labels"), blank=True)

class Meta:
ordering = ["doc_type", "title"]
Expand All @@ -419,6 +424,21 @@ class Meta:
def __str__(self):
return f"{self.doc_type} - {self.title}"

def apply_labels(self):
"""Apply labels to this document based on its properties."""
# label showing that a document is cited/reported in law reports, hence "more important"
label, _ = Label.objects.get_or_create(
code="reported",
defaults={"name": "Reported", "code": "reported"},
)

# if the document has alternative_names, apply the "reported" label
if self.alternative_names.exists():
self.labels.add(label.pk)
# if the document no alternative_names, remove the "reported" label
else:
self.labels.remove(label.pk)

def get_all_fields(self):
return self._meta.get_fields()

Expand Down Expand Up @@ -516,6 +536,9 @@ def pre_save(self):
self.work.title = self.title
self.work.save()

# apply labels
self.apply_labels()

def save(self, *args, **kwargs):
# give ourselves and subclasses a chance to pre-populate derived fields before saving, in case full_clean() has
# not yet been called
Expand Down
23 changes: 23 additions & 0 deletions peachjam/models/generic_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CoreDocument,
CoreDocumentManager,
CoreDocumentQuerySet,
Label,
Work,
)
from peachjam.models.author import Author
Expand Down Expand Up @@ -85,6 +86,28 @@ class Meta(CoreDocument.Meta):
def __str__(self):
return self.title

def apply_labels(self):
# label to indicate that this legislation is repealed
label, _ = Label.objects.get_or_create(
code="repealed", defaults={"name": "Repealed", "code": "repealed"}
)
# apply label if repealed
if self.metadata_json.get("repeal"):
self.repealed = True
self.labels.add(label.pk)

# remove label if not repealed
else:
self.repealed = False
self.labels.remove(label.pk)
self.save()

super().apply_labels()

def pre_save(self):
self.doc_type = "legislation"
return super().pre_save()

def save(self, *args, **kwargs):
self.apply_labels()
super().save(*args, **kwargs)
18 changes: 18 additions & 0 deletions peachjam/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,21 @@ def doc_deleted_update_extracted_citations(sender, instance, **kwargs):
"""Update language list on related work after a subclass of CoreDocument is deleted."""
if isinstance(instance, CoreDocument):
update_extracted_citations_for_a_work(instance.work_id)


@receiver(signals.post_save)
def doc_saved_apply_labels(sender, instance, **kwargs):
"""Apply labels when an instance of a CoreDocument is saved."""
if (
isinstance(instance, CoreDocument)
and instance.doc_type == "legislation"
and not kwargs["raw"]
):
instance.apply_labels()

if (
isinstance(instance, CoreDocument)
and not kwargs["raw"]
and instance.alternative_names.all()
):
instance.apply_labels()
8 changes: 8 additions & 0 deletions peachjam/templates/peachjam/_labels.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="d-flex align-items-center">
{% if labels %}
{% for label in labels %}
{% if label.code == "repealed" %}<span class="badge rounded-pill bg-danger">{{ label.name }}</span>{% endif %}
{% if label.code == "reported" %}<span class="badge rounded-pill bg-success">{{ label.name }}</span>{% endif %}
{% endfor %}
{% endif %}
</div>
1 change: 1 addition & 0 deletions peachjam/templates/peachjam/layouts/document_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
{% block document-title %}
<div class="d-md-flex justify-content-md-between py-4">
<h1>{{ document.title }}</h1>
{% include 'peachjam/_labels.html' %}
<div class="d-flex align-items-center">
<a href="https://api.whatsapp.com/send?text={{ request.build_absolute_uri }}"
class="btn btn-link"
Expand Down
1 change: 1 addition & 0 deletions peachjam/views/generic_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def get_context_data(self, **kwargs):
context["cited_documents_count"]
+ context["documents_citing_current_doc_count"]
)
context["labels"] = doc.labels.all()

return context

Expand Down

0 comments on commit d127c72

Please sign in to comment.