Skip to content

Commit

Permalink
Add transalation logic for summary in fieldreport and event
Browse files Browse the repository at this point in the history
  • Loading branch information
susilnem committed Sep 17, 2024
1 parent f860a44 commit 235766e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 57 deletions.
1 change: 1 addition & 0 deletions api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class EventAdmin(CompareVersionAdmin, RegionRestrictedAdmin, TranslationAdmin):
"districts",
"parent_event",
)
readonly_fields = ("name",)

def appeals(self, instance):
if getattr(instance, "appeals").exists():
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
]

This file was deleted.

37 changes: 20 additions & 17 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion api/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 235766e

Please sign in to comment.