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 _tx():
     dest_transfer = Account_transfer.get_by_key_name(str(source_transfer.key()), parent=source_transfer.counter_account.key())
     if not dest_transfer:
         dest_transfer = Account_transfer(
             parent = source_transfer.counter_account.key(),
             key_name = str(source_transfer.key()),
             self_account = source_transfer.counter_account,
             counter_account = source_transfer.key().parent(), #same as source_transfer.counter_account
             counter_transfer = source_transfer,
             currency_type = source_transfer.currency_type,
             amount = -source_transfer.amount,
             is_committed = True)
         account = Account.get(dest_transfer.self_account.key())
         account.balance += source_transfer.amount # add negative amount
         db.put([account, dest_transfer])
     return dest_transfer
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')