Commit 969d5699 authored by Skia's avatar Skia 🤘
Browse files

Merge branch 'subscriptions' into 'master'

Subscriptions stats

Des « statistiques » de cotisations
C'est pratique

See merge request !78
parents e7f7c575 801d287c
Pipeline #1027 passed with stage
in 4 minutes and 14 seconds
This diff is collapsed.
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
{% endif %} {% endif %}
{% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_root %} {% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_root %}
<li><a href="{{ url('subscription:subscription') }}">{% trans %}Subscriptions{% endtrans %}</a></li> <li><a href="{{ url('subscription:subscription') }}">{% trans %}Subscriptions{% endtrans %}</a></li>
<li><a href="{{ url('subscription:stats') }}">{% trans %}Subscription stats{% endtrans %}</a></li>
<li><a href="{{ url('club:club_new') }}">{% trans %}New club{% endtrans %}</a></li> <li><a href="{{ url('club:club_new') }}">{% trans %}New club{% endtrans %}</a></li>
{% endif %} {% endif %}
</ul> </ul>
......
This diff is collapsed.
...@@ -22,13 +22,10 @@ ...@@ -22,13 +22,10 @@
# #
# #
from django.conf.urls import url, include from django.conf.urls import url
from rootplace.views import * from rootplace.views import *
urlpatterns = [ urlpatterns = [
url(r'^merge$', MergeUsersView.as_view(), name='merge'), url(r'^merge$', MergeUsersView.as_view(), name='merge'),
] ]
...@@ -31,6 +31,7 @@ from django.core.exceptions import PermissionDenied ...@@ -31,6 +31,7 @@ from django.core.exceptions import PermissionDenied
from ajax_select.fields import AutoCompleteSelectField from ajax_select.fields import AutoCompleteSelectField
from core.views import CanViewMixin
from core.models import User from core.models import User
from counter.models import Customer from counter.models import Customer
......
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Subscription stats{% endtrans %}
{% endblock %}
{% block head %}
{{ super() }}
<script type="text/javascript" src="{{ static('core/js/Chart.bundle.min.js') }}"></script>
{% endblock %}
{% block content %}
<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>
<canvas id="statsChart" width="400" height="200"></canvas>
<p>
{% trans %}Total subscriptions{% endtrans %} : {{ subscriptions_total.count() }}<br><br>
{% trans %}Subscriptions by type{% endtrans %}<br><br>
{% for location in locations %}
{{ location[1] }} : <i class="nb">{{ subscriptions_total.filter(location=location[0]).count() }}</i><br>
{% endfor %}
<p>
<br>
<table>
<tr>
<th>{% trans %}Subscription type{% endtrans %}</th>
{% for location in locations %}
<th>{{ location[1] }}</th>
{% endfor %}
<th id="graphLabel">{% trans %}Total{% endtrans %}</th>
{% for type in subscriptions_types %}
<tr>
<td><i class="types" >{{ subscriptions_types[type]['name'] }}</i></td>
{% set subscriptions_total_type = subscriptions_total.filter(subscription_type=type) %}
{% for location in locations %}
<td>
{% 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] }} : <i class="nb">{{ subscriptions_total_type_location.filter(payment_method=p_type[0]).count()}}</i><br>
{% endfor %}
</td>
{% endfor %}
<td class="total"><i class="nb">{{subscriptions_total_type.count()}}</i>
</tr>
{% endfor %}
</table>
{% endblock %}
{% block script %}
{{ super() }}
<script>
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function getRandomColorUniq(list) {
var color = getRandomColor();
while (list.includes(color)){
color = getRandomColor();
}
return color;
}
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
var ctx = document.getElementById("statsChart").getContext('2d');
var labels = [];
var total = [];
var colors = [];
var colors_dimmed = [];
Array.from(document.getElementsByClassName("types")).forEach(function(element){
labels.push(element.childNodes[0].data);
});
Array.from(document.getElementsByClassName("total")).forEach(function(element){
total.push(element.childNodes[0].childNodes[0].data);
});
labels.forEach(function(element){
colors.push(getRandomColorUniq(colors));
});
colors.forEach(function(element){
var rgb_color = hexToRgb(element);
colors_dimmed.push('rgba(' + rgb_color.r + ', ' + rgb_color.g + ', ' + rgb_color.b + ', 0.2)');
});
console.log(colors);console.log(colors_dimmed);
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: document.getElementById("graphLabel").childNodes[0].data,
data: total,
backgroundColor: colors_dimmed,
borderColor: colors,
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
</script>
{% endblock %}
\ No newline at end of file
...@@ -22,13 +22,14 @@ ...@@ -22,13 +22,14 @@
# #
# #
from django.conf.urls import url, include from django.conf.urls import url
from subscription.views import * from subscription.views import *
urlpatterns = [ urlpatterns = [
# Subscription views # Subscription views
url(r'^$', NewSubscription.as_view(), name='subscription'), url(r'^$', NewSubscription.as_view(), name='subscription'),
url(r'stats', SubscriptionsStatsView.as_view(), name='stats'),
] ]
......
...@@ -22,11 +22,10 @@ ...@@ -22,11 +22,10 @@
# #
# #
from django.shortcuts import render from django.views.generic.edit import CreateView, FormView
from django.views.generic.edit import UpdateView, CreateView
from django.views.generic.base import View
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import PermissionDenied, ValidationError from django.core.exceptions import PermissionDenied, ValidationError
from django.core.urlresolvers import reverse_lazy
from django.db import IntegrityError from django.db import IntegrityError
from django import forms from django import forms
from django.forms import Select from django.forms import Select
...@@ -37,8 +36,21 @@ import random ...@@ -37,8 +36,21 @@ import random
from subscription.models import Subscription from subscription.models import Subscription
from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin
from core.views.forms import SelectDateTime
from core.models import User 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 SubscriptionForm(forms.ModelForm):
class Meta: class Meta:
model = Subscription model = Subscription
...@@ -114,3 +126,48 @@ class NewSubscription(CreateView): ...@@ -114,3 +126,48 @@ class NewSubscription(CreateView):
) )
return super(NewSubscription, self).form_valid(form) return super(NewSubscription, self).form_valid(form)
class SubscriptionsStatsView(FormView):
template_name = "subscription/stats.jinja"
form_class = SelectionDateForm
def dispatch(self, 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
kwargs = super(SubscriptionsStatsView, self).get_context_data(**kwargs)
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')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment