Subscriptions stats optimisations + form for start and end date

This commit is contained in:
2017-06-06 23:27:57 +02:00
parent 1c05671784
commit 88762c492f
3 changed files with 162 additions and 110 deletions

View File

@ -6,17 +6,27 @@
{% block content %}
{% trans %}Total subscriptions{% endtrans %} : {{ subscriptions_total.count() }}<br><br>
{% trans %}Subscriptions by type{% endtrans %}<br><br>
{% for location in locations %}
{{ location[1] }} {{ subscriptions_total.filter(location=location[0]).count() }}<br>
{% endfor %}
<p>
{{ form.start_date.label }}<br>
{{ form.start_date }}<br><br>
{{ form.end_date.label }}<br>
{{ form.end_date }}<br>
<p><input type="submit" value="{% trans %}Go{% endtrans %}" /></p>
</p>
<p>
{% trans %}Total subscriptions{% endtrans %} : {{ subscriptions_total.count() }}<br><br>
{% trans %}Subscriptions by type{% endtrans %}<br><br>
{% for location in locations %}
{{ location[1] }} : {{ subscriptions_total.filter(location=location[0]).count() }}<br>
{% endfor %}
<p>
<br>
<table>
<tr>
<th>{% trans %}Subscripton type{% endtrans %}</th>
<th>{% trans %}Subscription type{% endtrans %}</th>
{% for location in locations %}
<th>{{ location[1] }}</th>
{% endfor %}
@ -24,15 +34,17 @@
{% for type in subscriptions_types %}
<tr>
<td>{{ subscriptions_types[type]['name'] }}</td>
{% set subscriptions_total_type = subscriptions_total.filter(subscription_type=type) %}
{% for location in locations %}
<td>
{% trans %}Total{% endtrans %} : {{ subscriptions_total.filter(subscription_type=type, location=location[0]).count()}}<br>
{% set subscriptions_total_type_location = subscriptions_total_type.filter(location=location[0]) %}
{% trans %}Total{% endtrans %} : {{ subscriptions_total_type_location.count()}}<br>
{% for p_type in payment_types %}
{{ p_type[1] }} : {{ subscriptions_total.filter(subscription_type=type, location=location[0], payment_method=p_type[0]).count()}}<br>
{{ p_type[1] }} : {{ subscriptions_total_type_location.filter(payment_method=p_type[0]).count()}}<br>
{% endfor %}
</td>
{% endfor %}
<td>{{subscriptions_total.filter(subscription_type=type).count()}}
<td>{{subscriptions_total_type.count()}}
</tr>
{% endfor %}
</table>

View File

@ -22,12 +22,10 @@
#
#
from django.shortcuts import render
from django.views.generic.edit import UpdateView, CreateView
from django.views.generic.base import View
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView, FormView
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import PermissionDenied, ValidationError
from django.core.urlresolvers import reverse_lazy
from django.db import IntegrityError
from django import forms
from django.forms import Select
@ -38,8 +36,21 @@ import random
from subscription.models import Subscription
from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin
from core.views.forms import SelectDateTime
from core.models import User
class SelectionDateForm(forms.Form):
def __init__(self, *args, **kwargs):
super(SelectionDateForm, self).__init__(*args, **kwargs)
self.fields['start_date'] = forms.DateTimeField(
['%Y-%m-%d %H:%M:%S'], label=_("Start date"),
widget=SelectDateTime, required=True)
self.fields['end_date'] = forms.DateTimeField(
['%Y-%m-%d %H:%M:%S'], label=_("End date"),
widget=SelectDateTime, required=True)
class SubscriptionForm(forms.ModelForm):
class Meta:
model = Subscription
@ -116,21 +127,47 @@ class NewSubscription(CreateView):
return super(NewSubscription, self).form_valid(form)
class SubscriptionsStatsView(TemplateView):
class SubscriptionsStatsView(FormView):
template_name = "subscription/stats.jinja"
form_class = SelectionDateForm
def dispatch(self, request, *arg, **kwargs):
res = super(SubscriptionsStatsView, self).dispatch(request, *arg, **kwargs)
import datetime
self.start_date = datetime.datetime.today()
self.end_date = self.start_date
res = super(SubscriptionsStatsView, self).dispatch(
request, *arg, **kwargs)
if request.user.is_root or request.user.is_board_member:
return res
raise PermissionDenied
def post(self, request, *args, **kwargs):
self.form = self.get_form()
self.start_date = self.form['start_date']
self.end_date = self.form['end_date']
res = super(SubscriptionsStatsView, self).post(
request, *args, **kwargs)
if request.user.is_root or request.user.is_board_member:
return res
raise PermissionDenied
def get_initial(self):
init = {
'start_date': self.start_date.strftime('%Y-%m-%d %H:%M:%S'),
'end_date': self.end_date.strftime('%Y-%m-%d %H:%M:%S')
}
return init
def get_context_data(self, **kwargs):
from subscription.models import Subscription
import datetime
kwargs = super(SubscriptionsStatsView, self).get_context_data(**kwargs)
kwargs['subscriptions_total'] = Subscription.objects.filter(subscription_end__gte=datetime.datetime.today())
kwargs['subscriptions_total'] = Subscription.objects.filter(
subscription_end__gte=self.end_date,
subscription_start__lte=self.start_date)
kwargs['subscriptions_types'] = settings.SITH_SUBSCRIPTIONS
kwargs['payment_types'] = settings.SITH_COUNTER_PAYMENT_METHOD
kwargs['locations'] = settings.SITH_SUBSCRIPTION_LOCATIONS
return kwargs
def get_success_url(self, **kwargs):
return reverse_lazy('subscriptions:stats')