def save_redemptions(request, redemptions): # lock these rows to prevent race conditions list(request.user.reward_point_grantings.select_for_update()) list(request.user.reward_point_redemptions.select_for_update()) total_points_available = reward_points_of_user(request.user) total_points_redeemed = sum(redemptions.values()) if total_points_redeemed <= 0: raise NoPointsSelected(_("You cannot redeem 0 points.")) if total_points_redeemed > total_points_available: raise NotEnoughPoints(_("You don't have enough reward points.")) for event_id in redemptions: if redemptions[event_id] > 0: event = RewardPointRedemptionEvent.objects.get(id=event_id) if event.redeem_end_date < date.today(): raise RedemptionEventExpired(_("Sorry, the deadline for this event expired already.")) RewardPointRedemption.objects.create( user_profile=request.user, value=redemptions[event_id], event=event )
def save_redemptions(request, redemptions): total_points_available = reward_points_of_user(request.user) total_points_redeemed = sum(redemptions.values()) if total_points_redeemed <= 0: raise NoPointsSelected(_("You cannot redeem 0 points.")) if total_points_redeemed > total_points_available: raise NotEnoughPoints(_("You don't have enough reward points.")) for event_id in redemptions: if redemptions[event_id] > 0: redemption = RewardPointRedemption( user_profile=request.user, value=redemptions[event_id], event=RewardPointRedemptionEvent.objects.get(id=event_id)) redemption.save()