def create_card_holds(self, cursor): # Get the list of participants to create card holds for participants = cursor.all(""" SELECT * FROM payday_participants WHERE old_balance < giving_today AND has_credit_card AND is_suspicious IS false """) if not participants: return {} # Fetch existing holds participant_ids = set(p.id for p in participants) holds = self.fetch_card_holds(participant_ids) # Create new holds and check amounts of existing ones def f(p): amount = p.giving_today - p.old_balance if p.id in holds: if amount >= MINIMUM_CHARGE: charge_amount = upcharge(amount)[0] if holds[p.id].amount >= charge_amount: return else: # The amount is too low, cancel the hold and make a new one cancel_card_hold(holds.pop(p.id)) else: # not up to minimum charge level. cancel the hold cancel_card_hold(holds.pop(p.id)) return if amount >= MINIMUM_CHARGE: hold, error = create_card_hold(self.db, p, amount) if error: return 1 else: holds[p.id] = hold threaded_map(f, participants) # Update the values of card_hold_ok in our temporary table if not holds: return {} cursor.run( """ UPDATE payday_participants p SET card_hold_ok = true WHERE p.id IN %s """, (tuple(holds.keys()), )) return holds
def create_card_holds(self, cursor): # Get the list of participants to create card holds for participants = cursor.all(""" SELECT * FROM payday_participants WHERE old_balance < giving_today AND has_credit_card AND is_suspicious IS false """) if not participants: return {} # Fetch existing holds participant_ids = set(p.id for p in participants) holds = self.fetch_card_holds(participant_ids) # Create new holds and check amounts of existing ones def f(p): amount = p.giving_today - p.old_balance if p.id in holds: if amount >= MINIMUM_CHARGE: charge_amount = upcharge(amount)[0] if holds[p.id].amount >= charge_amount: return else: # The amount is too low, cancel the hold and make a new one cancel_card_hold(holds.pop(p.id)) else: # not up to minimum charge level. cancel the hold cancel_card_hold(holds.pop(p.id)) return if amount >= MINIMUM_CHARGE: hold, error = create_card_hold(self.db, p, amount) if error: return 1 else: holds[p.id] = hold threaded_map(f, participants) # Update the values of card_hold_ok in our temporary table if not holds: return {} cursor.run(""" UPDATE payday_participants p SET card_hold_ok = true WHERE p.id IN %s """, (tuple(holds.keys()),)) return holds
def settle_card_holds(self, cursor, holds): log("Settling card holds.") participants = cursor.all(""" SELECT * FROM payday_participants WHERE new_balance < 0 """) participants = [p for p in participants if p.id in holds] log("Capturing card holds.") # Capture holds to bring balances back up to (at least) zero def capture(p): amount = -p.new_balance capture_card_hold(self.db, p, amount, holds.pop(p.id)) threaded_map(capture, participants) log("Captured %i card holds." % len(participants)) log("Canceling card holds.") # Cancel the remaining holds threaded_map(cancel_card_hold, holds.values()) log("Canceled %i card holds." % len(holds))
def sync_all(db): dirty = db.all( 'SELECT package_manager, name FROM packages WHERE readme_raw IS NULL ' 'ORDER BY package_manager DESC, name DESC') threaded_map(Syncer(db), dirty, 4)