From 78bf4b7b8424d17eabb314cbd7dac1743c4d9846 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 6 Sep 2016 18:43:39 +0200 Subject: [PATCH 1/8] Fixed markdown api issue --- api/views/api.py | 12 ++++++++--- core/templates/core/user_account.jinja | 23 ++++++++++++++++++++ core/views/user.py | 30 +++++++++++++++++++++++++- requirements.txt | 2 ++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/api/views/api.py b/api/views/api.py index 6e7933be..37557587 100644 --- a/api/views/api.py +++ b/api/views/api.py @@ -1,14 +1,20 @@ from rest_framework.response import Response -from rest_framework.decorators import api_view +from rest_framework.decorators import api_view, renderer_classes +from rest_framework.renderers import StaticHTMLRenderer +from rest_framework.views import APIView from core.templatetags.renderer import markdown @api_view(['GET']) +@renderer_classes((StaticHTMLRenderer,)) def RenderMarkdown(request): """ Render Markdown """ - if request.method == 'GET': - return Response(markdown(request.GET['text'])) + try: + data = markdown(request.GET['text']) + except: + data = 'Error' + return Response(data) diff --git a/core/templates/core/user_account.jinja b/core/templates/core/user_account.jinja index e06c04ec..d36f5432 100644 --- a/core/templates/core/user_account.jinja +++ b/core/templates/core/user_account.jinja @@ -95,6 +95,29 @@ {% endif %} +

{% trans %}Account buyings{% endtrans %}

+ + + + + + + + + + {% for array in selling_months %} + {% for tuple in array %} + {% if tuple[0] != 0 %} + + + + + + {% endif %} + {% endfor %} + {% endfor %} + +
{% trans %}Year{% endtrans %}{% trans %}Month{% endtrans %}{% trans %}Total{% endtrans %}
{{ tuple[1].year }}{{ tuple[1]|date("E") }}{{ tuple[0] }} €
{% else %}

{% trans %}User has no account{% endtrans %}

{% endif %} diff --git a/core/views/user.py b/core/views/user.py index 41224f64..e0b5950d 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -12,7 +12,8 @@ from django.forms import CheckboxSelectMultiple from django.template.response import TemplateResponse from django.conf import settings -from datetime import timedelta +from django.utils import timezone +from datetime import timedelta, datetime, date import logging from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin @@ -316,11 +317,38 @@ class UserAccountView(UserTabsMixin, DetailView): return res raise PermissionDenied + def expense_by_month(self): + stats = [] + joined = self.object.date_joined.year + + years = datetime.now().year - joined + + years = range(0, years + 1) + months = range(1, 12) + + for y in years: + stats.append([]) + for m in months: + q = self.object.customer.buyings.filter( + date__year=joined + y, + date__month=m, + ) + stats[y].append( + ( + sum([p.unit_price * p.quantity for p in q]), + date(joined + y, m, 17) + ) + ) + + print(stats) + return stats + def get_context_data(self, **kwargs): kwargs = super(UserAccountView, self).get_context_data(**kwargs) kwargs['profile'] = self.object try: kwargs['customer'] = self.object.customer + kwargs['selling_months'] = self.expense_by_month() except: pass # TODO: add list of month where account has activity diff --git a/requirements.txt b/requirements.txt index f414561d..e9438558 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,5 @@ pyopenssl pytz djangorestframework django-phonenumber-field +django-ajax-selects +mysqlclient From a64b10776ea424cd4fd962086cc12c9fcba7e7f3 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 6 Sep 2016 21:47:15 +0200 Subject: [PATCH 2/8] Monthly user account --- core/templates/core/user_account.jinja | 130 ++++-------------- core/templates/core/user_account_detail.jinja | 102 ++++++++++++++ core/urls.py | 1 + core/views/user.py | 64 +++++++-- 4 files changed, 188 insertions(+), 109 deletions(-) create mode 100644 core/templates/core/user_account_detail.jinja diff --git a/core/templates/core/user_account.jinja b/core/templates/core/user_account.jinja index d36f5432..dfc3cdd8 100644 --- a/core/templates/core/user_account.jinja +++ b/core/templates/core/user_account.jinja @@ -1,5 +1,31 @@ {% extends "core/base.jinja" %} +{% macro monthly(obj) %} + + + + + + + + + + {% for array in obj %} + {% for tuple in array %} + {% if tuple[0] != 0 %} + {% set link=url('core:user_account_detail', user_id=profile.id, year=tuple[1].year, month=tuple[1].month) %} + + + + + + {% endif %} + {% endfor %} + {% endfor %} + +
{% trans %}Year{% endtrans %}{% trans %}Month{% endtrans %}{% trans %}Total{% endtrans %}
{{ tuple[1].year }}{{ tuple[1]|date("E") }}{{ tuple[0] }} €
+{% endmacro %} + {% block title %} {% trans user_name=profile.get_display_name() %}{{ user_name }}'s account{% endtrans %} {% endblock %} @@ -10,114 +36,16 @@

{% trans %}Amount: {% endtrans %}{{ customer.amount }} €

{% if customer.refillings.exists() %}

{% trans %}Refillings{% endtrans %}

- - - - - - - - - - - - {% for i in customer.refillings.order_by('-date').all() %} - - - - - - - {% if i.is_owned_by(user) %} - - {% endif %} - -{% endfor %} - -
{% trans %}Date{% endtrans %}{% trans %}Counter{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Amount{% endtrans %}{% trans %}Payment method{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.counter }}{{ i.operator.get_display_name() }}{{ i.amount }} €{{ i.get_payment_method_display() }}Delete
+ {{ monthly(refilling_month) }} {% endif %} {% if customer.buyings.exists() %}

{% trans %}Account buyings{% endtrans %}

- - - - - - - - - - - - - - {% for i in customer.buyings.order_by('-date').all() %} - - - - - - - - - {% if i.is_owned_by(user) %} - - {% endif %} - -{% endfor %} - -
{% trans %}Date{% endtrans %}{% trans %}Counter{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Label{% endtrans %}{% trans %}Quantity{% endtrans %}{% trans %}Total{% endtrans %}{% trans %}Payment method{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.counter }}{{ i.seller.get_display_name() }}{{ i.label }}{{ i.quantity }}{{ i.quantity * i.unit_price }} €{{ i.get_payment_method_display() }}Delete
+ {{ monthly(buyings_month) }} {% endif %} {% if customer.user.invoices.exists() %}

{% trans %}Eboutic invoices{% endtrans %}

- - - - - - - - - - {% for i in customer.user.invoices.order_by('-date').all() %} - - - - - -{% endfor %} - -
{% trans %}Date{% endtrans %}{% trans %}Items{% endtrans %}{% trans %}Amount{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }} -
    - {% for it in i.items.all() %} -
  • {{ it.quantity }} x {{ it.product_name }} - {{ it.product_unit_price }} €
  • - {% endfor %} -
-
{{ i.get_total() }} €
+ {{ monthly(invoices_month) }} {% endif %} -

{% trans %}Account buyings{% endtrans %}

- - - - - - - - - - {% for array in selling_months %} - {% for tuple in array %} - {% if tuple[0] != 0 %} - - - - - - {% endif %} - {% endfor %} - {% endfor %} - -
{% trans %}Year{% endtrans %}{% trans %}Month{% endtrans %}{% trans %}Total{% endtrans %}
{{ tuple[1].year }}{{ tuple[1]|date("E") }}{{ tuple[0] }} €
{% else %}

{% trans %}User has no account{% endtrans %}

{% endif %} diff --git a/core/templates/core/user_account_detail.jinja b/core/templates/core/user_account_detail.jinja new file mode 100644 index 00000000..5c352b3c --- /dev/null +++ b/core/templates/core/user_account_detail.jinja @@ -0,0 +1,102 @@ +{% extends "core/base.jinja" %} + +{% block title %} +{% trans user_name=profile.get_display_name() %}{{ user_name }}'s account{% endtrans %} +{% endblock %} + +{% block content %} +{% if customer %} +

{% trans %}User account{% endtrans %}

+

{% trans %}Amount: {% endtrans %}{{ customer.amount }} €

+{% if customer.refillings.exists() %} +

{% trans %}Refillings{% endtrans %}

+ + + + + + + + + + + + {% for i in customer.refillings.order_by('-date').all() %} + + + + + + + {% if i.is_owned_by(user) %} + + {% endif %} + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Counter{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Amount{% endtrans %}{% trans %}Payment method{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.counter }}{{ i.operator.get_display_name() }}{{ i.amount }} €{{ i.get_payment_method_display() }}Delete
+{% endif %} +{% if customer.buyings.exists() %} +

{% trans %}Account buyings{% endtrans %}

+ + + + + + + + + + + + + + {% for i in customer.buyings.order_by('-date').all() %} + + + + + + + + + {% if i.is_owned_by(user) %} + + {% endif %} + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Counter{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Label{% endtrans %}{% trans %}Quantity{% endtrans %}{% trans %}Total{% endtrans %}{% trans %}Payment method{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.counter }}{{ i.seller.get_display_name() }}{{ i.label }}{{ i.quantity }}{{ i.quantity * i.unit_price }} €{{ i.get_payment_method_display() }}Delete
+{% endif %} +{% if customer.user.invoices.exists() %} +

{% trans %}Eboutic invoices{% endtrans %}

+ + + + + + + + + + {% for i in customer.user.invoices.order_by('-date').all() %} + + + + + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Items{% endtrans %}{% trans %}Amount{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }} +
    + {% for it in i.items.all() %} +
  • {{ it.quantity }} x {{ it.product_name }} - {{ it.product_unit_price }} €
  • + {% endfor %} +
+
{{ i.get_total() }} €
+{% endif %} +{% else %} +

{% trans %}User has no account{% endtrans %}

+{% endif %} +

Retour

+{% endblock %} diff --git a/core/urls.py b/core/urls.py index baeae800..94767c20 100644 --- a/core/urls.py +++ b/core/urls.py @@ -37,6 +37,7 @@ urlpatterns = [ url(r'^user/(?P[0-9]+)/groups$', UserUpdateGroupView.as_view(), name='user_groups'), url(r'^user/tools/$', UserToolsView.as_view(), name='user_tools'), url(r'^user/(?P[0-9]+)/account$', UserAccountView.as_view(), name='user_account'), + url(r'^user/(?P[0-9]+)/account/(?P[0-9]+)/(?P[0-9]+)$', UserAccountDetailView.as_view(), name='user_account_detail'), url(r'^user/(?P[0-9]+)/stats$', UserStatsView.as_view(), name='user_stats'), # File views diff --git a/core/views/user.py b/core/views/user.py index e0b5950d..716a2ffd 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -11,6 +11,7 @@ from django.forms.models import modelform_factory from django.forms import CheckboxSelectMultiple from django.template.response import TemplateResponse from django.conf import settings +from django.views.generic.dates import YearMixin, MonthMixin from django.utils import timezone from datetime import timedelta, datetime, date @@ -300,24 +301,31 @@ class UserToolsView(UserTabsMixin, TemplateView): kwargs['object'] = self.request.user return kwargs -class UserAccountView(UserTabsMixin, DetailView): +class UserAccountBase(UserTabsMixin, DetailView): """ - Display a user's account + Base class for UserAccount """ + model = User pk_url_kwarg = "user_id" - template_name = "core/user_account.jinja" current_tab = "account" def dispatch(self, request, *arg, **kwargs): # Manually validates the rights - res = super(UserAccountView, self).dispatch(request, *arg, **kwargs) + res = super(UserAccountBase, self).dispatch(request, *arg, **kwargs) if (self.object == request.user or request.user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']) or request.user.is_root): return res raise PermissionDenied - def expense_by_month(self): +class UserAccountView(UserAccountBase): + """ + Display a user's account + """ + + template_name = "core/user_account.jinja" + + def expense_by_month(self, obj, calc): stats = [] joined = self.object.date_joined.year @@ -329,13 +337,13 @@ class UserAccountView(UserTabsMixin, DetailView): for y in years: stats.append([]) for m in months: - q = self.object.customer.buyings.filter( + q = obj.filter( date__year=joined + y, date__month=m, ) stats[y].append( ( - sum([p.unit_price * p.quantity for p in q]), + sum([calc(p) for p in q]), date(joined + y, m, 17) ) ) @@ -343,15 +351,55 @@ class UserAccountView(UserTabsMixin, DetailView): print(stats) return stats + def buyings_calc(self, query): + return query.unit_price * query.quantity + + def invoices_calc(self, query): + t = 0 + for it in query.items.all(): + t += it.quantity * it.product_unit_price + return t + + def refilling_calc(self, query): + return query.amount + def get_context_data(self, **kwargs): kwargs = super(UserAccountView, self).get_context_data(**kwargs) kwargs['profile'] = self.object try: kwargs['customer'] = self.object.customer - kwargs['selling_months'] = self.expense_by_month() + kwargs['buyings_month'] = self.expense_by_month( + self.object.customer.buyings, + self.buyings_calc + ) + kwargs['invoices_month'] = self.expense_by_month( + self.object.customer.user.invoices, + self.invoices_calc + ) + kwargs['refilling_month'] = self.expense_by_month( + self.object.customer.refillings, + self.refilling_calc + ) except: pass # TODO: add list of month where account has activity return kwargs + +class UserAccountDetailView(UserAccountBase, YearMixin, MonthMixin): + """ + Display a user's account for month + """ + + template_name = "core/user_account_detail.jinja" + + def get_context_data(self, **kwargs): + kwargs = super(UserAccountDetailView, self).get_context_data(**kwargs) + kwargs['profile'] = self.object + try: + kwargs['customer'] = self.object.customer + except: + pass + kwargs['tab'] = "account" + return kwargs From c0296b9adacab52f6ff82a16e4020b3efc454959 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 7 Sep 2016 01:51:34 +0200 Subject: [PATCH 3/8] Added slots on user profile --- core/templates/core/user_detail.jinja | 41 +++++++++++++++++---------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/core/templates/core/user_detail.jinja b/core/templates/core/user_detail.jinja index 55eb0870..6fa50cde 100644 --- a/core/templates/core/user_detail.jinja +++ b/core/templates/core/user_detail.jinja @@ -50,22 +50,33 @@ {# if the user is member of a club, he can view the subscription state #}

{% if get_subscriber(profile).is_subscribed() %} -{% trans subscription_end=get_subscriber(profile).subscriptions.last().subscription_end %}Subscribed until {{ subscription_end }}{% endtrans %}
-{% trans %}Account number: {% endtrans %}{{ profile.customer.account_id }}
-{% if get_subscriber(profile).tokens.exists() %} -

    -{% for i in get_subscriber(profile).tokens.all() %} -
  • {{ i }}
  • -{% endfor %} -
-{% endif %} + {% trans subscription_end=get_subscriber(profile).subscriptions.last().subscription_end %}Subscribed until {{ subscription_end }}{% endtrans %}
+ {% trans %}Account number: {% endtrans %}{{ profile.customer.account_id }}
+ {# Shows tokens bought by the user #} + {% if get_subscriber(profile).tokens.exists() %} +
{% trans %}Tokens{% endtrans %}
+
    + {% for i in get_subscriber(profile).tokens.all() %} +
  • {{ i }}
  • + {% endfor %} +
+ {% endif %} + {# Shows slots took by the user #} + {% if get_subscriber(profile).slots.exists() %} +
{% trans %}Slot{% endtrans %}
+
    + {% for i in get_subscriber(profile).slots.all() %} +
  • {{ i.get_type_display() }} - {{i.machine.launderette }}, {{ i.start_date|localtime|date("l j") }} : {{ i.start_date|localtime|time(DATETIME_FORMAT) }}
  • + {% endfor %} +
+ {% endif %} {% else %} -{% trans %}Not subscribed{% endtrans %} -{% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) %} -{% trans %}New subscription{% endtrans %} -{% endif %} -{% endif %} -

+ {% trans %}Not subscribed{% endtrans %} + {% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) %} + {% trans %}New subscription{% endtrans %} + {% endif %} + {% endif %} +

{% endif %} {% endblock %} From 61c77a7877881bb741dd78c9d664e6689ce52c39 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 7 Sep 2016 02:07:01 +0200 Subject: [PATCH 4/8] Added address on profile --- core/templates/core/user_detail.jinja | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/templates/core/user_detail.jinja b/core/templates/core/user_detail.jinja index 6fa50cde..0fa804f4 100644 --- a/core/templates/core/user_detail.jinja +++ b/core/templates/core/user_detail.jinja @@ -37,6 +37,11 @@ {{ profile.phone }}

{% endif %} + {% if profile.address %} +

+ {{ profile.address }} +

+ {% endif %}

{% if profile.promo %}

Promo {{ profile.promo }} From bca8dc039a3202e837d18b1406aa5745eca9eb12 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 7 Sep 2016 23:06:38 +0200 Subject: [PATCH 5/8] Added macros for slots and tokens display --- core/templates/core/macros.jinja | 22 +++++++++++++++++++ core/templates/core/user_detail.jinja | 19 +++------------- .../launderette/launderette_book.jinja | 9 +++----- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/core/templates/core/macros.jinja b/core/templates/core/macros.jinja index ac930514..9a048636 100644 --- a/core/templates/core/macros.jinja +++ b/core/templates/core/macros.jinja @@ -33,3 +33,25 @@ {%- endmacro %} + +{% macro show_slots(user) %} + {% if get_subscriber(user).slots.exists() %} +

{% trans %}Slot{% endtrans %}
+
    + {% for i in get_subscriber(user).slots.all() %} +
  • {{ i.get_type_display() }} - {{i.machine.launderette }}, {{ i.start_date|localtime|date("l j") }} : {{ i.start_date|localtime|time(DATETIME_FORMAT) }}
  • + {% endfor %} +
+ {% endif %} +{% endmacro %} + +{% macro show_tokens(user) %} + {% if get_subscriber(user).tokens.exists() %} +
{% trans %}Tokens{% endtrans %}
+
    + {% for i in get_subscriber(user).tokens.all() %} +
  • {{ i }}
  • + {% endfor %} +
+ {% endif %} +{% endmacro %} \ No newline at end of file diff --git a/core/templates/core/user_detail.jinja b/core/templates/core/user_detail.jinja index 0fa804f4..8bf4075c 100644 --- a/core/templates/core/user_detail.jinja +++ b/core/templates/core/user_detail.jinja @@ -1,4 +1,5 @@ {% extends "core/base.jinja" %} +{% from "core/macros.jinja" import show_slots, show_tokens %} {% block title %} {% trans user_name=profile.get_display_name() %}{{ user_name }}'s profile{% endtrans %} @@ -58,23 +59,9 @@ {% trans subscription_end=get_subscriber(profile).subscriptions.last().subscription_end %}Subscribed until {{ subscription_end }}{% endtrans %}
{% trans %}Account number: {% endtrans %}{{ profile.customer.account_id }}
{# Shows tokens bought by the user #} - {% if get_subscriber(profile).tokens.exists() %} -
{% trans %}Tokens{% endtrans %}
-
    - {% for i in get_subscriber(profile).tokens.all() %} -
  • {{ i }}
  • - {% endfor %} -
- {% endif %} + {{ show_tokens(profile) }} {# Shows slots took by the user #} - {% if get_subscriber(profile).slots.exists() %} -
{% trans %}Slot{% endtrans %}
-
    - {% for i in get_subscriber(profile).slots.all() %} -
  • {{ i.get_type_display() }} - {{i.machine.launderette }}, {{ i.start_date|localtime|date("l j") }} : {{ i.start_date|localtime|time(DATETIME_FORMAT) }}
  • - {% endfor %} -
- {% endif %} + {{ show_slots(profile) }} {% else %} {% trans %}Not subscribed{% endtrans %} {% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) %} diff --git a/launderette/templates/launderette/launderette_book.jinja b/launderette/templates/launderette/launderette_book.jinja index 6eca751a..5f3e9bc2 100644 --- a/launderette/templates/launderette/launderette_book.jinja +++ b/launderette/templates/launderette/launderette_book.jinja @@ -1,4 +1,5 @@ {% extends "core/base.jinja" %} +{% from "core/macros.jinja" import show_slots, show_tokens %} {% block title %} {% trans %}Launderette{% endtrans %} @@ -52,10 +53,6 @@ {% endfor %} +{{ show_slots(user) }} +{{ show_tokens(user) }} {% endblock %} - - - - - - From 8d060af46cda6e72730f83c0bc64b5097e94e5ef Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 8 Sep 2016 03:21:39 +0200 Subject: [PATCH 6/8] Removed wierd hack --- core/views/user.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/core/views/user.py b/core/views/user.py index 716a2ffd..dc7674cc 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -327,28 +327,22 @@ class UserAccountView(UserAccountBase): def expense_by_month(self, obj, calc): stats = [] - joined = self.object.date_joined.year - years = datetime.now().year - joined - - years = range(0, years + 1) - months = range(1, 12) - - for y in years: + for year in obj.datetimes('date', 'year', order='ASC'): stats.append([]) - for m in months: + i = 0 + for month in obj.filter(date__year=year.year).datetimes( + 'date', 'month', order='ASC'): q = obj.filter( - date__year=joined + y, - date__month=m, - ) - stats[y].append( - ( - sum([calc(p) for p in q]), - date(joined + y, m, 17) - ) + date__year=month.year, + date__month=month.month ) + stats[i].append(( + sum([calc(p) for p in q]), + month + )) + i += 1 - print(stats) return stats def buyings_calc(self, query): From 41d5a02d77ddce5a4c49c89abb94d8adec0b5e19 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 8 Sep 2016 03:38:28 +0200 Subject: [PATCH 7/8] Fixed account order and detail account view --- core/templates/core/user_account_detail.jinja | 9 ++++++--- core/views/user.py | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/core/templates/core/user_account_detail.jinja b/core/templates/core/user_account_detail.jinja index 5c352b3c..19748bd2 100644 --- a/core/templates/core/user_account_detail.jinja +++ b/core/templates/core/user_account_detail.jinja @@ -21,7 +21,8 @@ - {% for i in customer.refillings.order_by('-date').all() %} + {% for i in customer.refillings.order_by('-date').filter( + date__year=year, date__month=month) %} {{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }} {{ i.counter }} @@ -51,7 +52,8 @@ - {% for i in customer.buyings.order_by('-date').all() %} + {% for i in customer.buyings.order_by('-date').all().filter( + date__year=year, date__month=month) %} {{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }} {{ i.counter }} @@ -79,7 +81,8 @@ - {% for i in customer.user.invoices.order_by('-date').all() %} + {% for i in customer.user.invoices.order_by('-date').all().filter( + date__year=year, date__month=month) %} {{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }} diff --git a/core/views/user.py b/core/views/user.py index dc7674cc..39ff5e68 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -328,11 +328,11 @@ class UserAccountView(UserAccountBase): def expense_by_month(self, obj, calc): stats = [] - for year in obj.datetimes('date', 'year', order='ASC'): + for year in obj.datetimes('date', 'year', order='DESC'): stats.append([]) i = 0 for month in obj.filter(date__year=year.year).datetimes( - 'date', 'month', order='ASC'): + 'date', 'month', order='DESC'): q = obj.filter( date__year=month.year, date__month=month.month @@ -391,6 +391,8 @@ class UserAccountDetailView(UserAccountBase, YearMixin, MonthMixin): def get_context_data(self, **kwargs): kwargs = super(UserAccountDetailView, self).get_context_data(**kwargs) kwargs['profile'] = self.object + kwargs['year'] = self.get_year() + kwargs['month'] = self.get_month() try: kwargs['customer'] = self.object.customer except: From 2f7dc9ec1959ba8d39442504f0638081213a5f02 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 8 Sep 2016 04:15:34 +0200 Subject: [PATCH 8/8] Used lambda function in user detail --- core/views/user.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/core/views/user.py b/core/views/user.py index 39ff5e68..b239284c 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -345,18 +345,12 @@ class UserAccountView(UserAccountBase): return stats - def buyings_calc(self, query): - return query.unit_price * query.quantity - def invoices_calc(self, query): t = 0 for it in query.items.all(): t += it.quantity * it.product_unit_price return t - def refilling_calc(self, query): - return query.amount - def get_context_data(self, **kwargs): kwargs = super(UserAccountView, self).get_context_data(**kwargs) kwargs['profile'] = self.object @@ -364,7 +358,7 @@ class UserAccountView(UserAccountBase): kwargs['customer'] = self.object.customer kwargs['buyings_month'] = self.expense_by_month( self.object.customer.buyings, - self.buyings_calc + (lambda q: q.unit_price * q.quantity) ) kwargs['invoices_month'] = self.expense_by_month( self.object.customer.user.invoices, @@ -372,7 +366,7 @@ class UserAccountView(UserAccountBase): ) kwargs['refilling_month'] = self.expense_by_month( self.object.customer.refillings, - self.refilling_calc + (lambda q: q.amount) ) except: pass