def try_transact_funds(from_username, to_username, amount, bid=None): """ Returns (success, msg) tuple """ try: from_prof = User.objects.get(username=from_username).profile to_prof = User.objects.get(username=to_username).profile except User.DoesNotExist: log_error.error("Tried to transact funds with non-existing user") try: _lock_dat_shit(from_prof, 'credits') _lock_dat_shit(to_prof, 'credits') if (from_prof.credits < amount): if from_username == 'EXCHANGE': # if the exchange runs out, re-up it from_prof.credits += 10000 + amount # in case transaction is more than 10000 else: return (False, "Not enough credits") #TODO think of more ways this could go wrong record_transaction(from_username, to_username, amount, datetime.now(), bid) from_prof.credits -= amount to_prof.credits += amount # saves in finally block return (True, "success") except Exception, msg: return (False, "Something went wrong")
def add_credit_backdoor(username, amount): """ Use try_transact_credits to make all transactions except backdoor bonuses """ user = User.objects.get(username=username) user_profile = user.profile _lock_dat_shit(user_profile, 'credits') try: # safety zone start user_profile.credits += amount record_transaction('EXCHANGE', username, amount, datetime.now(), None) finally: user_profile.save()