Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dref/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class DrefAdmin(CompareVersionAdmin, TranslationAdmin, admin.ModelAdmin):
"risk_security",
"proposed_action",
)
readonly_fields = ("original_language",)

def get_queryset(self, request):
return (
Expand Down Expand Up @@ -143,6 +144,7 @@ class DrefOperationalUpdateAdmin(CompareVersionAdmin, TranslationAdmin, admin.Mo
"district",
"risk_security",
)
readonly_fields = ("original_language",)
list_filter = ["dref"]

def get_queryset(self, request):
Expand Down Expand Up @@ -199,6 +201,7 @@ class DrefFinalReportAdmin(CompareVersionAdmin, TranslationAdmin, admin.ModelAdm
"national_society_actions",
"source_information",
)
readonly_fields = ("original_language",)
list_filter = ["dref"]
search_fields = ["title", "national_society__name", "appeal_code"]

Expand Down
14 changes: 13 additions & 1 deletion dref/migrations/0085_remove_dref_is_published_and_more.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason on why we are modifying existing migration file?
How we are planning to run this in local and already deployed instances

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

from django.db import migrations


def migrate_approved_status_to_new_value(apps, schema_editor):
"""
Migrates the 'APPROVED' status from old value (4) to new value (3)
"""
models = [
"Dref",
"DrefOperationalUpdate",
"DrefFinalReport",
]
for model_name in models:
Model = apps.get_model("dref", model_name)
Model.objects.filter(status=4).update(status=3)
class Migration(migrations.Migration):

dependencies = [
Expand All @@ -22,4 +33,5 @@ class Migration(migrations.Migration):
model_name="drefoperationalupdate",
name="is_published",
),
migrations.RunPython(migrate_approved_status_to_new_value, reverse_code=migrations.RunPython.noop),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.2.19 on 2025-10-11 11:56

from django.db import migrations, models


def update_original_language(apps, schema_editor):
"""
Populate the original_language field with 'en' for all existing records.
"""
models = [
"Dref",
"DrefOperationalUpdate",
"DrefFinalReport",
]
for model_name in models:
Model = apps.get_model("dref", model_name)
Model.objects.filter(original_language__isnull=True).update(original_language="en")


class Migration(migrations.Migration):

dependencies = [
('dref', '0085_remove_dref_is_published_and_more'),
]

operations = [
migrations.AlterField(
model_name='dref',
name='status',
field=models.IntegerField(choices=[(1, 'Draft'), (2, 'Finalized'), (3, 'Approved')], default=1, verbose_name='status'),
),
migrations.AlterField(
model_name='dreffinalreport',
name='status',
field=models.IntegerField(choices=[(1, 'Draft'), (2, 'Finalized'), (3, 'Approved')], default=1, verbose_name='status'),
),
migrations.AlterField(
model_name='drefoperationalupdate',
name='status',
field=models.IntegerField(choices=[(1, 'Draft'), (2, 'Finalized'), (3, 'Approved')], default=1, verbose_name='status'),
),
migrations.RunPython(update_original_language,reverse_code=migrations.RunPython.noop)
]
6 changes: 2 additions & 4 deletions dref/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,9 @@ class DisasterCategory(models.IntegerChoices):
class Status(models.IntegerChoices):
DRAFT = 1, _("Draft")
"""Draft: Initial stage content is being created and is not ready for review."""
FINALIZING = 2, _("Finalizing")
"""Finalizing: Content is in the translation process from the original language into English."""
FINALIZED = 3, _("Finalized")
FINALIZED = 2, _("Finalized")
"""Finalized: Translation is completed, content is ready for review, and updates to the original language are locked."""
APPROVED = 4, _("Approved")
APPROVED = 3, _("Approved")
"""Approved: The content has been reviewed, accepted, and is ready for use."""

created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
Expand Down
25 changes: 23 additions & 2 deletions dref/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.contrib.auth.models import User
from django.db import models, transaction
from django.utils import timezone
from django.utils.translation import gettext
from django.utils.translation import get_language, gettext
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers
Expand Down Expand Up @@ -205,6 +205,7 @@ class MiniDrefSerializer(serializers.ModelSerializer):
unpublished_final_report_count = serializers.SerializerMethodField()
operational_update_details = serializers.SerializerMethodField()
final_report_details = serializers.SerializerMethodField()
original_language = serializers.CharField(read_only=True)

class Meta:
model = Dref
Expand Down Expand Up @@ -235,6 +236,7 @@ class Meta:
"status",
"status_display",
"date_of_approval",
"original_language",
]

@extend_schema_field(MiniOperationalUpdateActiveSerializer(many=True))
Expand Down Expand Up @@ -348,22 +350,29 @@ def get_image_url(self, identifiedneed) -> str:


class MiniOperationalUpdateSerializer(ModelSerializer):
status_display = serializers.CharField(source="get_status_display", read_only=True)

class Meta:
model = DrefOperationalUpdate
fields = [
"id",
"title",
"operational_update_number",
"status",
"status_display",
]


class MiniDrefFinalReportSerializer(ModelSerializer):
status_display = serializers.CharField(source="get_status_display", read_only=True)

class Meta:
model = DrefFinalReport
fields = [
"id",
"title",
"status",
"status_display",
]


Expand Down Expand Up @@ -426,6 +435,7 @@ class Meta:
"created_by",
"budget_file_preview",
"is_dref_imminent_v2",
"original_language",
)
exclude = (
"cover_image",
Expand Down Expand Up @@ -603,6 +613,8 @@ def validate_budget_file_preview(self, budget_file_preview):
return budget_file_preview

def create(self, validated_data):
current_lang = get_language()
validated_data["original_language"] = current_lang
validated_data["created_by"] = self.context["request"].user
validated_data["is_active"] = True
type_of_dref = validated_data.get("type_of_dref")
Expand Down Expand Up @@ -639,6 +651,8 @@ def create(self, validated_data):
to = {u.email for u in validated_data["users"]}
else:
to = None
# set original language
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a TODO?

validated_data["original_language"] = current_lang
dref = super().create(validated_data)
if to:
transaction.on_commit(lambda: send_dref_email.delay(dref.id, list(to), "New"))
Expand Down Expand Up @@ -723,6 +737,7 @@ class Meta:
"operational_update_number",
"modified_by",
"created_by",
"original_language",
)
exclude = (
"images",
Expand Down Expand Up @@ -776,6 +791,7 @@ def validate_images_file(self, images):

def create(self, validated_data):
dref = validated_data["dref"]
current_language = get_language()
dref_operational_update = DrefOperationalUpdate.objects.filter(dref=dref).order_by("-operational_update_number").first()
validated_data["created_by"] = self.context["request"].user
if not dref_operational_update:
Expand Down Expand Up @@ -899,7 +915,7 @@ def create(self, validated_data):
validated_data["is_man_made_event"] = dref.is_man_made_event
validated_data["event_text"] = dref.event_text
validated_data["did_national_society"] = dref.did_national_society

validated_data["original_language"] = current_language
operational_update = super().create(validated_data)
# XXX: Copy files from DREF (Only nested serialized fields)
nested_serialized_file_fields = [
Expand Down Expand Up @@ -1112,6 +1128,7 @@ class Meta:
"created_by",
"financial_report_preview",
"is_dref_imminent_v2",
"original_language",
)
exclude = (
"images",
Expand Down Expand Up @@ -1222,13 +1239,15 @@ def create(self, validated_data):
# here check if there is operational update for corresponding dref
# if yes copy from the latest operational update
# else copy from dref
current_language = get_language()
dref = validated_data["dref"]
dref_operational_update = (
DrefOperationalUpdate.objects.filter(dref=dref, status=Dref.Status.APPROVED)
.order_by("-operational_update_number")
.first()
)
validated_data["created_by"] = self.context["request"].user
validated_data["original_language"] = current_language
# NOTE: Checks and common fields for the new dref final reports of new dref imminents
if dref.type_of_dref == Dref.DrefType.IMMINENT and dref.is_dref_imminent_v2:
validated_data["is_dref_imminent_v2"] = True
Expand Down Expand Up @@ -1526,6 +1545,7 @@ class CompletedDrefOperationsSerializer(serializers.ModelSerializer):
status_display = serializers.CharField(source="get_status_display", read_only=True)
application_type = serializers.SerializerMethodField()
application_type_display = serializers.SerializerMethodField()
original_language = serializers.CharField(read_only=True)

class Meta:
model = DrefFinalReport
Expand All @@ -1543,6 +1563,7 @@ class Meta:
"dref",
"status",
"status_display",
"original_language",
)

def get_application_type(self, obj) -> str:
Expand Down
4 changes: 4 additions & 0 deletions dref/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from celery import shared_task
from django.template.loader import render_to_string

Expand All @@ -6,6 +8,8 @@
from .models import Dref
from .utils import get_email_context

logger = logging.getLogger(__name__)


@shared_task
def send_dref_email(dref_id, users_emails, new_or_updated=""):
Expand Down
Loading
Loading