From 919bd6e480c72eb2565d559fd0067cabdf53f942 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 21 Jul 2011 15:15:17 -0400 Subject: [PATCH] Add Transaction.source and add_transaction. --- insider/models.py | 56 +++++++++++++++++++++++++++++++++++++++++++++-- insider/views.py | 3 +++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/insider/models.py b/insider/models.py index 4b8b73e..13d80cf 100644 --- a/insider/models.py +++ b/insider/models.py @@ -37,11 +37,63 @@ class Transaction (models.Model): date = models.DateTimeField() ticker = models.ForeignKey(Ticker) shares = models.IntegerField('number of shares') - value = models.DecimalField( - 'value in US dollars', max_digits=20, decimal_places=2) + # If you want to track values in several currencies, check out the # python-money package: http://code.google.com/p/python-money/ + value = models.DecimalField( + 'value in US dollars', max_digits=20, decimal_places=2) + + source = models.CharField(max_length=200) # ideally a permalink def __unicode__(self): return (u'{0.person} traded {0.shares} shares (${0.value}) ' 'of {0.ticker} on {0.date}').format(self) + + +def add_transaction(person, date, exchange, exchange_symbol, company, + company_symbol, shares, value, source): + if not value and not shares: + return # not a transaction (insider just declaring holdings?) + try: + person = Person.objects.get(name__iexact=person) + except Person.DoesNotExist: + person = Person(name=person) + print('adding new person {}'.format(person)) + person.save() + try: + exchange = Exchange.objects.get(symbol__iexact=exchange_symbol) + except Exchange.DoesNotExist: + exchange = Exchange(name=exchange, symbol=exchange_symbol) + print('adding new exchange {}'.format(exchange)) + exchange.save() + try: + tickers = Ticker.objects.filter(symbol__iexact=company_symbol) + ticker = tickers.get(exchange__id=exchange.id) + except Ticker.DoesNotExist: + try: + company = Company.objects.get(name__iexact=company) + except Company.DoesNotExist: + company = Company(name=company) + print('adding new company {}'.format(company)) + company.save() + ticker = Ticker(exchange=exchange, company=company, + symbol=company_symbol) + print('adding new ticker {}'.format(ticker)) + ticker.save() + company = ticker.company + try: + transactions = Transaction.objects.filter(person__id=person.id) + transactions = transactions.filter(ticker__id=ticker.id) + transactions = transactions.filter(date=date) + transactions = transactions.filter(shares=shares) + transaction = transactions.get(value=value) + except Transaction.DoesNotExist: + transaction = Transaction( + person=person, + date=date, + ticker=ticker, + shares=shares, + value=value, + source=source) + print('adding new transaction {}'.format(transaction)) + transaction.save() diff --git a/insider/views.py b/insider/views.py index 5f20316..5441807 100644 --- a/insider/views.py +++ b/insider/views.py @@ -67,6 +67,9 @@ class TransactionTable(Table): model = Transaction order_by = '-date' + def render_source(self, value): + return mark_safe(u'source'.format(value)) + class TransactionTableView(SingleTableMixin, ListView): model = Transaction -- 2.26.2