Skip to content

Commit

Permalink
Add fieldreport number logic and suffic for covid
Browse files Browse the repository at this point in the history
  • Loading branch information
susilnem committed Nov 22, 2024
1 parent 514e4a3 commit 2dc83a4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class FieldReportAdmin(CompareVersionAdmin, RegionRestrictedAdmin, TranslationAd
"districts",
)

readonly_fields = ("report_date", "created_at", "updated_at", "summary")
readonly_fields = ("report_date", "created_at", "updated_at", "summary", "fr_num")
list_filter = [MembershipFilter]
actions = [
"create_events",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.16 on 2024-10-07 18:46
# Generated by Django 4.2.16 on 2024-11-20 09:20

from django.db import migrations, models

Expand Down Expand Up @@ -35,6 +35,11 @@ class Migration(migrations.Migration):
name="title_fr",
field=models.CharField(blank=True, max_length=256, null=True),
),
migrations.AddField(
model_name="fieldreport",
name="fr_num",
field=models.IntegerField(blank=True, null=True, verbose_name="field report number"),
),
migrations.AddField(
model_name="fieldreport",
name="title",
Expand Down
28 changes: 23 additions & 5 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,7 @@ class RecentAffected(models.IntegerChoices):
summary = models.TextField(verbose_name=_("summary"), blank=True)
# Title field is used for the translation and later adding formated into the summary
title = models.CharField(max_length=256, blank=True)
fr_num = models.IntegerField(verbose_name=_("field report number"), null=True, blank=True)
description = HTMLField(verbose_name=_("description"), blank=True, default="")
dtype = models.ForeignKey(DisasterType, verbose_name=_("disaster type"), on_delete=models.PROTECT)
event = models.ForeignKey(
Expand Down Expand Up @@ -1674,17 +1675,34 @@ def generate_formatted_summary(self):
country_iso3 = self.countries.first().iso3 if self.id and self.countries.first() else "N/A"
dtype = self.dtype.name if self.dtype else "N/A"
start_date = self.start_date.strftime("%m-%Y")
field_report_number = FieldReport.objects.filter(countries__iso3=country_iso3).exclude(id=self.id).count() + 1
current_date = timezone.now().strftime("%Y-%m-%d")

if self.fr_num is None and self.event and self.id:
current_fr_number = (
FieldReport.objects.filter(event=self.event, countries__iso3=country_iso3).aggregate(
max_fr_num=models.Max("fr_num")
)["max_fr_num"]
or 0
)
field_report_number = current_fr_number + 1
self.fr_num = field_report_number
# NOTE: Updating the updated_fields when the fr_num is updated by translation tasks
yield "fr_num"

# NOTE: Report number is set to None if the report is not associated with an event
if self.id and not self.event:
self.fr_num = None

suffix = ""
if self.fr_num and self.fr_num > 1:
suffix = f"#{self.fr_num} ({current_date})"

for lang in AVAILABLE_LANGUAGES:
activate(lang)
if self.is_covid_report:
# {ISO3}: COVID-19 #{Field Report Number} ({Date})
self.summary = f"{country_iso3}: COVID-19 #{field_report_number} ({current_date})"
self.summary = f"{country_iso3}: COVID-19 {suffix}"
else:
# {ISO3}: {Disaster Type} - {Start Date} #{Field Report Number} ({Date})
self.summary = f"{country_iso3}: {dtype} - {start_date} {self.title} #{field_report_number} ({current_date})"
self.summary = f"{country_iso3}: {dtype} - {start_date} - {self.title} {suffix}"
deactivate()
yield build_localized_fieldname("summary", lang)

Expand Down

0 comments on commit 2dc83a4

Please sign in to comment.