示例#1
0
文件: views.py 项目: Kaniabi/MoneyDJ
def edit_transaction(request, account, transaction):
    account = get_object_or_404(Account, pk=account, user=request.user)
    transaction = get_object_or_404(Transaction, pk=transaction, account=account)
    
    if request.method == "POST":
        form = QuickTransactionForm(request.POST)
        if int(request.POST['account']) != account.pk:
            raise Http404
        
        if form.is_valid():
            form.save(instance=transaction)
            return redirect(reverse('moneydj.accounts.views.view', args=[transaction.account.pk]))
    else:
        tags = u''
        # Build the tag field
        for tl in transaction.taglink_set.all():
            tags = tags + tl.tag.name
            if tl.split != transaction.amount:
                tags = tags + u':' + unicode(str(abs(tl.split)))
            tags = tags + u' '
            
        tags = tags.strip()
        data = {
            'date': transaction.date,
            'payee': transaction.payee.name,
            'amount': abs(transaction.amount),
            'credit': int(transaction.amount > 0),
            'transfer': transaction.transfer,
            'tags': tags,
            'account': account.pk,
            'comment': transaction.comment
        }
        form = QuickTransactionForm(initial=data)

    return render_to_response('transaction_edit.html', { 'form': form, 'account': account, 'transaction': transaction }, context_instance=RequestContext(request))
示例#2
0
文件: views.py 项目: Kaniabi/MoneyDJ
def view(request, id):
    """Lists the transactions in an account"""
    acc = get_object_or_404(Account, pk=id, user=request.user)

    # "Add Transaction" form
    if request.method == 'POST':
        if 'account' not in request.POST.keys() or request.POST['account'] != id:
            return HttpResponseBadRequest()
        
        transaction_form = QuickTransactionForm(request.POST)
        if (transaction_form.is_valid()):
            transaction = transaction_form.save()
            messages.add_message(request, messages.SUCCESS, _('Your transaction was succesfully added'))
            acc = transaction.account
            transaction_form = QuickTransactionForm(initial={ 'account': acc.pk, 'user': request.user.pk })
    else:
        transaction_form = QuickTransactionForm(initial={ 'account': acc.pk, 'user': request.user.pk })

    # Get all the transactions
    transactions = Transaction.objects.select_related().filter(account=acc).order_by('-date', '-date_created')
    
    paginator = Paginator(transactions, 20)
    
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
        
    try:
        transactions = paginator.page(page)
    except (EmptyPage, InvalidPage):
        transactions = paginator.page(paginator.num_pages)


    return render_to_response('account_view.html', {
        'account': acc,
        'transactions': transactions,
        'transaction_form': transaction_form,
    }, context_instance=RequestContext(request))
示例#3
0
        tags = ' '.join(
            random.sample(payee['tags'], random.randint(1,
                                                        len(payee['tags']))))

        if 'credit' in payee.keys():
            credit = payee['credit']
        else:
            credit = 0

        data = {
            'payee':
            payee['name'],
            'amount':
            Decimal('%.2f' % (random.randint(payee['min'], payee['max']) +
                              round(random.random(), 2))),
            'tags':
            tags,
            'date':
            random_date(min_date, max_date),
            'account':
            account.pk,
            'credit':
            credit
        }

        form = QuickTransactionForm(data=data)

        if form.is_valid():
            form.save()
        else:
            print 'ERROR creating transaction:', ', '.join(form.errors)
示例#4
0
print "Min date:", min_date

for account in Account.objects.select_related().all():
    t_count = random.randint(50, 100)
    print 'Creating %d transactions for' % t_count, account.name
    
    for i in range(0, t_count):
        payee = weighted_choice(payees)
        
        tags = ' '.join(random.sample(payee['tags'], random.randint(1, len(payee['tags']))))
        
        if 'credit' in payee.keys():
            credit = payee['credit']
        else:
            credit = 0
        
        data = {
            'payee': payee['name'],
            'amount': Decimal('%.2f' % (random.randint(payee['min'], payee['max']) + round(random.random(), 2))),
            'tags': tags,
            'date': random_date(min_date, max_date),
            'account': account.pk,
            'credit': credit
        }
        
        form = QuickTransactionForm(data=data)
        
        if form.is_valid():
            form.save()
        else:
            print 'ERROR creating transaction:', ', '.join(form.errors)