From 6991710efacf8a8857cda2184e2f7666463bef11 Mon Sep 17 00:00:00 2001 From: Alexander Burdeiny Date: Fri, 20 May 2016 15:16:09 +0300 Subject: [PATCH] 1. multiple tags in select field fixed 2. admin statistic (section/event) date range error fixed; 'no data" message added --- article/admin_urls.py | 7 +- static/custom_js/select_tag.js | 2 +- stats_collector/admin.py | 97 +++++++++++---------- stats_collector/forms.py | 18 ++-- templates/admin/stats/event_stat.html | 3 + templates/admin/stats/section_stat.html | 110 +++++++++++++----------- theme/admin.py | 4 +- 7 files changed, 131 insertions(+), 110 deletions(-) diff --git a/article/admin_urls.py b/article/admin_urls.py index 220fa796..5e503b45 100644 --- a/article/admin_urls.py +++ b/article/admin_urls.py @@ -4,10 +4,13 @@ from admin import BlogList, BlogView, NewsList, NewsView, ArticleDeleteView urlpatterns = patterns('article.admin', url(r'^blog/all/$', BlogList.as_view()), - url(r'^blog/$', BlogView.as_view()), - url(r'^delete/(?P.*)/$', ArticleDeleteView.as_view()), url(r'^news/all/$', NewsList.as_view()), + + url(r'^delete/(?P.*)/$', ArticleDeleteView.as_view()), + + url(r'^blog/$', BlogView.as_view()), url(r'^news/$', NewsView.as_view()), + url(r'^blog/(?P.*)/$', BlogView.as_view()), url(r'^news/(?P.*)/$', NewsView.as_view()), ) diff --git a/static/custom_js/select_tag.js b/static/custom_js/select_tag.js index 54744560..aab5af84 100644 --- a/static/custom_js/select_tag.js +++ b/static/custom_js/select_tag.js @@ -49,4 +49,4 @@ $(document).ready(function(){ }); });//end get });//end change - });//end ready \ No newline at end of file + });//end ready diff --git a/stats_collector/admin.py b/stats_collector/admin.py index f56f0686..1813e792 100644 --- a/stats_collector/admin.py +++ b/stats_collector/admin.py @@ -194,32 +194,32 @@ class StatSectionsView(StatBaseView): if self.request.is_ajax(): return context - pt = self.get_pivot_table() - - # change numpy.nan values to python's None (js null with json) - # result = pt.where(pt.notnull(), None) - result = pt.fillna(value=0) - result_summary = result.sum().to_dict() - context['columns'] = self.get_column_names() - - # if self.pt_columns != 'kind': - # result.columns = list(map(lambda x: str(int(x)), result.columns.tolist())) - - data = list(map( - # lambda x: {'date': pydate_to_js(x[0]), 'attrs': json.dumps(x[1:]).replace('[', '').replace(']', '')}, - lambda x: json.dumps(list(x), cls=CustomJSEncoder), - result.itertuples() - )) - - data_summary = [] - for key, val in result_summary.items(): - data_summary.append({ - 'name': self.get_col_map()[key], - 'val': val - }) - - context['data'] = data - context['data_summary'] = data_summary + if self.qs: + pt = self.get_pivot_table() + + # change numpy.nan values to python's None (js null with json) + # result = pt.where(pt.notnull(), None) + result = pt.fillna(value=0) + result_summary = result.sum().to_dict() + context['columns'] = self.get_column_names() + + # if self.pt_columns != 'kind': + # result.columns = list(map(lambda x: str(int(x)), result.columns.tolist())) + + data = list(map( + # lambda x: {'date': pydate_to_js(x[0]), 'attrs': json.dumps(x[1:]).replace('[', '').replace(']', '')}, + lambda x: json.dumps(list(x), cls=CustomJSEncoder), + result.itertuples() + )) + + data_summary = [] + for key, val in result_summary.items(): + data_summary.append({ + 'name': self.get_col_map()[key], + 'val': val + }) + context['data'] = data + context['data_summary'] = data_summary return context @@ -257,6 +257,7 @@ class EventStatView(StatBaseView): return self.render_to_response(self.get_context_data(form=form)) def get_params_form(self): + form_kwargs = {} country_ids = set() city_ids = set() theme_ids = set() @@ -271,27 +272,29 @@ class EventStatView(StatBaseView): 'exposition__tag', 'conference__tag', ] - e_country_ids, c_country_ids, e_city_ids, c_city_ids, e_theme_ids, c_theme_ids, e_tag_ids, c_tag_ids = \ - zip(*self.qs.values_list(*val_list)) - country_ids.update(e_country_ids) - country_ids.update(c_country_ids) - city_ids.update(e_city_ids) - city_ids.update(c_city_ids) - theme_ids.update(e_theme_ids) - theme_ids.update(e_theme_ids) - tag_ids.update(e_tag_ids) - tag_ids.update(c_tag_ids) - for ids_set in [country_ids, city_ids, theme_ids, tag_ids]: - if None in ids_set: - ids_set.remove(None) - - form_kwargs = { - 'country_choices': Country.objects.language().filter(pk__in=country_ids).values_list('pk', 'name').order_by('name'), - 'city_choices': City.objects.language().filter(pk__in=city_ids).values_list('pk', 'name').order_by('name'), - 'theme_choices': Theme.objects.language().filter(pk__in=theme_ids).values_list('pk', 'name').order_by('name'), - 'tag_choices': Tag.objects.language().filter(pk__in=tag_ids).values_list('pk', 'name').order_by('name'), - 'data': self.request.POST - } + values_list = self.qs.values_list(*val_list) + if values_list: + e_country_ids, c_country_ids, e_city_ids, c_city_ids, e_theme_ids, c_theme_ids, e_tag_ids, c_tag_ids = \ + zip(*values_list) + country_ids.update(e_country_ids) + country_ids.update(c_country_ids) + city_ids.update(e_city_ids) + city_ids.update(c_city_ids) + theme_ids.update(e_theme_ids) + theme_ids.update(e_theme_ids) + tag_ids.update(e_tag_ids) + tag_ids.update(c_tag_ids) + for ids_set in [country_ids, city_ids, theme_ids, tag_ids]: + if None in ids_set: + ids_set.remove(None) + + form_kwargs = { + 'country_choices': Country.objects.language().filter(pk__in=country_ids).values_list('pk', 'name').order_by('name'), + 'city_choices': City.objects.language().filter(pk__in=city_ids).values_list('pk', 'name').order_by('name'), + 'theme_choices': Theme.objects.language().filter(pk__in=theme_ids).values_list('pk', 'name').order_by('name'), + 'tag_choices': Tag.objects.language().filter(pk__in=tag_ids).values_list('pk', 'name').order_by('name'), + 'data': self.request.POST + } form = self.params_form_cls(**form_kwargs) return form diff --git a/stats_collector/forms.py b/stats_collector/forms.py index a846d275..11240a14 100644 --- a/stats_collector/forms.py +++ b/stats_collector/forms.py @@ -27,8 +27,14 @@ class BaseDateFilter(forms.Form): date_end = forms.DateField(label=_(u'Дата окончания'), input_formats=['%Y-%m-%d', '%d.%m.%Y'], required=False) def clean(self): - if not self.cleaned_data['date_begin'] and self.cleaned_data['date_end']: - self.cleaned_data['date_begin'] = self.cleaned_data['date_end'] - timedelta(days=14) + date_begin = self.cleaned_data['date_begin'] + date_end = self.cleaned_data['date_end'] + if not date_begin and date_end: + self.cleaned_data['date_begin'] = date_end - timedelta(days=14) + elif date_begin and date_end and date_begin >= date_end: + raise forms.ValidationError( + _(u'Дата начала должна быть меньше даты окончания.'), + code='invalid') return self.cleaned_data @@ -70,10 +76,10 @@ class EventsParamsFilter(forms.Form): tag = forms.TypedMultipleChoiceField(label=_(u'Тег'), required=False, coerce=int) def __init__(self, *args, **kwargs): - country_choices = kwargs.pop('country_choices') - city_choices = kwargs.pop('city_choices') - theme_choices = kwargs.pop('theme_choices') - tag_choices = kwargs.pop('tag_choices') + country_choices = kwargs.pop('country_choices', []) + city_choices = kwargs.pop('city_choices', []) + theme_choices = kwargs.pop('theme_choices', []) + tag_choices = kwargs.pop('tag_choices', []) super(EventsParamsFilter, self).__init__(*args, **kwargs) diff --git a/templates/admin/stats/event_stat.html b/templates/admin/stats/event_stat.html index 6f1478e6..07fadbe7 100644 --- a/templates/admin/stats/event_stat.html +++ b/templates/admin/stats/event_stat.html @@ -146,6 +146,9 @@
+ {% if not data %} + {% trans "Нет данных" %} + {% endif %}
diff --git a/templates/admin/stats/section_stat.html b/templates/admin/stats/section_stat.html index bc82309d..f3e7f316 100644 --- a/templates/admin/stats/section_stat.html +++ b/templates/admin/stats/section_stat.html @@ -7,58 +7,62 @@