From 235766e690df08d33253c2e4897c6cf2ceb2e9ee Mon Sep 17 00:00:00 2001 From: Sushil Tiwari Date: Tue, 17 Sep 2024 14:48:12 +0545 Subject: [PATCH] Add transalation logic for summary in fieldreport and event --- api/admin.py | 1 + ..._event_title_ar_event_title_en_and_more.py | 63 +++++++++++++++++++ ...ort_title_fieldreport_title_ar_and_more.py | 38 ----------- api/models.py | 37 ++++++----- api/serializers.py | 2 +- api/translation.py | 2 +- 6 files changed, 86 insertions(+), 57 deletions(-) create mode 100644 api/migrations/0214_event_title_event_title_ar_event_title_en_and_more.py delete mode 100644 api/migrations/0214_fieldreport_title_fieldreport_title_ar_and_more.py diff --git a/api/admin.py b/api/admin.py index 9299ab1a5..af3677ff9 100644 --- a/api/admin.py +++ b/api/admin.py @@ -219,6 +219,7 @@ class EventAdmin(CompareVersionAdmin, RegionRestrictedAdmin, TranslationAdmin): "districts", "parent_event", ) + readonly_fields = ("name",) def appeals(self, instance): if getattr(instance, "appeals").exists(): diff --git a/api/migrations/0214_event_title_event_title_ar_event_title_en_and_more.py b/api/migrations/0214_event_title_event_title_ar_event_title_en_and_more.py new file mode 100644 index 000000000..4013e9353 --- /dev/null +++ b/api/migrations/0214_event_title_event_title_ar_event_title_en_and_more.py @@ -0,0 +1,63 @@ +# Generated by Django 4.2.16 on 2024-09-17 08:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("api", "0213_merge_20240807_1001"), + ] + + operations = [ + migrations.AddField( + model_name="event", + name="title", + field=models.CharField(blank=True, max_length=256), + ), + migrations.AddField( + model_name="event", + name="title_ar", + field=models.CharField(blank=True, max_length=256, null=True), + ), + migrations.AddField( + model_name="event", + name="title_en", + field=models.CharField(blank=True, max_length=256, null=True), + ), + migrations.AddField( + model_name="event", + name="title_es", + field=models.CharField(blank=True, max_length=256, null=True), + ), + migrations.AddField( + model_name="event", + name="title_fr", + field=models.CharField(blank=True, max_length=256, null=True), + ), + migrations.AddField( + model_name="fieldreport", + name="title", + field=models.CharField(blank=True, max_length=256), + ), + migrations.AddField( + model_name="fieldreport", + name="title_ar", + field=models.CharField(blank=True, max_length=256, null=True), + ), + migrations.AddField( + model_name="fieldreport", + name="title_en", + field=models.CharField(blank=True, max_length=256, null=True), + ), + migrations.AddField( + model_name="fieldreport", + name="title_es", + field=models.CharField(blank=True, max_length=256, null=True), + ), + migrations.AddField( + model_name="fieldreport", + name="title_fr", + field=models.CharField(blank=True, max_length=256, null=True), + ), + ] diff --git a/api/migrations/0214_fieldreport_title_fieldreport_title_ar_and_more.py b/api/migrations/0214_fieldreport_title_fieldreport_title_ar_and_more.py deleted file mode 100644 index 38925900b..000000000 --- a/api/migrations/0214_fieldreport_title_fieldreport_title_ar_and_more.py +++ /dev/null @@ -1,38 +0,0 @@ -# Generated by Django 4.2.15 on 2024-09-13 08:21 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('api', '0213_merge_20240807_1001'), - ] - - operations = [ - migrations.AddField( - model_name='fieldreport', - name='title', - field=models.CharField(blank=True, max_length=256), - ), - migrations.AddField( - model_name='fieldreport', - name='title_ar', - field=models.CharField(blank=True, max_length=256, null=True), - ), - migrations.AddField( - model_name='fieldreport', - name='title_en', - field=models.CharField(blank=True, max_length=256, null=True), - ), - migrations.AddField( - model_name='fieldreport', - name='title_es', - field=models.CharField(blank=True, max_length=256, null=True), - ), - migrations.AddField( - model_name='fieldreport', - name='title_fr', - field=models.CharField(blank=True, max_length=256, null=True), - ), - ] diff --git a/api/models.py b/api/models.py index 8fdd3f9aa..e5da1352c 100644 --- a/api/models.py +++ b/api/models.py @@ -19,9 +19,12 @@ # from django.db.models import Prefetch from django.dispatch import receiver from django.utils import timezone +from django.utils.translation import activate from django.utils.translation import gettext_lazy as _ from tinymce.models import HTMLField +from lang.translation import AVAILABLE_LANGUAGES + from .utils import validate_slug_number # is_user_ifrc, @@ -750,7 +753,7 @@ class Event(models.Model): ) image = models.ImageField(verbose_name=_("image"), null=True, blank=True, upload_to=snippet_image_path) summary = HTMLField(verbose_name=_("summary"), blank=True, default="") - # title = models.CharField(max_length=256, blank=True) + title = models.CharField(max_length=256, blank=True) num_injured = models.IntegerField(verbose_name=_("number of injured"), null=True, blank=True) num_dead = models.IntegerField(verbose_name=_("number of dead"), null=True, blank=True) @@ -845,6 +848,13 @@ def record_type(self): def to_dict(self): return to_dict(self) + def generate_formatted_name(self): + for transalation in AVAILABLE_LANGUAGES: + activate(transalation) + country = self.countries.first() + start_date = self.start_date.strftime("%m-%Y") + self.name = f"{country.iso3}: {self.dtype.name} - {start_date} - {self.title}" + def save(self, *args, **kwargs): # Make the slug lowercase @@ -855,6 +865,8 @@ def save(self, *args, **kwargs): if not self.id and not self.disaster_start_date: self.disaster_start_date = timezone.now() + self.generate_formatted_name() + return super(Event, self).save(*args, **kwargs) def __str__(self): @@ -1648,22 +1660,13 @@ class Meta: # filters = models.Q() def generate_formatted_summary(self) -> str: - translations = { - "summary_en": self.title_en, - "summary_fr": self.title_fr, - "summary_es": self.title_es, - "summary_ar": self.title_ar, - } - country = self.countries.first() - disater = self.dtype - start_date = self.start_date.strftime("%m-%Y") - - field_report_number = FieldReport.objects.filter(countries=country).count() - date = timezone.now().strftime("%Y-%m-%d") - for summary_field, title in translations.items(): - if title: - summary = f"{country.iso3}: {disater.name} - {start_date} {title} #{field_report_number} ({date})" - setattr(self, summary_field, summary) + for transalation in AVAILABLE_LANGUAGES: + activate(transalation) + country = self.countries.first() + start_date = self.start_date.strftime("%m-%Y") + field_report_number = FieldReport.objects.filter(countries=country).count() + 1 + date = timezone.now().strftime("%Y-%m-%d") + self.summary = f"{country.iso3}: {self.dtype.name} - {start_date} {self.title} #{field_report_number} ({date})" def save(self, *args, **kwargs): # On save, is report_date or start_date is not set, set it to now. diff --git a/api/serializers.py b/api/serializers.py index 9d2203ddd..d471d196c 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -2024,7 +2024,7 @@ class Meta: def create_event(self, report): event = Event.objects.create( - name=report.title, + title=report.title, dtype=report.dtype, summary=report.description or "", disaster_start_date=report.start_date, diff --git a/api/translation.py b/api/translation.py index 46ffc0d38..a68847800 100644 --- a/api/translation.py +++ b/api/translation.py @@ -78,7 +78,7 @@ class DisasterTypeTO(TranslationOptions): @register(Event) class EventTO(TranslationOptions): - fields = ("name", "summary") + fields = ("name", "summary", "title") skip_fields = ("name",) # XXX: CUSTOM field Not used by TranslationOptions, but used in lang/tasks.py