コード例 #1
0
ファイル: views.py プロジェクト: rneupane/tracker
def addSecurityTransaction(request, portfolio_id):
    # add new security
    # update average price.
    BUYING = True
    if not request.POST:
        form = SecurityTransactionForm()
        return render_to_response('manager/addSecurityTransaction.html', {'form':form, 'p_id':portfolio_id},context_instance=RequestContext(request))
    
    ajax_return = {}
    success = False
    
    newForm = SecurityTransactionForm(request.POST)
    if newForm.is_valid():
        try:
            portfolio = Portfolio.objects.get(pk=portfolio_id)
            newSecurity = newForm.save(commit=False)

            if newSecurity.type.name == "Sell":
                BUYING = False
            
            symbol = string.upper(newForm.cleaned_data['symbol'])
            securityObj, created = Security.objects.get_or_create(symbol=symbol)
            newSecurity.portfolio = portfolio
            newSecurity.security = securityObj
            newSecurity.unit_price = newSecurity.centicize(newSecurity.unit_price)
            newSecurity.amount = newSecurity.units * newSecurity.unit_price
            newSecurity.commission = newSecurity.centicize(newSecurity.commission)
            
            if newSecurity.commission > 0:
                if BUYING:
                    newSecurity.amount += newSecurity.commission
                else:
                    newSecurity.amount -= newSecurity.commission
            
            newSecurity.save()
            
            # update holding.
            count = PortfolioHolding.objects.filter(portfolio=portfolio, securities=newSecurity.security).count()
            if count == 0:
                ph = PortfolioHolding(portfolio=portfolio, securities=securityObj, held=newSecurity.units, average_price=newSecurity.unit_price, last_price=newSecurity.unit_price)
                print 'created new ph'
            else:
                ph = PortfolioHolding.objects.get(portfolio=portfolio, securities=newSecurity.security)
                #ph.updateHolding(newSecurity)
            ph.save()
            
            # update cash
            cash_obj, created = Security.objects.get_or_create(symbol='CASH')
            c_count = PortfolioHolding.objects.filter(portfolio=portfolio, securities=cash_obj).count()
            if c_count == 0:
                ch = PortfolioHolding()
                ch.portfolio = portfolio
                ch.securities = cash_obj
                ch.average_price = 100
                ch.last_price = 100
                if BUYING:
                    ch.held = -newSecurity.amount
            else:
                ch = PortfolioHolding.objects.get(portfolio=portfolio, securities=cash_obj)
                print 'previously held:', ch.held
                if BUYING:
                    ch.held -= newSecurity.amount
                    print 'after buying:', newSecurity.amount, ": ", ch.held
                else:
                    ch.held += newSecurity.amount
                    print 'after selling:', newSecurity.amount, ": ", ch.held

            ch.save()
            success = True
            msg = u"Added new transaction. %s %d shares of %s!" %( newSecurity.type.name, newSecurity.units, newSecurity.security.symbol)
            ajax_return['ok'] = success
            ajax_return['msg'] = msg
        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:
            return HttpResponse(serialized, mimetype="application/json")
        else:
            return HttpResponseServerError(serialized, mimetype="application/json")
    else:
        if success:
            print "success..."
            return HttpResponseRedirect('/tracker/portfolios/'+portfolio_id)
        else:
            print "failed..."
            return render_to_response('manager/addSecurityTransaction.html', {'form':newForm,'p_id':portfolio_id}, context_instance=RequestContext(request))
コード例 #2
0
ファイル: views.py プロジェクト: rneupane/tracker
def editSecurityTransaction(request, transaction_id=None):
    transaction = None
    portfolio_id = request.session.get('p_id')
    BUYING = True
    # get object if id provided.    
    if transaction_id is not None:
        transaction = get_object_or_404(Transaction, pk=transaction_id)
        # share price in dollars.
        transaction.unit_price = transaction.dollarize(transaction.unit_price)
        transaction.commission = transaction.dollarize(transaction.commission)
        transaction.symbol = transaction.security.symbol
        # commission in dollars.
        
    if request.method == 'POST':
        # form submitted.
        new_transaction_form = SecurityTransactionForm(request.POST, instance=transaction)
        if new_transaction_form.is_valid():
            if newSecurity.type.name == "Sell":
                BUYING = False            
            
            new_transaction = new_transaction_form.save(commit=False)
            new_transaction.portfolio = Portfolio.objects.get(pk=portfolio_id)
            
            securityObj, created = Security.objects.get_or_create(symbol=symbol)
            new_transaction.portfolio = portfolio
            new_transaction.security = securityObj
            new_transaction.unit_price = new_transaction.centicize(new_transaction.unit_price)
            new_transaction.amount = new_transaction.units * new_transaction.unit_price
            new_transaction.commission = new_transaction.centicize(new_transaction.commission)
            
            if new_transaction.commission > 0:
                if BUYING:
                    new_transaction.amount += new_transaction.commission
                else:
                    new_transaction.amount -= new_transaction.commission
            
            new_transaction.save()
            
            # update holding.
            count = PortfolioHolding.objects.filter(portfolio=portfolio, securities=new_transaction.security).count()
            if count == 0:
                ph = PortfolioHolding(portfolio=portfolio, securities=securityObj, held=new_transaction.units, average_price=new_transaction.unit_price, last_price=new_transaction.unit_price)
                print 'created new ph'
            else:
                ph = PortfolioHolding.objects.get(portfolio=portfolio, securities=new_transaction.security)
                #ph.updateHolding(newSecurity)
            ph.save()
            
            # update cash
            cash_obj, created = Security.objects.get_or_create(symbol='CASH')
            c_count = PortfolioHolding.objects.filter(portfolio=portfolio, securities=cash_obj).count()
            if c_count == 0:
                ch = PortfolioHolding()
                ch.portfolio = portfolio
                ch.securities = cash_obj
                ch.average_price = 100
                ch.last_price = 100
                if BUYING:
                    ch.held = -new_transaction.amount
            else:
                ch = PortfolioHolding.objects.get(portfolio=portfolio, securities=cash_obj)
                print 'previously held:', ch.held
                if BUYING:
                    ch.held -= new_transaction.amount
                    print 'after buying:', new_transaction.amount, ": ", ch.held
                else:
                    ch.held += new_transaction.amount
                    print 'after selling:', new_transaction.amount, ": ", ch.held

            ch.save()
            return HttpResponseRedirect('/tracker/portfolios/'+portfolio_id)
    else:
        #empty form        
        if transaction is None:
            form = SecurityTransactionForm(instance=transaction)
        else:
            form = SecurityTransactionForm(symbol=transaction.symbol, instance=transaction)
                               
    return render_to_response('manager/addSecurityTransaction.html', {'form':form, 'p_id':portfolio_id},context_instance=RequestContext(request))