diff options
Diffstat (limited to 'ishtar_common/views_item.py')
| -rw-r--r-- | ishtar_common/views_item.py | 48 | 
1 files changed, 44 insertions, 4 deletions
| diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 01475f6ae..145c56ca4 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -18,8 +18,8 @@ from django.contrib.staticfiles.templatetags.staticfiles import static  from django.core.cache import cache  from django.core.exceptions import ObjectDoesNotExist  from django.core.urlresolvers import reverse, NoReverseMatch -from django.db.models import Q, Count, ImageField, Func, ExpressionWrapper, \ -    FloatField +from django.db.models import Q, Count, Sum, ImageField, Func, \ +    ExpressionWrapper, FloatField  from django.db.models.fields import FieldDoesNotExist  from django.http import HttpResponse  from django.shortcuts import render @@ -1205,6 +1205,25 @@ def _get_data_from_query_old(items, query_table_cols, request,      return datas +def _get_json_stats(items, stats_sum_variable, stats_modality_1, +                    stats_modality_2): +    q = items.values(stats_modality_1, stats_modality_2) +    if stats_sum_variable == 'pk': +        q = q.annotate(sum=Count('pk')) +    else: +        q = q.annotate(sum=Sum(stats_sum_variable)) +    data = [] +    for values in q.order_by(stats_modality_1, stats_modality_2).all(): +        modality_1 = values[stats_modality_1] +        if not data or data[-1][0] != modality_1: +            data.append([modality_1, []]) +        data[-1][1].append( +            (values[stats_modality_2], values["sum"]) +        ) +    data = json.dumps({"data": data}) +    return HttpResponse(data, content_type='application/json') + +  DEFAULT_ROW_NUMBER = 10  # length is used by ajax DataTables requests  EXCLUDED_FIELDS = ['length'] @@ -1252,10 +1271,14 @@ def get_item(model, func_name, default_name, extra_request_keys=None,              data_type = dct.pop('type')          if not data_type:              data_type = 'json' -        if data_type == "json": +        if "json" in data_type:              EMPTY = '[]' -        if data_type not in ('json', 'csv', 'json-image', 'json-map'): +        if data_type not in ('json', 'csv', 'json-image', 'json-map', +                             'json-stats'): +            return HttpResponse(EMPTY, content_type='text/plain') + +        if data_type == 'json-stats' and len(model.STATISTIC_MODALITIES) < 2:              return HttpResponse(EMPTY, content_type='text/plain')          model_to_check = model @@ -1605,6 +1628,23 @@ def get_item(model, func_name, default_name, extra_request_keys=None,          table_cols = list(table_cols)          if data_type == "json-map":              table_cols = []  # only pk for map +        elif data_type == "json-stats": +            stats_modality_1 = request_items.get("stats_modality_1", None) +            stats_modality_2 = request_items.get("stats_modality_2", None) +            if not stats_modality_1 or \ +                    stats_modality_1 not in model.STATISTIC_MODALITIES: +                stats_modality_1 = model.STATISTIC_MODALITIES[0] +            if not stats_modality_2 or \ +                    stats_modality_2 not in model.STATISTIC_MODALITIES: +                stats_modality_2 = model.STATISTIC_MODALITIES[1] +            stats_sum_variable = request_items.get('stats_sum_variable', None) +            stats_sum_variable_keys = list(model.STATISTIC_SUM_VARIABLE.keys()) +            if not stats_sum_variable or \ +                    stats_sum_variable not in stats_sum_variable_keys: +                stats_sum_variable = stats_sum_variable_keys[0] +            return _get_json_stats( +                items, stats_sum_variable, stats_modality_1, stats_modality_2) +          query_table_cols = []          for idx, cols in enumerate(table_cols):              if type(cols) not in (list, tuple): | 
