From 9fff4583466f23cd7c6e09812e7d473ddae32f2a Mon Sep 17 00:00:00 2001 From: jssmk Date: Sun, 9 Apr 2017 22:12:12 +0300 Subject: [PATCH 01/17] first sketch --- project/creditor/templates/admin/table.html | 46 ++++++++++++++ .../creditor/templates/admin/table_year.html | 47 ++++++++++++++ project/creditor/templatetags/__init__.py | 1 + project/creditor/templatetags/keyvalue.py | 13 ++++ .../creditor/templatetags/month_sum_value.py | 13 ++++ project/creditor/urls-admin.py | 18 ++++++ project/creditor/views.py | 62 ++++++++++++++++++- 7 files changed, 199 insertions(+), 1 deletion(-) create mode 100755 project/creditor/templates/admin/table.html create mode 100755 project/creditor/templates/admin/table_year.html create mode 100644 project/creditor/templatetags/__init__.py create mode 100644 project/creditor/templatetags/keyvalue.py create mode 100644 project/creditor/templatetags/month_sum_value.py create mode 100644 project/creditor/urls-admin.py diff --git a/project/creditor/templates/admin/table.html b/project/creditor/templates/admin/table.html new file mode 100755 index 00000000..251dd92d --- /dev/null +++ b/project/creditor/templates/admin/table.html @@ -0,0 +1,46 @@ +{% extends "admin/base.html" %} + +{% load i18n %} + +{% block extrastyle %} + +{% endblock %} + + +{% block title %} {{ site_title|default:_('Django site admin') }}{% endblock %} + + +{% block content %} + +

Month: +

+ +

{% trans "Summary" %}

+

{% trans "Month:" %} {{ month }}-{{ year }}

+ + + + +
{% trans "Income" %}{{ income.sum }}
{% trans "Receivables" %}{{ receivables.sum }}
{% trans "Balance" %}{{ total.sum }}
+ +

{% trans "Transactions" %}

+ + + {% for list_item in object_list %} + + + + + {% endfor %} +
{{ list_item.owner }}{{ list_item.amount }}
+ +{% endblock content %} + diff --git a/project/creditor/templates/admin/table_year.html b/project/creditor/templates/admin/table_year.html new file mode 100755 index 00000000..228ded0d --- /dev/null +++ b/project/creditor/templates/admin/table_year.html @@ -0,0 +1,47 @@ +{% extends "admin/base.html" %} + +{% load i18n %} +{% load keyvalue %} + +{% block extrastyle %} + +{% endblock %} + + +{% block title %} {{ site_title|default:_('Django site admin') }}{% endblock %} + + +{% block content %} + + +

{% trans "Summary" %}

+

{% trans "Year" %} {{ year }}

+ + + + + + + + +{% for m in months %} + + + + + + +{% endfor %} +
{% trans "Month" %}{% trans "Income" %}{% trans "Receivables" %}{% trans "Total" %}
{{ m }}{{income|keyvalue:m}}{{receivables|keyvalue:m}}
+ + +{% endblock content %} + diff --git a/project/creditor/templatetags/__init__.py b/project/creditor/templatetags/__init__.py new file mode 100644 index 00000000..40a96afc --- /dev/null +++ b/project/creditor/templatetags/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/project/creditor/templatetags/keyvalue.py b/project/creditor/templatetags/keyvalue.py new file mode 100644 index 00000000..4348193b --- /dev/null +++ b/project/creditor/templatetags/keyvalue.py @@ -0,0 +1,13 @@ +from django import template + +register = template.Library() + +@register.filter +def keyvalue(month_array, month): + print("keyvalue "+str(month)) + for d in month_array: + print(d['month']) + if month == d['month']: + print("found "+str(month)+" "+str(d['sum'])) + return d['sum'] + return '' diff --git a/project/creditor/templatetags/month_sum_value.py b/project/creditor/templatetags/month_sum_value.py new file mode 100644 index 00000000..7c8e39c6 --- /dev/null +++ b/project/creditor/templatetags/month_sum_value.py @@ -0,0 +1,13 @@ +from django import template + +register = template.Library() + +@register.filter +def month_sum_value(month_array, month): + print("keyvalue "+str(month)) + for d in month_array: + print(d['month']) + if month == d['month']: + print("found "+str(month)+" "+str(d['sum'])) + return d['sum'] + return '' diff --git a/project/creditor/urls-admin.py b/project/creditor/urls-admin.py new file mode 100644 index 00000000..d5110a1b --- /dev/null +++ b/project/creditor/urls-admin.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from django.conf.urls import url +from django.contrib.auth.decorators import permission_required + +from . import views + +urlpatterns = [ + #url(r'^table/([0-9]{4})/$', permission_required('is_staff')(views.TransactionYearView), name="table"), + #url(r'^table/(?P[0-9]{4})/(?P[0-9]{2})/$', permission_required('is_staff')(views.month_view), name="table"), + + + url(r'^table/(?P[0-9]{4})/$', permission_required('is_staff')(views.TransactionYearView.as_view()), name="table"), + url(r'^table/(?P[0-9]{4})/(tag=(?P[0-9]+)|)$', permission_required('is_staff')(views.TransactionYearView.as_view()), name="table"), + + url(r'^table/(?P[0-9]{4})/(?P[0-9]{2})/$', permission_required('is_staff')(views.TransactionMonthView.as_view()), name="table"), + url(r'^table/(?P[0-9]{4})/(?P[0-9]{2})/(tag=(?P[0-9]+)|)$', permission_required('is_staff')(views.TransactionMonthView.as_view()), name="table"), + +] diff --git a/project/creditor/views.py b/project/creditor/views.py index d3ff201c..9aa42d4d 100644 --- a/project/creditor/views.py +++ b/project/creditor/views.py @@ -1,4 +1,64 @@ # -*- 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 +from django.utils import timezone +from django.db.models import Count -# Create your views here. +class TransactionMonthView(ListView): + model = Transaction + template_name = "admin/table.html" + + param_year = timezone.now().year + param_month = timezone.now().month + param_tag = 0 + def get_queryset(self): + + self.param_year = self.kwargs['year'] + self.param_month = self.kwargs['month'] + if('tag' in self.kwargs): + self.param_tag = int(self.kwargs['tag']) + + if(self.param_tag in TransactionTag.objects.values_list('pk', flat=True)): + return Transaction.objects.filter(stamp__year=self.param_year).filter(stamp__month=self.param_month).filter(tag__pk=self.param_tag) + + return Transaction.objects.filter(stamp__year=self.param_year).filter(stamp__month=self.param_month) + + + 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['total'] = self.get_queryset().aggregate(sum=Sum('amount')) + context['year'] = self.param_year + context['month'] = self.param_month + + return context + +class TransactionYearView(ListView): + model = Transaction + template_name = "admin/table_year.html" + + param_year = timezone.now().year + param_tag = 0 + def get_queryset(self): + + self.param_year = self.kwargs['year'] + if('tag' in self.kwargs): + self.param_tag = int(self.kwargs['tag']) + + #if(self.param_tag in TransactionTag.objects.values_list('pk', flat=True)): + #return Transaction.objects.filter(stamp__year=self.param_year).filter(tag__pk=self.param_tag) + + return Transaction.objects.filter(stamp__year=self.param_year)#.annotate(sum_income=Sum('amount')) + + + 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': 'extract( month from stamp )'}).values('month').annotate(sum=Sum('amount')).order_by() + context['income'] = self.get_queryset().filter(amount__gte=0).extra(select={'month': 'extract( month from stamp )'}).values('month').annotate(sum=Sum('amount')).order_by() + context['year'] = self.param_year + print(context['receivables']) + context['months'] = range(1, 13) + return context \ No newline at end of file From d71e06879f3e9ccef4ef515014a1791fa0fdef3e Mon Sep 17 00:00:00 2001 From: jssmk Date: Fri, 21 Apr 2017 22:14:36 +0300 Subject: [PATCH 02/17] aggregation etc. --- project/creditor/__views.py | 72 +++++++++++++++++++ project/creditor/templates/admin/table.html | 25 +++++-- .../creditor/templates/admin/table_year.html | 10 +-- project/creditor/templatetags/keyvalue.py | 13 ---- .../creditor/templatetags/month_sum_value.py | 8 +-- project/creditor/urls-admin.py | 15 ++-- project/creditor/views.py | 23 +++--- 7 files changed, 119 insertions(+), 47 deletions(-) create mode 100644 project/creditor/__views.py delete mode 100644 project/creditor/templatetags/keyvalue.py diff --git a/project/creditor/__views.py b/project/creditor/__views.py new file mode 100644 index 00000000..19b69d66 --- /dev/null +++ b/project/creditor/__views.py @@ -0,0 +1,72 @@ +# -*- 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 +from django.utils import timezone + + +class TransactionMonthView(ListView, year, month): + model = Transaction + template_name = "admin/table.html" +# def get(self, request, *args, **kwargs): +# print(request.GET.get('y', '')) +# print(request.GET.get('m', '')) +# return super(TransactionTableView, self).get(request, *args,**kwargs) + + param_year = timezone.now().year + param_month = timezone.now().month + param_tag = 0 + def get_queryset(self): + self.param_year = self.request.GET.get('y', timezone.now().year) + self.param_month = self.request.GET.get('m', timezone.now().month) + self.param_tag = int(self.request.GET.get('tag', 0)) + print(self.param_year) + print(self.param_month) + print(TransactionTag.objects.values_list('pk', flat=True)) + print(self.param_tag) # vain jos on jokin sellainen numero joka on käytössä + if(self.param_tag in TransactionTag.objects.values_list('pk', flat=True)): + print("tag!") + return Transaction.objects.filter(stamp__year=self.param_year).filter(stamp__month=self.param_month).filter(tag__pk=self.param_tag) + return Transaction.objects.filter(stamp__year=self.param_year).filter(stamp__month=self.param_month) + + def get_context_data(self, **kwargs): + context = super(TransactionTableView, 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['total'] = self.get_queryset().aggregate(sum=Sum('amount')) + context['year'] = self.param_year + context['month'] = self.param_month + + return context + + +class TransactionYearView(ListView): + model = Transaction + template_name = "admin/table.html" +# def get(self, request, *args, **kwargs): +# print(request.GET.get('y', '')) +# print(request.GET.get('m', '')) +# return super(TransactionTableView, self).get(request, *args,**kwargs) + + param_year = timezone.now().year + param_tag = 0 + def get_queryset(self): + self.param_year = self.request.GET.get('y', timezone.now().year) + self.param_tag = int(self.request.GET.get('tag', 0)) + print(self.param_year) + print(TransactionTag.objects.values_list('pk', flat=True)) + print(self.param_tag) # vain jos on jokin sellainen numero joka on käytössä + if(self.param_tag in TransactionTag.objects.values_list('pk', flat=True)): + print("tag!") + return Transaction.objects.filter(stamp__year=self.param_year).filter(tag__pk=self.param_tag) + return Transaction.objects.filter(stamp__year=self.param_year) + + def get_context_data(self, **kwargs): + context = super(TransactionTableView, 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['total'] = self.get_queryset().aggregate(sum=Sum('amount')) + context['year'] = self.param_year + + return context \ No newline at end of file diff --git a/project/creditor/templates/admin/table.html b/project/creditor/templates/admin/table.html index 251dd92d..d22bd802 100755 --- a/project/creditor/templates/admin/table.html +++ b/project/creditor/templates/admin/table.html @@ -20,11 +20,24 @@ {% block content %} -

Month: -

-

{% trans "Summary" %}

-

{% trans "Month:" %} {{ month }}-{{ year }}

+ +{% for y in years %} + + +{% for m in months %} +{% if y = year and m = month %} + +{% else %} + +{% endif %} +{% endfor %} + +{% endfor %} +
{{ y }}{{ m }}{{ m }}
+ + +

{% trans "Summary" %} {{ month }}-{{ year }}

@@ -37,7 +50,9 @@

{% trans "Transactions" %}

{% for list_item in object_list %} - + {% if list_item.amount < 0 %} + {% else %}{% endif %} + {% endfor %}
{% trans "Income" %}{{ income.sum }}
{% trans "Receivables" %}{{ receivables.sum }}
{{ list_item.owner }}{{ list_item.amount }}{{ list_item.amount }}+{{ list_item.amount }}{{ list_item.tag }}
diff --git a/project/creditor/templates/admin/table_year.html b/project/creditor/templates/admin/table_year.html index 228ded0d..bf0f6e5d 100755 --- a/project/creditor/templates/admin/table_year.html +++ b/project/creditor/templates/admin/table_year.html @@ -1,7 +1,7 @@ {% extends "admin/base.html" %} {% load i18n %} -{% load keyvalue %} +{% load month_sum_value %} {% block extrastyle %}