Skip to content

Commit

Permalink
Add title and skip summary translation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
susilnem committed Sep 17, 2024
1 parent 74b6305 commit f860a44
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class FieldReportAdmin(CompareVersionAdmin, RegionRestrictedAdmin, TranslationAd
"districts",
)

readonly_fields = ("report_date", "created_at", "updated_at")
readonly_fields = ("report_date", "created_at", "updated_at", "summary")
list_filter = [MembershipFilter]
actions = [
"create_events",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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),
),
]
25 changes: 24 additions & 1 deletion api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,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)

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 @@ -1436,6 +1437,8 @@ class RecentAffected(models.IntegerChoices):
# Used to differentiate reports that have and have not been synced from DMIS
rid = models.CharField(verbose_name=_("r id"), max_length=100, null=True, blank=True, editable=False)
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)
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 @@ -1643,14 +1646,34 @@ class Meta:
# filters = models.Q(visibility__in=[VisibilityChoices.MEMBERSHIP, VisibilityChoices.PUBLIC])
# if is_user_ifrc(user):
# filters = models.Q()
# return FieldReport.objects.filter(filters)

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)

def save(self, *args, **kwargs):
# On save, is report_date or start_date is not set, set it to now.
if not self.id and not self.report_date:
self.report_date = timezone.now()
if not self.id and not self.start_date:
self.start_date = timezone.now()
# NOTE: Overriding the summary field with translated title
self.generate_formatted_summary()

return super(FieldReport, self).save(*args, **kwargs)

def indexing(self):
Expand Down
3 changes: 2 additions & 1 deletion api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2020,10 +2020,11 @@ class FieldReportSerializer(
class Meta:
model = FieldReport
fields = "__all__"
read_only_fields = ("summary",)

def create_event(self, report):
event = Event.objects.create(
name=report.summary,
name=report.title,
dtype=report.dtype,
summary=report.description or "",
disaster_start_date=report.start_date,
Expand Down
4 changes: 3 additions & 1 deletion api/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class DisasterTypeTO(TranslationOptions):
@register(Event)
class EventTO(TranslationOptions):
fields = ("name", "summary")
skip_fields = ("name",) # XXX: CUSTOM field Not used by TranslationOptions, but used in lang/tasks.py


@register(ExternalPartner)
Expand All @@ -88,7 +89,8 @@ class ExternalPartnerTO(TranslationOptions):

@register(FieldReport)
class FieldReportTO(TranslationOptions):
fields = ("summary", "description", "actions_others", "other_sources")
fields = ("title", "summary", "description", "actions_others", "other_sources")
skip_fields = ("summary",) # XXX: CUSTOM field Not used by TranslationOptions, but used in lang/tasks.py


@register(GDACSEvent)
Expand Down
5 changes: 4 additions & 1 deletion lang/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ def get_translatable_models(cls, only_models: typing.Optional[typing.List[models

@classmethod
def get_translatable_fields(cls, model):
return list(translator.get_options_for_model(model).fields.keys())
translation_options = translator.get_options_for_model(model)
# NOTE: Some skipped fields are handled manually.
skipped_fields = set(getattr(translation_options, "skip_fields", []))
return [field for field in translation_options.fields.keys() if field not in skipped_fields]

def translate_model_fields(self, obj, translatable_fields=None):
if skip_auto_translation(obj):
Expand Down

0 comments on commit f860a44

Please sign in to comment.