Skip to content
This repository was archived by the owner on Mar 28, 2024. It is now read-only.

Commit f585380

Browse files
authored
Merge pull request #79 from OpenSPP/78-fix-error-in-_cancel_entitlements_async-in-spp_entitlement_in_kind
Fix error in in-kind entitlement cancellation and deleting multiple entitlements
2 parents aab216c + f43a6ed commit f585380

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

Diff for: spp_entitlement_basket/models/entitlement_manager.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,17 @@ def set_pending_validation_entitlements(self, cycle):
172172
:param cycle: A recordset of cycle
173173
:return:
174174
"""
175-
# Get the number of entitlements in cycle
175+
# Get the number of entitlements
176+
entitlements_count = cycle.get_entitlements(
177+
["draft"], entitlement_model="g2p.entitlement.inkind", count=True
178+
)
179+
180+
# Get the entitlements
176181
entitlements = cycle.get_entitlements(
177182
["draft"],
178183
entitlement_model="g2p.entitlement.inkind",
179184
)
180-
entitlements_count = len(entitlements)
185+
181186
if entitlements_count < self.MIN_ROW_JOB_QUEUE:
182187
self._set_pending_validation_entitlements(entitlements)
183188

@@ -193,7 +198,7 @@ def _set_pending_validation_entitlements(self, entitlements):
193198
:return:
194199
"""
195200
entitlements.update({"state": "pending_validation"})
196-
_logger.debug("Entitlement Validation: total: %s" % (len(entitlements)))
201+
# _logger.debug("Entitlement Validation: total: %s" % (len(entitlements)))
197202

198203
def validate_entitlements(self, cycle):
199204
"""Validate Basket Entitlements.
@@ -203,12 +208,19 @@ def validate_entitlements(self, cycle):
203208
:param cycle: A recordset of cycle
204209
:return:
205210
"""
206-
# Get the number of entitlements in cycle
211+
# Get the number of entitlements
212+
entitlements_count = cycle.get_entitlements(
213+
["draft", "pending_validation"],
214+
entitlement_model="g2p.entitlement.inkind",
215+
count=True,
216+
)
217+
218+
# Get the entitlements
207219
entitlements = cycle.get_entitlements(
208220
["draft", "pending_validation"],
209221
entitlement_model="g2p.entitlement.inkind",
210222
)
211-
entitlements_count = len(entitlements)
223+
212224
if entitlements_count < self.MIN_ROW_JOB_QUEUE:
213225
err, message = self._validate_entitlements(entitlements)
214226
if err > 0:
@@ -264,12 +276,19 @@ def cancel_entitlements(self, cycle):
264276
:param cycle: A recordset of cycle
265277
:return:
266278
"""
267-
# Get the entitlements in cycle
279+
# Get the number of entitlements
280+
entitlements_count = cycle.get_entitlements(
281+
["draft", "pending_validation", "approved"],
282+
entitlement_model="g2p.entitlement.inkind",
283+
count=True,
284+
)
285+
286+
# Get the entitlements
268287
entitlements = cycle.get_entitlements(
269288
["draft", "pending_validation", "approved"],
270289
entitlement_model="g2p.entitlement.inkind",
271290
)
272-
entitlements_count = len(entitlements)
291+
273292
if entitlements_count < self.MIN_ROW_JOB_QUEUE:
274293
self._cancel_entitlements(entitlements)
275294
else:

Diff for: spp_entitlement_in_kind/models/entitlement_manager.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -266,33 +266,32 @@ def cancel_entitlements(self, cycle):
266266
:param cycle: A recordset of cycle
267267
:return:
268268
"""
269-
# Get the entitlements in cycle
269+
# Get the total number of entitlements
270270
entitlements_count = cycle.get_entitlements(
271271
["draft", "pending_validation", "approved"],
272272
entitlement_model="g2p.entitlement.inkind",
273273
count=True,
274274
)
275+
276+
# Get the entitlements
277+
entitlements = cycle.get_entitlements(
278+
["draft", "pending_validation", "approved"],
279+
entitlement_model="g2p.entitlement.inkind",
280+
)
281+
275282
if entitlements_count < self.MIN_ROW_JOB_QUEUE:
276-
self._cancel_entitlements(cycle)
283+
self._cancel_entitlements(entitlements)
277284
else:
278-
self._cancel_entitlements_async(cycle, entitlements_count)
285+
self._cancel_entitlements_async(cycle, entitlements, entitlements_count)
279286

280-
def _cancel_entitlements(self, cycle, offset=0, limit=None):
287+
def _cancel_entitlements(self, entitlements):
281288
"""Cancel In-Kind Entitlements.
282-
Basket Entitlement Manager :meth:`_cancel_entitlements`.
289+
In-Kind Entitlement Manager :meth:`_cancel_entitlements`.
283290
Cancel entitlements in a cycle.
284291
285-
:param cycle: A recordset of cycle
286-
:param offset: An integer value to be used in :meth:`cycle.get_entitlements` for setting the query offset
287-
:param limit: An integer value to be used in :meth:`cycle.get_entitlements` for setting the query limit
292+
:param entitlements: A recordset of entitlements to cancel
288293
:return:
289294
"""
290-
entitlements = cycle.get_entitlements(
291-
["draft", "pending_validation", "approved"],
292-
entitlement_model="g2p.entitlement.inkind",
293-
offset=offset,
294-
limit=limit,
295-
)
296295
entitlements.update({"state": "cancelled"})
297296

298297
def approve_entitlements(self, entitlements):

Diff for: spp_programs/models/entitlement.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,13 @@ def _gc_mark_expired_entitlement(self):
159159
).write({"state": "expired"})
160160

161161
def unlink(self):
162-
if self.state == "draft":
163-
return super(InKindEntitlement, self).unlink()
164-
else:
165-
raise ValidationError(
166-
_("Only draft entitlements are allowed to be deleted")
167-
)
162+
for rec in self:
163+
if rec.state == "draft":
164+
return super(InKindEntitlement, self).unlink()
165+
else:
166+
raise ValidationError(
167+
_("Only draft entitlements are allowed to be deleted")
168+
)
168169

169170
def approve_entitlement(self):
170171
state_err, message = self.program_id.get_manager(

0 commit comments

Comments
 (0)