Skip to content

Commit

Permalink
Merge pull request #1858 from IFRCGo/hot-fix/dref-final-report
Browse files Browse the repository at this point in the history
Rename incorrect name fields
  • Loading branch information
szabozoltan69 authored Jul 28, 2023
2 parents 3a109f7 + 1edd526 commit d064974
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 11 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

## 1.1.484

### Added
- Hotfix for DREF final reports

## 1.1.483

### Added
Expand Down Expand Up @@ -2262,7 +2267,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## 0.1.20

[Unreleased]: https://github.com/IFRCGo/go-api/compare/1.1.483...HEAD
[Unreleased]: https://github.com/IFRCGo/go-api/compare/1.1.484...HEAD
[1.1.484]: https://github.com/IFRCGo/go-api/compare/1.1.483...1.1.484
[1.1.483]: https://github.com/IFRCGo/go-api/compare/1.1.482...1.1.483
[1.1.482]: https://github.com/IFRCGo/go-api/compare/1.1.481...1.1.482
[1.1.481]: https://github.com/IFRCGo/go-api/compare/1.1.480...1.1.481
Expand Down
18 changes: 18 additions & 0 deletions dref/migrations/0064_dreffinalreport_financial_report_preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.20 on 2023-07-27 12:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dref', '0063_merge_20230628_1022'),
]

operations = [
migrations.AddField(
model_name='dreffinalreport',
name='financial_report_preview',
field=models.FileField(blank=True, null=True, upload_to='dref/images/', verbose_name='financial preview'),
),
]
29 changes: 29 additions & 0 deletions dref/migrations/0065_auto_20230728_0405.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.20 on 2023-07-28 04:05

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('dref', '0064_dreffinalreport_financial_report_preview'),
]

operations = [
migrations.AlterField(
model_name='dref',
name='modified_at',
field=models.DateTimeField(default=django.utils.timezone.now, null=True, verbose_name='modified at'),
),
migrations.AlterField(
model_name='dreffinalreport',
name='modified_at',
field=models.DateTimeField(default=django.utils.timezone.now, null=True, verbose_name='modified at'),
),
migrations.AlterField(
model_name='drefoperationalupdate',
name='modified_at',
field=models.DateTimeField(default=django.utils.timezone.now, null=True, verbose_name='modified at'),
),
]
26 changes: 23 additions & 3 deletions dref/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.templatetags.static import static
from django.core.exceptions import ValidationError
from django.contrib.postgres.aggregates import ArrayAgg
from django.utils import timezone

from api.models import Country, DisasterType, District, FieldReport

Expand Down Expand Up @@ -224,7 +225,7 @@ class Status(models.IntegerChoices):
COMPLETED = 1, _("Completed")

created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True, blank=True)
modified_at = models.DateTimeField(verbose_name=_("modified at"), default=timezone.now, null=True)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("created by"),
Expand Down Expand Up @@ -657,9 +658,10 @@ def clone(self, user):
return clone


@reversion.register()
class DrefOperationalUpdate(models.Model):
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True, blank=True)
modified_at = models.DateTimeField(verbose_name=_("modified at"), default=timezone.now, null=True)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("created by"),
Expand Down Expand Up @@ -1014,9 +1016,10 @@ def get_for(user):
return DrefOperationalUpdate.objects.filter(id__in=union_query.values("id")).distinct()


@reversion.register()
class DrefFinalReport(models.Model):
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True)
modified_at = models.DateTimeField(verbose_name=_("modified at"), default=timezone.now, null=True)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("created by"),
Expand Down Expand Up @@ -1260,6 +1263,9 @@ class DrefFinalReport(models.Model):
verbose_name=_("financial report"),
related_name="financial_report_dref_final_report",
)
financial_report_preview = models.FileField(
verbose_name=_("financial preview"), null=True, blank=True, upload_to="dref/images/"
)
num_assisted = models.IntegerField(verbose_name=_("number of assisted"), blank=True, null=True)
has_national_society_conducted = models.BooleanField(
verbose_name=_("Has national society conducted any intervention"), null=True, blank=True
Expand All @@ -1273,13 +1279,27 @@ class DrefFinalReport(models.Model):
null=True,
blank=True,
)
__financial_report_id = None

class Meta:
verbose_name = _("Dref Final Report")
verbose_name_plural = _("Dref Final Reports")

def save(self, *args, **kwargs):
if self.financial_report_id and self.financial_report_id != self.__financial_report_id:
pages = convert_from_bytes(self.financial_report.file.read())
if len(pages) > 0:
financial_report_preview = pages[0] # get first page
filename = f'preview_{self.financial_report.file.name.split("/")[0]}.png'
temp_image = open(os.path.join("/tmp", filename), "wb")
financial_report_preview.save(temp_image, "PNG")
thumb_data = open(os.path.join("/tmp", filename), "rb")
self.financial_report_preview.save(filename, thumb_data, save=False)
else:
raise ValidationError({"financial_report": "Sorry cannot generate preview for empty pdf"})

self.status = Dref.Status.COMPLETED if self.is_published else Dref.Status.IN_PROGRESS
self.__financial_report_id = self.financial_report_id
super().save(*args, **kwargs)

@staticmethod
Expand Down
16 changes: 11 additions & 5 deletions dref/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ def update(self, instance, validated_data):
to = None
if modified_at and instance.modified_at and modified_at < instance.modified_at:
raise serializers.ValidationError({"modified_at": settings.DREF_OP_UPDATE_FINAL_REPORT_UPDATE_ERROR_MESSAGE})
validated_data["modified_at"] = modified_at
dref = super().update(instance, validated_data)
if to:
transaction.on_commit(lambda: send_dref_email.delay(dref.id, list(to), "Updated"))
Expand Down Expand Up @@ -549,7 +550,7 @@ class DrefOperationalUpdateSerializer(NestedUpdateMixin, NestedCreateMixin, Mode

class Meta:
model = DrefOperationalUpdate
read_only_fields = ("operational_update_number",)
read_only_fields = ("operational_update_number", "modified_by", "created_by")
exclude = ("images", "photos", "event_map", "cover_image")

def validate(self, data):
Expand All @@ -574,7 +575,7 @@ def validate(self, data):
return data

def validate_appeal_code(self, appeal_code):
if appeal_code != self.instance.appeal_code:
if appeal_code and appeal_code != self.instance.appeal_code:
raise serializers.ValidationError("Can't edit MDR Code")
return appeal_code

Expand All @@ -590,6 +591,7 @@ def create(self, validated_data):
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:
validated_data["title"] = dref.title
validated_data["title_prefix"] = dref.title_prefix
Expand Down Expand Up @@ -807,7 +809,7 @@ def create(self, validated_data):
return operational_update

def update(self, instance, validated_data):
validated_data["updated_by"] = self.context["request"].user
validated_data["modified_by"] = self.context["request"].user
# changing_timeframe_operation = validated_data.get(
# "changing_timeframe_operation", instance.changing_timeframe_operation
# )
Expand Down Expand Up @@ -887,6 +889,7 @@ def update(self, instance, validated_data):

if modified_at and instance.modified_at and modified_at < instance.modified_at:
raise serializers.ValidationError({"modified_at": settings.DREF_OP_UPDATE_FINAL_REPORT_UPDATE_ERROR_MESSAGE})
validated_data["modified_at"] = modified_at
return super().update(instance, validated_data)


Expand All @@ -911,9 +914,11 @@ class DrefFinalReportSerializer(NestedUpdateMixin, NestedCreateMixin, ModelSeria
financial_report_details = DrefFileSerializer(source="financial_report", read_only=True)
created_by_details = UserNameSerializer(source="created_by", read_only=True)
users_details = UserNameSerializer(source="users", many=True, read_only=True)
modified_by_details = UserNameSerializer(source="modified_by", read_only=True)

class Meta:
model = DrefFinalReport
read_only_fields = ("modified_by", "created_by", "financial_report_preview")
exclude = (
"images",
"photos",
Expand Down Expand Up @@ -955,6 +960,7 @@ def create(self, validated_data):
dref_operational_update = (
DrefOperationalUpdate.objects.filter(dref=dref, is_published=True).order_by("-operational_update_number").first()
)
validated_data["created_by"] = self.context["request"].user
if dref_operational_update:
validated_data["title"] = dref_operational_update.title
validated_data["title_prefix"] = dref_operational_update.title_prefix
Expand Down Expand Up @@ -1150,8 +1156,8 @@ def update(self, instance, validated_data):
raise serializers.ValidationError({"modified_at": "Modified At is required!"})
if modified_at and instance.modified_at and modified_at < instance.modified_at:
raise serializers.ValidationError({"modified_at": settings.DREF_OP_UPDATE_FINAL_REPORT_UPDATE_ERROR_MESSAGE})

validated_data["updated_by"] = self.context["request"].user
validated_data["modified_at"] = modified_at
validated_data["modified_by"] = self.context["request"].user
return super().update(instance, validated_data)


Expand Down
3 changes: 2 additions & 1 deletion dref/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def test_dref_is_published(self, mock_now):
Test for dref if is_published = True
"""

initial_now = datetime(2011, 11, 11)
initial_now = datetime(2023, 7, 28)
mock_now.return_value = initial_now

region = Region.objects.create(name=RegionName.AFRICA)
Expand Down Expand Up @@ -430,6 +430,7 @@ def test_dref_is_published(self, mock_now):
)
url = f"/api/v2/dref/{not_published_dref.id}/"
self.client.force_authenticate(self.user)
data["modified_at"] = initial_now + timedelta(days=10)
response = self.client.patch(url, data)
self.assert_200(response)

Expand Down
2 changes: 1 addition & 1 deletion main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from .celery import app as celery_app

__all__ = ['celery_app']
__version__ = '1.1.483'
__version__ = '1.1.484'

0 comments on commit d064974

Please sign in to comment.