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