Skip to content

Commit

Permalink
Merge pull request #3685 from unicef/29971/revert-a-terminated-pca-in…
Browse files Browse the repository at this point in the history
…-admin

[ch29971] Admin agreements: add-the-ability-to-revert-a-terminated-pca
  • Loading branch information
robertavram authored Jun 7, 2024
2 parents 9948a61 + 9b8db1b commit caf3c23
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/etools/applications/partners/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.forms import SelectMultiple
from django.http.response import HttpResponseRedirect
from django.urls import reverse
from django.utils import timezone
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
Expand All @@ -17,6 +18,7 @@
from unicef_attachments.admin import AttachmentSingleInline
from unicef_attachments.models import Attachment
from unicef_snapshot.admin import ActivityInline, SnapshotModelAdmin
from unicef_snapshot.models import Activity

from etools.applications.partners.exports import PartnerExport
from etools.applications.partners.forms import InterventionAttachmentForm # TODO intervention sector locations cleanup
Expand Down Expand Up @@ -709,12 +711,18 @@ class AgreementAttachmentInline(AttachmentSingleInline):
code = 'partners_agreement'


class AgreementTerminationDocAttachmentInline(AttachmentSingleInline):
verbose_name_plural = _('Termination Doc Attachment')
code = 'partners_agreement_termination_doc'


class AgreementAdmin(
AttachmentInlineAdminMixin,
ExportMixin,
HiddenPartnerMixin,
CountryUsersAdminMixin,
RestrictedEditAdminMixin,
ExtraUrlMixin,
SnapshotModelAdmin,
):
staff_only = False
Expand Down Expand Up @@ -766,6 +774,7 @@ class AgreementAdmin(
inlines = [
ActivityInline,
AgreementAttachmentInline,
AgreementTerminationDocAttachmentInline
]

def has_module_permission(self, request):
Expand Down Expand Up @@ -811,6 +820,44 @@ def get_interventions_admin_urls(self, objs):
urls.append(formatted_url)
return urls

@button(permission=lambda request, obj: request.user.groups.filter(name='RSS').exists() and
obj.status == Agreement.TERMINATED and obj.end > timezone.now().date())
def revert_termination(self, request, pk):
agreement = Agreement.objects.get(pk=pk)
agreement.status = Agreement.SIGNED
try:
termination_doc = Attachment.objects.get(
code='partners_agreement_termination_doc',
content_type=ContentType.objects.get_for_model(Agreement),
object_id=agreement.pk
)
agreement.termination_doc.remove(termination_doc)
except Attachment.DoesNotExist:
pass

agreement.save(update_fields=['status'])

terminated_interventions = agreement.interventions.filter(status=Intervention.TERMINATED)
interventions_to_update = []
for i in terminated_interventions:
intervention_activities = Activity.objects.filter(
target_content_type=ContentType.objects.get_for_model(Intervention),
target_object_id=i.id,
action=Activity.UPDATE,
change__status__after=Intervention.TERMINATED,
)
if not intervention_activities.exists():
continue

previous_status = intervention_activities.last().change['status']['before']
if previous_status in [Intervention.SIGNED, Intervention.ACTIVE, Intervention.SUSPENDED]:
i.status = previous_status
interventions_to_update.append(i)

Intervention.objects.bulk_update(interventions_to_update, fields=['status'])

return HttpResponseRedirect(reverse('admin:partners_agreement_change', args=[pk]))


class FileTypeAdmin(RestrictedEditAdmin):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ msgstr "إطار النهج المنسق للتحويلات النقدية"
msgid "Signed Amendment"
msgstr "التعديل الموقع"

msgid "Termination Doc Attachment"
msgstr ""

msgid "Agreement Details"
msgstr "تفاصيل الاتفاقية"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ msgstr "Hact"
msgid "Signed Amendment"
msgstr "Enmienda firmada"

msgid "Termination Doc Attachment"
msgstr ""

msgid "Agreement Details"
msgstr "Detalles del acuerdo"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ msgstr "Hact"
msgid "Signed Amendment"
msgstr "Amendement signé"

msgid "Termination Doc Attachment"
msgstr ""

msgid "Agreement Details"
msgstr "Détails de l'accord"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ msgstr "HACT"
msgid "Signed Amendment"
msgstr "Aditivo assinado"

msgid "Termination Doc Attachment"
msgstr ""

msgid "Agreement Details"
msgstr "Detalhes do Acordo"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ msgstr "ГПДП"
msgid "Signed Amendment"
msgstr "Подписанная поправка"

msgid "Termination Doc Attachment"
msgstr ""

msgid "Agreement Details"
msgstr "Детали соглашения"

Expand Down
50 changes: 49 additions & 1 deletion src/etools/applications/partners/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import datetime

from django.contrib.admin.sites import AdminSite
from django.core.files.uploadedfile import SimpleUploadedFile

from unicef_snapshot.models import Activity

from etools.applications.attachments.tests.factories import AttachmentFactory
from etools.applications.core.tests.cases import BaseTenantTestCase
from etools.applications.partners.admin import AgreementAdmin, InterventionAdmin
from etools.applications.partners.models import Agreement, Intervention
from etools.applications.partners.tests.factories import AgreementFactory, InterventionFactory, PartnerFactory
from etools.applications.reports.tests.factories import CountryProgrammeFactory
from etools.applications.users.tests.factories import UserFactory
from etools.applications.users.tests.factories import CountryFactory, GroupFactory, RealmFactory, UserFactory


class MockRequest:
Expand Down Expand Up @@ -103,3 +105,49 @@ def test_save_model_update(self):
"after": Agreement.TERMINATED
}
})

def test_revert_termination(self):
RealmFactory(
user=self.user,
country=CountryFactory(),
organization=self.user.profile.organization,
group=GroupFactory(name='RSS')
)

agreement = AgreementFactory(
partner=self.partner, status=Agreement.TERMINATED, end=datetime.date.today() + datetime.timedelta(days=7))
a = AttachmentFactory(
code='partners_agreement_termination_doc',
content_object=agreement,
file=SimpleUploadedFile('simple_file.txt', b'simple_file.txt'),
)
agreement.termination_doc.add(a)
suspended_pd = InterventionFactory(
agreement=agreement,
title='Intervention 1',
status=Intervention.SUSPENDED,
)
ia = InterventionAdmin(Intervention, self.site)
suspended_pd.status = Intervention.TERMINATED
ia.save_model(self.request, suspended_pd, {}, True)

closed_pd = InterventionFactory(
agreement=agreement,
title='Intervention 1',
status=Intervention.CLOSED,
)
closed_pd.status = Intervention.TERMINATED
ia.save_model(self.request, closed_pd, {}, True)

aa = AgreementAdmin(Agreement, self.site)
aa.revert_termination(self.request, pk=agreement.pk)

agreement.refresh_from_db()
self.assertEqual(agreement.status, Agreement.SIGNED)
self.assertEqual(agreement.termination_doc.count(), 0)

suspended_pd.refresh_from_db()
self.assertEqual(suspended_pd.status, Intervention.SUSPENDED)

closed_pd.refresh_from_db()
self.assertEqual(closed_pd.status, Intervention.TERMINATED)

0 comments on commit caf3c23

Please sign in to comment.