Exemple #1
0
def addCashTransaction(request, portfolio_id):
    if not request.POST:
        form = CashTransactionForm()
        return render_to_response('manager/addCashTransaction.html', {'form':form, 'p_id':portfolio_id},context_instance=RequestContext(request))
    
    ajax_return = {}
    
    newCashTransaction = CashTransactionForm(request.POST)
    success = False
    if newCashTransaction.is_valid():
        try:
            newCash = newCashTransaction.save(commit=False)
            newCash.portfolio = Portfolio.objects.get(pk=portfolio_id)
            newCash.security, created = Security.objects.get_or_create(symbol="CASH")
            newCash.units = newCash.amount
            newCash.amount = newCash.amount * 100
            newCash.unit_price = 100
            newCash.save()
            # update holding.
            count = PortfolioHolding.objects.filter(portfolio=newCash.portfolio, securities=newCash.security).count()
            if count == 0:
                ph = PortfolioHolding(portfolio=newCash.portfolio, securities=newCash.security, held=0, average_price=100, last_price=100)
            else:
                ph = PortfolioHolding.objects.get(portfolio=newCash.portfolio, securities=newCash.security)

            ph.updateHolding(newCash)
            ph.save()
                        
            success = True
            msg = u"Added new transaction. %s $ %f to portfolio!" %( newCash.type.name, newCash.units )
            ajax_return['msg'] = msg
            ajax_return['ok'] = success
        except:
            success = False;
            ajax_return['msg'] = u"Cannot add transaction."
            ajax_return['ok'] = success
    else:
        ajax_return['msg'] = u"Required Fields Missing."
            
    if request.is_ajax():
        serialized = simplejson.dumps(ajax_return)
        if success == True:
            return HttpResponse(serialized, mimetype="application/json")
        else:
            return HttpResponseServerError(serialized, mimetype="application/json")
    else:
        if success == True:
            return HttpResponseRedirect('/tracker/portfolios/'+portfolio_id)
        else:
            return render_to_response('manager/addCashTransaction.html', {'form':newCashTransaction,'p_id':portfolio_id},context_instance=RequestContext(request))
Exemple #2
0
def editCashTransaction(request, transaction_id=None):
    transaction = None
    portfolio_id = request.session.get('p_id')
    old_transaction_amount = None
    # get object if id provided.    
    if transaction_id is not None:
        transaction = get_object_or_404(Transaction, pk=transaction_id)
        #display in dollars.
        old_transaction_amount = transaction.amount
        transaction.amount = transaction.amount / 100
        
    # POST -> coming from form. either update or new.
    if request.method == 'POST':
        new_transaction_form = CashTransactionForm(request.POST, instance=transaction)
        if new_transaction_form.is_valid():
            new_transaction = new_transaction_form.save(commit = False)
            new_transaction.portfolio = Portfolio.objects.get(pk=portfolio_id)
            new_transaction.security, created = Security.objects.get_or_create(symbol="CASH")
            new_transaction.units = new_transaction.amount
            new_transaction.amount = new_transaction.amount * 100 # store in cents.
            new_transaction.unit_price = 100 #100 cents. 1$
            new_transaction.save()
            
            # update holding only if amount has changed.
            if old_transaction_amount != new_transaction.amount:
                count = PortfolioHolding.objects.filter(portfolio=new_transaction.portfolio, security=new_transaction.security).count()
                if count == 0:
                    ph = PortfolioHolding(portfolio=new_transaction.portfolio, held=0, average_price=100, last_price=100)
                else:
                    ph = PortfolioHolding.objects.get(portfolio=new_transaction.portfolio)

                ph.updateHolding(new_transaction)
                ph.save()
                # only now can add security.
                # ph.securities.add(new_transaction.security)
            return HttpResponseRedirect('/tracker/portfolios/'+portfolio_id)
    else:
        form = CashTransactionForm(instance = transaction)
    return render_to_response('manager/addCashTransaction.html', {'form':form, 'p_id':portfolio_id}, context_instance=RequestContext(request))