Add Transaction.source and add_transaction.
authorW. Trevor King <wking@drexel.edu>
Thu, 21 Jul 2011 19:15:17 +0000 (15:15 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 21 Jul 2011 19:15:17 +0000 (15:15 -0400)
insider/models.py
insider/views.py

index 4b8b73ec78417a2ab73bda19b51060ba2b10a6fe..13d80cf44e1ca508db37ff543e8b98088b31e99b 100644 (file)
@@ -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()
index 5f2031614493b4d04b5177dc9f20f1f4ae00511b..544180783bd48f0e4f748b21f05132933bc520f9 100644 (file)
@@ -67,6 +67,9 @@ class TransactionTable(Table):
         model = Transaction
         order_by = '-date'
 
+    def render_source(self, value):
+        return mark_safe(u'<a href="{}">source</a>'.format(value))
+
 
 class TransactionTableView(SingleTableMixin, ListView):
     model = Transaction