+from decimal import Decimal, ROUND_DOWN
from datetime import datetime, timedelta
+import re
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.utils.safestring import mark_safe
from django.views.generic import ListView
-from django_tables2 import Table, LinkColumn
+from django_tables2 import Table, Column, LinkColumn
from django_tables2.utils import A
from django_tables2.views import SingleTableMixin
from .models import Company, Person, Ticker, Transaction
-class AttrAccessor (object):
- def __init__(self, attribute, attributes):
- self._attribute = attribute
- self._attributes = attributes
- def resolve(self, context):
- return {'year': 1000}
- obj = getattr(context, self._attribute)
- print(obj)
- print(self._attributes)
- return dict((a, getattr(obj, a)) for a in self._attributes)
+class NumberColumn(Column):
+ @staticmethod
+ def _format_integer(integer, sep=','):
+ """Pretty-print an integer at thousands, millions, etc.
+
+ >>> NumberColumn._format_integer(1234567)
+ '1,234,567'
+ >>> NumberColumn._format_integer(-123456)
+ '-123,456'
+ >>> NumberColumn._format_integer(1234567, sep=' ')
+ '1 234 567'
+ """
+ sign = ''
+ if integer < 0:
+ sign = '-'
+ string = '{:d}'.format(abs(integer))
+ return sign + sep.join(re.findall('(.{1,3})', string[::-1]))[::-1]
+
+ def render(self, value):
+ classes = ['number']
+ if value < 0:
+ classes.append('negative')
+ if value > 0:
+ classes.append('positive')
+ if isinstance(value, Decimal):
+ integer = int(value.quantize(Decimal('1.'), rounding=ROUND_DOWN))
+ decimal = int(abs(value - integer) * 100)
+ print value
+ value = '{}.{:02d}'.format(self._format_integer(integer), decimal)
+ print value
+ elif isinstance(value, int):
+ value = self._format_integer(value)
+ else:
+ raise NotImplementedError(type(value))
+ return mark_safe(u'<span class="{}">{}</span>'.format(
+ u' '.join(classes), value))
class TransactionTable(Table):
'date', kwargs=dict((x, A('date.{}'.format(x)))
for x in ['year', 'month', 'day']))
ticker = LinkColumn('ticker', args=[A('ticker.pk')])
+ shares = NumberColumn()
+ value = NumberColumn()
class Meta:
model = Transaction