Skip to content
1 change: 1 addition & 0 deletions project/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# Your stuff: custom urls includes go here
url(r'^members/', include('members.urls')),
url(r'^velkoja/', include('velkoja.urls')),
url(r'^creditor/', include('creditor.urls')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be under /admin/ -urlspace somehow ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution now in f7a8e7c. Though, might not be the ideal way to handle this.


url(r'^api/', include(router.urls)),
url(r'^api-auth/get-token/', authtoken_views.obtain_auth_token),
Expand Down
99 changes: 99 additions & 0 deletions project/creditor/templates/admin/table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{% extends "admin/base.html" %}

{% load i18n %}


{% block extrastyle %}
<style>
@media print
{
.no-print, .no-print *
{
display: none !important;
}
}
</style>
{% endblock %}


{% block title %} {{ site_title|default:_('Django site admin') }}{% endblock %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs proper title

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in 82bf0ac



{% block content %}

<div style="float: right; max-width:400px">

<h3>{% trans "Filter by transaction types" %}</h3>
<span><a style="display: inline-block; margin-right: 10px;" href="{% url 'table_m' year=year month=month %}">{% trans "All types" %}</a></span>
{% for t in tags %}
{% if t.pk = tag_pk %}
<span style="border: solid 2pt red; display: inline-block; margin-right: 10px"><a href="{% url 'table_m' year=year month=month tag=t.pk|stringformat:"i" %}">{{ t.label }}</a></span>
{% else %}
<span><a style="display: inline-block; margin-right: 10px;" href="{% url 'table_m' year=year month=month tag=t.pk|stringformat:"i" %}">{{ t.label }}</a></span>
{% endif %}
{% endfor %}

<h3>{% trans "Month selection" %}</h3>
<table>
{% for y in years %}
<tr>
{% if tag %}
<th><a href="{% url 'table_y' year=y tag=tag_pk %}">{{ y }}</a></th>
{% else %}
<th><a href="{% url 'table_y' year=y %}">{{ y }}</a></th>
{% endif %}
{% for m in months %}
{% if y = year and m = month %}
<td style="border: solid 2pt red;"><a href="{% url 'table_m' year=y month=m tag=tag_pk %}">{{ m }}</a></td>
{% else %}
<td><a href="{% url 'table_m' year=y month=m tag=tag_pk %}">{{ m }}</a></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
</div>


<h1>{{ month }}-{{ year }} {{ tag.label }}</h1>

<h3>{% trans "Summary" %}</h3>
<table>
<tr><th>{% trans "Income" %}</th><td>{{ income.sum }}</td></tr>
<tr><th>{% trans "Receivables" %}</th><td>{{ receivables.sum }}</td></tr>
<tr><th>{% trans "Balance" %}</th><td>{{ total.sum }}</td></tr>
</table>


<h3>{% trans "Frequency table" %}</h3>
<table>
<tr>
<th>{% trans "Amount" %}</th>
<th>{% trans "Freq" %}</th>
</tr>
{% for f in freq_table %}
<tr>
<th>{{ f.amount }}</th>
<td>{{ f.freq }}</td>
</tr>
{% endfor %}
</table>



<h3>{% trans "Transactions" %}</h3>

<table>
{% for list_item in object_list %}
<tr>
<td>{{ list_item.owner }}</td>
{% if list_item.amount < 0 %}<td style="color: red;">{{ list_item.amount }}</td>
{% else %}<td style="color: green;">+{{ list_item.amount }}</td>{% endif %}
<td>{{ list_item.tag }}</td>
<td>{{ list_item.stamp }}</td>
</tr>
{% endfor %}
</table>

{% endblock content %}

80 changes: 80 additions & 0 deletions project/creditor/templates/admin/table_year.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{% extends "admin/base.html" %}

{% load i18n %}
{% load aggregation_utils %}

{% block extrastyle %}
<style>
@media print
{
.no-print, .no-print *
{
display: none !important;
}
}
</style>
{% endblock %}


{% block title %} {{ site_title|default:_('Django site admin') }}{% endblock %}


{% block content %}


<div style="float: right; max-width:400px">

<h3>{% trans "Filter by transaction types" %}</h3>
<span><a style="display: inline-block; margin-right: 10px;" href="{% url 'table_y' year=year %}">{% trans "All types" %}</a></span>
{% for t in tags %}
{% if t.pk = tag_pk %}
<span style="border: solid 2pt red; display: inline-block; margin-right: 10px"><a href="{% url 'table_y' year=year tag=t.pk|stringformat:"i" %}">{{ t.label }}</a></span>
{% else %}
<span><a style="display: inline-block; margin-right: 10px;" href="{% url 'table_y' year=year tag=t.pk|stringformat:"i" %}">{{ t.label }}</a></span>
{% endif %}
{% endfor %}


<h3>{% trans "Month selection" %}</h3>
<table>
{% for y in years %}
<tr>
<th><a href="{% url 'table_y' year=y tag=tag_pk %}">{{ y }}</a></th>
{% for m in months %}
<td><a href="{% url 'table_m' year=y month=m tag=tag_pk %}">{{ m }}</a></td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>

<div style="clear: left">



<h1>{{ year }} {{ tag.label }}</h1>

<h3>{% trans "Summary" %}</h3>
<p>{% trans "Year" %} {{ year }}</p>

<table border="1">
<tr>
<th>{% trans "Month" %}</th>
<th>{% trans "Income" %}</th>
<th>{% trans "Receivables" %}</th>
<th>{% trans "Total" %}</th>
</tr>
{% for m in months %}
<tr>
<th><a href="{% url 'table_m' year=year month=m tag=tag_pk %}">{{ m }}</a></th>
<td><span style="color: green;">{{income|month_sum_value:m}}</span></td>
<td><span style="color: red;">{{receivables|month_sum_value:m}}</span></td>
<td>{% if total|month_sum_value:m < 0 %}<span style="color: red;">{% else %}<span style="color: green;">{% endif %}{{total|month_sum_value:m}}</span></td>
</tr>
{% endfor %}
</table>

</div>

{% endblock content %}

1 change: 1 addition & 0 deletions project/creditor/templatetags/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
11 changes: 11 additions & 0 deletions project/creditor/templatetags/aggregation_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django import template

register = template.Library()

@register.filter
def month_sum_value(month_array, month):
for d in month_array:
if str(month) == d['month']:
return d['sum']
return '.'

14 changes: 14 additions & 0 deletions project/creditor/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from django.conf.urls import url
from django.contrib.auth.decorators import permission_required

from . import views

urlpatterns = [
url(r'^transaction_table/$', permission_required('is_staff')(views.TransactionYearView.as_view()), name="table_y"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This decorator redirects to nonexistent URL for login, probably a missing configuration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

login_url set in e059375

url(r'^transaction_table/(?P<year>[0-9]{4})/$', permission_required('is_staff')(views.TransactionYearView.as_view()), name="table_y"),
url(r'^transaction_table/(?P<year>[0-9]{4})/tag=(?P<tag>[0-9]+)$', permission_required('is_staff')(views.TransactionYearView.as_view()), name="table_y"),

url(r'^transaction_table/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', permission_required('is_staff')(views.TransactionMonthView.as_view()), name="table_m"),
url(r'^transaction_table/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/tag=(?P<tag>[0-9]+)$', permission_required('is_staff')(views.TransactionMonthView.as_view()), name="table_m"),
]
76 changes: 75 additions & 1 deletion project/creditor/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,78 @@
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.views.generic import ListView
from creditor.models import Transaction, TransactionTag
from django.db.models import Sum, Count
from django.utils import timezone

# Create your views here.

class TransactionMonthView(ListView):
model = Transaction
template_name = "admin/table.html"

param_tag = None
def get_queryset(self):

self.param_year = self.kwargs['year']
self.param_month = self.kwargs['month']

if('tag' in self.kwargs and int(self.kwargs['tag']) in TransactionTag.objects.values_list('pk', flat=True)):
#print(TransactionTag.objects.filter(pk=int(self.kwargs['tag']))[0])
self.param_tag = TransactionTag.objects.filter(pk=int(self.kwargs['tag']))[0]
return Transaction.objects.filter(stamp__year=self.param_year).filter(stamp__month=self.param_month).filter(tag__pk=self.param_tag.pk).order_by('owner__lname','owner__fname')

return Transaction.objects.filter(stamp__year=self.param_year).filter(stamp__month=self.param_month).order_by('owner__lname','owner__fname')


def get_context_data(self, **kwargs):
context = super(TransactionMonthView, self).get_context_data(**kwargs)
context['income'] = self.get_queryset().filter(amount__gte=0).aggregate(sum=Sum('amount'))
context['receivables'] = self.get_queryset().filter(amount__lt=0).aggregate(sum=Sum('amount'))
context['freq_table'] = self.get_queryset().values('amount').annotate(freq=Count('amount')).order_by('-amount')
context['total'] = self.get_queryset().aggregate(sum=Sum('amount'))
context['year'] = self.param_year
context['tag'] = self.param_tag
context['tag_pk'] = 0 if self.param_tag == None else self.param_tag.pk
#context['tag_label'] = self.param_tag.label
context['month'] = self.param_month.rjust(2, "0")
context['months'] = ['01','02','03','04','05','06','07','08','09','10','11','12']
context['years'] = [str(d.year) for d in Transaction.objects.datetimes('stamp', 'year')]
context['tags'] = TransactionTag.objects.values('pk','label').order_by('pk')

return context

class TransactionYearView(ListView):
model = Transaction
template_name = "admin/table_year.html"

param_year = timezone.now().year
param_tag = None
def get_queryset(self):

if('year' in self.kwargs):
self.param_year = self.kwargs['year']

# tag number must refer to existing TransactionTag
if('tag' in self.kwargs and int(self.kwargs['tag']) in TransactionTag.objects.values_list('pk', flat=True)):
self.param_tag = TransactionTag.objects.filter(pk=int(self.kwargs['tag']))[0]
#self.tag_label = TransactionTag.objects.filter(pk=self.param_tag)[0]
#self.param_tag = self.kwargs['tag']
return Transaction.objects.filter(stamp__year=self.param_year).filter(tag__pk=self.param_tag.pk)

return Transaction.objects.filter(stamp__year=self.param_year)


def get_context_data(self, **kwargs):
context = super(TransactionYearView, self).get_context_data(**kwargs)
context['receivables'] = self.get_queryset().filter(amount__lt=0).extra(select={'month': "to_char(stamp, 'MM' )"}).values('month').annotate(sum=Sum('amount')).order_by()
context['income'] = self.get_queryset().filter(amount__gte=0).extra(select={'month': "to_char(stamp, 'MM' )"}).values('month').annotate(sum=Sum('amount')).order_by()
context['total'] = self.get_queryset().extra(select={'month': "to_char(stamp, 'MM' )"}).values('month').annotate(sum=Sum('amount')).order_by()
context['tag'] = self.param_tag
context['tag_pk'] = 0 if self.param_tag == None else self.param_tag.pk
#context['tag_label'] = None if self.param_tag == None else self.param_tag.label
context['year'] = self.param_year
context['months'] = ['01','02','03','04','05','06','07','08','09','10','11','12']
context['years'] = [d.year for d in Transaction.objects.datetimes('stamp', 'year')]
context['tags'] = TransactionTag.objects.values('pk','label').order_by('pk')

return context