Example #1
0
def unread_wallet(request):
    if request.user.is_authenticated():
        unread = cache.get('%s-unread_wallet' % request.user.id)
        if unread == None:
            unread = reset_wallet_cache(request)
    else:
        unread = False
    return {'unread_wallet': unread}
Example #2
0
def wallet(request):
    wallet = request.user.wallet
    incomes = request.user.incomes.order_by('-add_time')
    transactions = request.user.transactions.order_by('-add_time')
    
    results = []
    for income in incomes:
        t = {
            'add_time': income.add_time,
            'title': income.trade.book_copy.book.title,
            'trade_id': income.trade.id,
            'from': income.buyer.profile,
            'to': 'Account',
            'amount': '+%s' % income.earning,
            'balance': income.seller_balance,
            'type': income.trade.get_type(),
            'trans_type': 4
        }
        results.append(t)
    
    for transaction in transactions:
        trans_type = transaction.type
        t = {
            'add_time': transaction.add_time,
            'balance': transaction.buyer_balance,
            'deposit': transaction.buyer_deposit,
            'trans_type': trans_type,
        }
        if trans_type == 1: # deposit
            t['title'] = 'Card Swipe'
            t['trade_id'] = None
            t['from'] = 'Card'
            t['to'] = 'Account'
            t['amount'] = "+%s" % transaction.earning
        elif trans_type == 2: # withdraw (aka cash out)
            t['title'] = 'Cash Out'
            t['trade_id'] = None
            t['from'] = 'Account'
            t['to'] = 'Check'
            t['amount'] = "-%s" % transaction.payment
        elif trans_type == 3: # deposit return to balance
            t['title_text'] = 'Deposit return for: '
            t['title'] = transaction.trade.book_copy.book.title
            t['trade_id'] = transaction.trade.id
            t['from'] = 'Deposit'
            t['to'] = 'Account'
            t['amount'] = '+%s' % -transaction.deposit
        elif trans_type == 0: # transfer
            t['title'] = transaction.trade.book_copy.book.title
            t['trade_id'] = transaction.trade.id
            if transaction.trade.get_type() == 'sale':
                t['type'] = 'sale'
                t['from'] = 'Account'
                t['to'] = transaction.seller.profile
                t['amount'] = '-%s' % transaction.payment
            else:
                t['type'] = 'lease'
                t['from_top'] = 'Account'
                t['to_top'] = transaction.seller.profile
                t['from_bot'] = 'Account'
                t['to_bot'] = 'Deposit'
                t['amount_top'] = '-%s' % transaction.payment
                t['amount_bot'] = '-%s' % transaction.deposit
        results.append(t)
    
    results = sorted(results, key=itemgetter('add_time'))
    results.reverse()
    
    request.user.wallet.unread = False
    request.user.wallet.save()
    reset_wallet_cache(request)
    
    args = {'wallet': request.user.wallet, 'results': results}
    return render_to_response('wallet/wallet_credits.html', args, context_instance=RequestContext(request))