diff --git a/api/admin.py b/api/admin.py index f25b7abad..027a336ff 100644 --- a/api/admin.py +++ b/api/admin.py @@ -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", diff --git a/api/migrations/0215_event_title_event_title_ar_event_title_en_and_more.py b/api/migrations/0215_event_title_event_title_ar_event_title_en_and_more.py index 686006441..4e0d326fe 100644 --- a/api/migrations/0215_event_title_event_title_ar_event_title_en_and_more.py +++ b/api/migrations/0215_event_title_event_title_ar_event_title_en_and_more.py @@ -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 @@ -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", diff --git a/api/models.py b/api/models.py index 956c52a63..0407381ed 100644 --- a/api/models.py +++ b/api/models.py @@ -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( @@ -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)