def complete_uncommitted_actions(request, caller_context, container_user):
    accountController = AccountController()
    success = True
    try:
        transfer_count = accountController.execute_uncommitted_account_transfers(20)
    except Exception, e:
        success = False
        logging.error(e)
    def check_daily_reward(self, container_user):
        '''was rewarding experience for daily visit as well as coins.  dropping it for now.
           characterController.add_character_reward(container_user.character, "daily visit", 'experience', 50)
        '''

        accountController = AccountController()
        today = date.today()
        def get_or_create_daily_reward_tx():
            todays_reward = Daily_reward.get_by_key_name(str(today))
            if not todays_reward:
                todays_reward = Daily_reward(key_name=str(today), date=today)
                todays_reward.put()
                coins_account = Account(parent=todays_reward, 
                                       currency_type='coins', 
                                       negative_balance_allowed=True)
                coins_account.put()
                todays_reward.coins_account = coins_account # redundant account again, is this really necessary?
                todays_reward.put()
            return todays_reward
                        
        def pay_daily_reward_tx(reward_account, character_coins_account):
            dest_transfer = Account_transfer.get_by_key_name(str(character_coins_account.key()), parent=reward_account) #str(reward_account.key())
            if not dest_transfer:
                amount = random.randint(25, 75)
                transfer = accountController.transfer_currency(todays_reward.coins_account, character_coins_account, amount)
                return transfer
            return None
            
        todays_reward = db.run_in_transaction(get_or_create_daily_reward_tx)        
        reward_transfer = db.run_in_transaction(pay_daily_reward_tx, todays_reward.coins_account, container_user.character.coins_account)
        if reward_transfer:
            try:
                accountController.roll_forward_account_transfer(reward_transfer)
            except Exception, e:
                logging.exception(e)
            return reward_transfer.amount
def offerpal_reward(request, caller_context, container_user, container):

    logging.info('offerpal deposit request: %s' % request)
    accountController = AccountController()

    required_fields = ('id', 'snuid', 'currency', 'verifier')
    for field in required_fields:
        if field not in request.GET:
            return HttpResponse(status=400, content='Missing required field: %s' % field)
    offerpal_secret_keys = {'orkut.com' : '1174959640013506'}
    offerpal_id = request.GET['id']
    snuid = request.GET['snuid']
    amount = int(request.GET['currency'])
    verifier = request.GET['verifier']
    affl = None
    if 'affl' in request.GET:
        affl = request.GET['affl'] #optional tracking id
    error = None
    if 'error' in request.GET:
        error = request.GET['error']
    base_string = '%s:%s:%d:%s' % (offerpal_id, snuid, amount, offerpal_secret_keys[container])
    match_string = md5.new(base_string).hexdigest()
    success = False
    found_user = None

    deposit = None
    try:
        deposit = Offerpal_deposit.get_by_key_name(offerpal_id)
    except:
        pass
    if deposit and deposit.success:
        logging.info('duplicate request %s' % deposit.offerpal_id)
        response = HttpResponse('Duplicate request, already received id: %s' % offerpal_id)
        response.status_code = 403 # 403 tells offerpal not to try again
        return response
    
    if match_string != verifier:
        logging.info('base: %s match: %s verifier: %s' % (base_string, match_string, verifier))
        verified = False
        response = HttpResponse('Authorization Failed.')
        response.status_code = 401
    else:
        verified = True
        domain = container             
        container_user_key = domain + ":" + snuid;
        container_user = Container_user.get_by_key_name(container_user_key)
        if container_user == None:
            response = HttpResponse('User Not Found')
            logging.info('could not look up user %s' % container_user_key)
            found_user = False
            response.status_code = 403 # 403 tells offerpal not to try again
            #todo: log this! send email?
        else:
            # log increase, message container_user, notification to container_user
            response = HttpResponse('offerpal reward for %s user.  Reward: %d.' % (container, amount), 'text/html')
            success = True
            found_user = True

    offerpal_deposit = Offerpal_deposit(key_name = offerpal_id,
                                            offerpal_id = offerpal_id, 
                                            snuid = snuid,
                                            currency_type = 'gold',
                                            deposit_amount = amount,
                                            verifier = verifier,
                                            verified = verified,
                                            found_user = found_user,
                                            affl = affl,
                                            error = error,
                                            response_code = response.status_code,
                                            success = success)
    offerpal_deposit.put()
    gold_account = Account(parent=offerpal_deposit,
                           key_name=offerpal_id,
                           currency_type='gold', 
                           negative_balance_allowed=False, 
                           balance=amount)
    gold_account.put()
    offerpal_deposit.account = gold_account
    offerpal_deposit.put()
    
    transfer = accountController.transfer_currency_in_txn(offerpal_deposit.account, container_user.character.gold_account, amount)
    if transfer:
        try:
            message = Message(message_type=5,
                              recipient=container_user.character, 
                              body="Your Offerpal deposit has posted! You have been credited with %d gold pieces" % amount,
                              pop_message_box = True)
            
            message.put() # notify depositor
            accountController.roll_forward_account_transfer(transfer)

        except Exception, e:
            logging.exception(e)
            logging.warning('failed to roll forward transfer of Offerpal deposit')