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))
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))
def edit_transaction(request, t_id=None): """ edit transaction. takes transaction id as t_id """ transaction = None portfolio = None symbol = None old_type = None old_amnt = 0 old_security = None new_security = None if t_id is not None: #print 'transaction id is: ', t_id transaction = get_object_or_404(Transaction, pk=t_id) portfolio = Portfolio.objects.get(pk=transaction.portfolio.id) symbol = transaction.security.symbol old_type = transaction.type.name old_amnt = transaction.amount old_security = transaction.security transaction.set_values_in_dollars() if request.method == 'POST': form = None # create form. new_tran_type = TransactionType.objects.get(pk=request.POST['type']) if new_tran_type.name in ('Withdraw', 'Deposit'): # cash transaction create cash form form = CashTransactionForm(request.POST,instance=transaction) elif new_tran_type.name in ('Buy', 'Sell'): # security form. initial so symbol gets passed through. form = SecurityTransactionForm(request.POST, initial={'symbol':symbol}, instance=transaction) else: pass if form.is_valid(): # commit=False so can alter values before saving. new_transaction = form.save(commit=False) new_transaction.portfolio = portfolio if new_transaction.type.name in ('Deposit', 'Withdraw'): # for cash transaction. security does not change. new_transaction.security = old_security new_transaction.units = new_transaction.amount new_transaction.unit_price = 1 elif new_transaction.type.name in ('Buy', 'Sell'): # security could have been changed. new_security = Security.objects.get(symbol=form.cleaned_data['symbol']) if old_security.symbol != new_security.symbol: # new security entered. new_transaction.security = new_security else: pass new_transaction.set_values_in_cents() #also calculates amount # adjust previous amnt. print 'prev_type:', old_type print 'prev_amt:', old_amnt if old_type in ('Sell', 'Deposit'): # if money in MUST subtract to undo. old_amnt = -old_amnt update_cash_holding(portfolio, old_amnt) # adjust w/ new amnt. cash = new_transaction.amount print 'new_type:', new_transaction.type.name print 'new_amt:', cash if new_transaction.type.name in ('Sell', 'Deposit'): # money in add. update_cash_holding(portfolio, cash) elif new_transaction.type.name in ('Buy', 'Withdraw'): # money out subtract. update_cash_holding(portfolio, -cash) else: pass new_transaction.save() if new_transaction.type.name in ('Buy', 'Sell'): update_holdings(portfolio, new_transaction.security) # all done. return HttpResponsePermanentRedirect('/tracker/portfolios/summary/') else: print 'form is not valid... why?' else: form = None if transaction.type.name in ('Deposit', 'Withdraw'): form = CashTransactionForm(instance=transaction) elif transaction.type.name in ('Buy', 'Sell'): if transaction is not None: form = SecurityTransactionForm(initial={'symbol':symbol}, instance=transaction) else: pass return render_to_response('manager/portfolio/add_transaction.html', {'form':form}, context_instance=RequestContext(request) )