Skip to content

Commit 4141551

Browse files
Fix grant pending status sync issue
Change pending_status field logic: - pending_status=None means no pending change (use current status) - pending_status=value means there''s a pending status change This fixes the issue where confirmed grants still showed as pending. Changes: - Modified Grant.pending_status field to allow null values - Removed problematic sync logic in save() method - Updated _calculate_grant_amounts to use effective status - Updated admin queries and actions to work with new logic - Updated tests to reflect new behavior Co-authored-by: Marco Acierno <[email protected]>
1 parent f8abc19 commit 4141551

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

backend/custom_admin/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ def confirm_pending_status(modeladmin, request, queryset):
6767
@validate_single_conference_selection
6868
def reset_pending_status_back_to_status(modeladmin, request, queryset):
6969
queryset.update(
70-
pending_status=F("status"),
70+
pending_status=None,
7171
)

backend/grants/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,8 @@ def get_queryset(self, request):
652652
return (
653653
super()
654654
.get_queryset(request)
655-
.exclude(
656-
pending_status=F("status"),
655+
.filter(
656+
pending_status__isnull=False,
657657
)
658658
)
659659

backend/grants/models.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class ApprovedType(models.TextChoices):
149149
_("pending status"),
150150
choices=Status.choices,
151151
max_length=30,
152-
default=Status.pending,
152+
null=True,
153153
blank=True,
154154
)
155155
approved_type = models.CharField(
@@ -229,9 +229,6 @@ def save(self, *args, **kwargs):
229229
self._update_country_type()
230230
self._calculate_grant_amounts()
231231

232-
if self.pending_status == self._original_status:
233-
self.pending_status = self.status
234-
235232
update_fields = kwargs.get("update_fields", None)
236233
if update_fields:
237234
update_fields.append("total_amount")
@@ -249,7 +246,9 @@ def save(self, *args, **kwargs):
249246
self._original_status = self.status
250247

251248
def _calculate_grant_amounts(self):
252-
if self.pending_status != Grant.Status.approved:
249+
# Use pending_status if set, otherwise use current status
250+
effective_status = self.pending_status if self.pending_status is not None else self.status
251+
if effective_status != Grant.Status.approved:
253252
return
254253

255254
if (

backend/grants/tests/test_models.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def test_sets_country_type_does_nothing_if_unset():
162162
assert grant.country_type is None
163163

164164

165-
def test_syncs_pending_status_on_change():
165+
def test_pending_status_no_longer_syncs_with_status():
166166
grant = GrantFactory(
167167
pending_status=Grant.Status.pending,
168168
status=Grant.Status.pending,
@@ -171,10 +171,10 @@ def test_syncs_pending_status_on_change():
171171
grant.status = Grant.Status.approved
172172
grant.save(update_fields=["status"])
173173

174-
# Pending status should be updated to match the status
174+
# Pending status should remain unchanged when status changes
175175
grant.refresh_from_db()
176176

177-
assert grant.pending_status == Grant.Status.approved
177+
assert grant.pending_status == Grant.Status.pending # Should remain unchanged
178178
assert grant.status == Grant.Status.approved
179179

180180

@@ -192,3 +192,34 @@ def test_doesnt_sync_pending_status_if_different_values():
192192

193193
assert grant.pending_status == Grant.Status.refused
194194
assert grant.status == Grant.Status.waiting_for_confirmation
195+
196+
197+
def test_pending_status_none_means_no_pending_change():
198+
grant = GrantFactory(
199+
pending_status=None,
200+
status=Grant.Status.approved,
201+
)
202+
203+
# When pending_status is None, the effective status should be the current status
204+
# This affects the _calculate_grant_amounts method
205+
grant.approved_type = Grant.ApprovedType.ticket_only
206+
grant.departure_country = "IT"
207+
grant.save()
208+
209+
# Since effective status is approved (from status field), amounts should be calculated
210+
assert grant.ticket_amount is not None
211+
212+
213+
def test_pending_status_set_overrides_current_status():
214+
grant = GrantFactory(
215+
pending_status=Grant.Status.approved,
216+
status=Grant.Status.pending,
217+
)
218+
219+
# When pending_status is set, it should be used as the effective status
220+
grant.approved_type = Grant.ApprovedType.ticket_only
221+
grant.departure_country = "IT"
222+
grant.save()
223+
224+
# Since effective status is approved (from pending_status), amounts should be calculated
225+
assert grant.ticket_amount is not None

0 commit comments

Comments
 (0)