Commits

Dan Connolly http://www.w3.org/People/Connolly/  committed 43dce41

tab-separated values export view runs, though not correctly

  • Participants
  • Parent commits f857faa

Comments (0)

Files changed (5)

File qfm/templates/export.tsv

+{% load financial %}							
+Date	Account	Num	Description	Memo	Category	Clr	Amount
+							
+			BALANCE {{ frm|date:"d/M/y" }}				0.00
+							
+{% for t in transactions %}
+{{ t.date|date:"d/M/y" }}	{{ t.acct.name }}	@@num	{{ t.payee }}	{% for s in t.split_set.all %}{%ifequal s.acct.kind "AL"%}[{{s.acct.name|escape}}]{%endifequal%}{%ifnotequal s.acct.kind "AL"%}{{s.acct.name|escape}}{%endifnotequal%}{%if s.job %}/{{s.job.name|escape}}{%endif%}	{{ s.clr }} {{ s.subtot|currency }}
+{% endfor %}{% endfor %}
+
+			TOTAL {{ frm|date:"d/M/y" }} - {{ frm|date:"d/M/y" }}				@@
+							
+			BALANCE {{ frm|date:"d/M/y" }}				@@
+							
+							
+			TOTAL INFLOWS				@@
+			TOTAL OUTFLOWS				@@
+							
+			NET TOTAL				@@

File qfm/templatetags/__init__.py

Empty file added.

File qfm/templatetags/financial.py

+def currency(value, args=""):
+    """
+    >>> currency(-11400.01)
+    '-11,400.01'
+
+    >>> currency(10)
+    '10.00'
+    >>> currency(2.3)
+    '2.30'
+    """
+    s = "%.2f" % value
+    n = value < 0 and 1 or 0
+    i = 6
+    while i < len(s) - n:
+        s = s[:-i] + "," + s[-i:]
+        i += 4
+    return s
+
+def _test():
+    import doctest
+    doctest.testmod()
+
+if __name__ == '__main__':
+    _test()
+else:
+    from django import template
+    register = template.Library()
+
+    register.filter('currency', currency)
+
 urlpatterns = patterns('',
     (r'^$', 'dm93data.qfm.views.accounts'),
     (r'^register/(?P<acct_id>\d+)', 'dm93data.qfm.views.register'),
+    (r'^export', 'dm93data.qfm.views.export'),
 )

File qfm/views.py

 # based on
 # http://www.djangoproject.com/documentation/0.96/tutorial03/
 
+import datetime
+
 from django.shortcuts import render_to_response
 from dm93data.qfm.models import Account, Transaction
+from django.http import HttpResponse
+from django.template import loader
 
 def accounts(request):
     accounts = Account.objects.filter(kind="AL")
                               {'account': account,
                                'balance': bal,
                                'transactions': transactions})
+
+def asDate(s):
+    y = int(s[:4])
+    m = int(s[5:7])
+    d = int(s[8:10])
+    return datetime.date(y, m, d)
+
+def export(request):
+    frm, to = asDate(request.GET['from']), asDate(request.GET['to'])
+    transactions = Transaction.objects.filter(date__range=(frm, to)) \
+                   .order_by('date')
+    body = loader.render_to_string('export.tsv',
+                                   {'transactions': transactions,
+                                    'frm': frm,
+                                    'to': to
+                                    })
+    return HttpResponse(body, mimetype="text/plain")
+