wcs/wcs/statistics/views.py

129 lines
4.9 KiB
Python

# w.c.s. - web application for online forms
# Copyright (C) 2005-2021 Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from django.http import HttpResponseBadRequest, HttpResponseForbidden, JsonResponse
from django.urls import reverse
from django.views.generic import View
from quixote import get_publisher
from wcs.api_utils import is_url_signed
from wcs.categories import Category
from wcs.qommon import _, misc
from wcs.qommon.misc import C_
from wcs.qommon.storage import Equal
class RestrictedView(View):
def dispatch(self, *args, **kwargs):
if not is_url_signed():
return HttpResponseForbidden()
return super().dispatch(*args, **kwargs)
class IndexView(RestrictedView):
def get(self, request, *args, **kwargs):
if not get_publisher().is_using_postgresql():
return JsonResponse({'data': [], 'err': 0})
categories = Category.select()
categories.sort(key=lambda x: misc.simplify(x.name))
category_options = [{'id': '_all', 'label': C_('categories|All')}] + [
{'id': x.id, 'label': x.name} for x in categories
]
return JsonResponse(
{
'data': [
{
'name': _('Forms Count'),
'url': request.build_absolute_uri(reverse('api-statistics-forms-count')),
'id': 'forms_counts',
'filters': [
{
'id': 'time_interval',
'label': _('Interval'),
'options': [
{
'id': 'month',
'label': _('Month'),
},
{
'id': 'year',
'label': _('Year'),
},
{
'id': 'weekday',
'label': _('Week day'),
},
{
'id': 'hour',
'label': _('Hour'),
},
],
'required': True,
'default': 'month',
},
{
'id': 'category',
'label': _('Category'),
'options': category_options,
'required': False,
'default': '_all',
},
],
}
]
}
)
class FormsCountView(RestrictedView):
def get(self, request, *args, **kwargs):
from wcs import sql
time_interval = request.GET.get('time_interval', 'month')
totals_kwargs = {
'period_start': request.GET.get('start'),
'period_end': request.GET.get('end'),
'criterias': [],
}
category_id = request.GET.get('category')
if category_id and category_id != '_all':
totals_kwargs['criterias'].append(Equal('category_id', category_id))
time_interval_methods = {
'month': sql.get_monthly_totals,
'year': sql.get_yearly_totals,
'weekday': sql.get_weekday_totals,
'hour': sql.get_hour_totals,
}
if time_interval in time_interval_methods:
totals = time_interval_methods[time_interval](**totals_kwargs)
else:
return HttpResponseBadRequest('invalid time_interval parameter')
return JsonResponse(
{
'data': {
'x_labels': [x[0] for x in totals],
'series': [
{
'label': _('Forms Count'),
'data': [x[1] for x in totals],
}
],
},
'err': 0,
}
)