diff --git a/emencia/django/newsletter/forms.py b/emencia/django/newsletter/forms.py index e4b8941a..fc47e873 100644 --- a/emencia/django/newsletter/forms.py +++ b/emencia/django/newsletter/forms.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """Forms for emencia.django.newsletter""" +from datetime import datetime, date, timedelta from django import forms from django.utils.translation import ugettext_lazy as _ from django.http import Http404 @@ -8,8 +9,7 @@ from django.core.exceptions import ValidationError from django.utils import translation from haystack.query import SearchQuerySet from functions.search_forms import get_by_lang -from emencia.django.newsletter.models import Contact, ContactSettings -from emencia.django.newsletter.models import MailingList +from emencia.django.newsletter.models import Contact, ContactSettings, MailingList, PopupCount from functions.form_check import translit_with_separator as tr from theme.models import Theme from country.models import Country, Area @@ -19,7 +19,7 @@ from city.models import City class MailingListSubscriptionForm(forms.ModelForm): """Form for subscribing to a mailing list""" # Notes : This form will not check the uniquess of - # the 'email' field, by defining it explictly and setting + # the 'email' field, by defining it explictly and setting6 # it the Meta.exclude list, for allowing registration # to a mailing list even if the contact already exists. # Then the contact is always added to the subscribers field @@ -94,7 +94,7 @@ class ContactSettingsForm(forms.ModelForm): return Theme.objects.none() -from datetime import datetime + class ContactFilterForm(forms.Form): email = forms.EmailField( @@ -334,4 +334,28 @@ class SubscribeSettingsForm(AbstractSubscribeForm): return contactsettings def clean_email(self): - return self.cleaned_data['email'] \ No newline at end of file + return self.cleaned_data['email'] + +from django.db.models import Sum +class PopupCountFilter(forms.Form): + fr = forms.DateField(required=False) + to = forms.DateField(required=False) + + def filter(self): + fr = self.cleaned_data.get('fr') + to = self.cleaned_data.get('to') + if not fr and not to: + fr = date.today() + + qs = PopupCount.objects.filter(date__gte=fr) + contacts = Contact.objects.filter(creation_date__gte=fr) + + if to: + contacts = contacts.filter(creation_date__lte=to+timedelta(days=1)) + qs = qs.filter(date__lte=to) + + subscribed = contacts.count() + activated = contacts.filter(activated=True).count() + popups = qs.aggregate(count=Sum('cnt'))['count'] + + return {'subscribed': subscribed, 'activated': activated, 'popups': popups} \ No newline at end of file diff --git a/emencia/django/newsletter/models.py b/emencia/django/newsletter/models.py index 71f77c9d..0d7c609f 100644 --- a/emencia/django/newsletter/models.py +++ b/emencia/django/newsletter/models.py @@ -564,9 +564,9 @@ class PopupCount(models.Model): ordering = ['-date'] def get_subscr(self): - qs = Contact.objects.filter(creation_date=self.date) + qs = Contact.objects.filter(creation_date__startswith=self.date) return qs.count() def get_active(self): - qs = Contact.objects.filter(creation_date=self.date, activated=True) + qs = Contact.objects.filter(creation_date__startswith=self.date, activated=True) return qs.count() \ No newline at end of file diff --git a/emencia/django/newsletter/views/admin_views.py b/emencia/django/newsletter/views/admin_views.py index 6ad72ab6..ccf19a7d 100644 --- a/emencia/django/newsletter/views/admin_views.py +++ b/emencia/django/newsletter/views/admin_views.py @@ -11,6 +11,7 @@ from HTMLParser import HTMLParseError from emencia.django.newsletter.models import Contact, ContactSettings, MailingList, Newsletter, Attachment, ContactMailingStatus from emencia.django.newsletter.admin_forms import ContactSettingsForm, MailingListForm, NewsletterForm, AttachmentForm from emencia.django.newsletter.mailer import Mailer +from emencia.django.newsletter.forms import PopupCountFilter from ..forms import ContactFilterForm, ContactImportForm from ..utils.excel import ExcelResponse from functions.admin_views import paginate_results @@ -347,12 +348,32 @@ def count_popups(request): obj.save() return HttpResponse(obj.cnt, content_type='application/json') -from django.db.models import Sum + +class PopupStatisticsView(FormView): + form_class = PopupCountFilter + template_name = 'admin/newsletters/popup_count.html' + + def get(self, request, *args, **kwargs): + form_class = self.get_form_class() + form = form_class(request.GET) + if form.is_valid(): + return self.form_valid(form) + else: + return self.form_invalid(form) + + def form_valid(self, form): + data = form.filter() + data['form'] = form + + return self.render_to_response(data) + + +""" class PopupStatisticsView(TemplateView): model = PopupCount - template_name='admin/newsletters/popup_count.html' + template_name = 'admin/newsletters/popup_count.html' def get(self, request, *args, **kwargs): qs = PopupCount.objects.all() @@ -369,3 +390,4 @@ class PopupStatisticsView(TemplateView): res = {'cnt':0, 'subscr':0, 'active':0} return self.render_to_response({'object':res}) +""" \ No newline at end of file diff --git a/static/custom_js/main.js b/static/custom_js/main.js index 3fde29bd..e5eaa456 100644 --- a/static/custom_js/main.js +++ b/static/custom_js/main.js @@ -236,138 +236,141 @@ $(document).ready(function(){ return false; }); // end on-of events - - - - - - $('#id_city').select2({ + if( $("#id_city" ).length ) { + $('#id_city').select2({ placeholder: "Город", width: 'element', ajax: { - url: "/admin/city/search/", - dataType: "json", - quietMillis: 200, - - data: function(term, page, country){ - var country = $('#id_country').val() - return {term: term, - page: page, - country: country}; + url: "/admin/city/search/", + dataType: "json", + quietMillis: 200, + + data: function(term, page, country){ + var country = $('#id_country').val() + return {term: term, + page: page, + country: country}; + }, + + results: function (data) { + var results = []; + $.each(data, function(index, item){ + results.push({ + id: item.id, + text: item.label + }); + }); + return {results: results}; + } }, + initSelection : function(element, callback) { + var id= $(element).val(); + var text = $(element).attr('data-init-text'); + callback({id: id, text:text}); - results: function (data) { - var results = []; - $.each(data, function(index, item){ - results.push({ - id: item.id, - text: item.label - }); - }); - return {results: results}; } - }, - initSelection : function(element, callback) { - var id= $(element).val(); - var text = $(element).attr('data-init-text'); - callback({id: id, text:text}); - - } - }); + }); + } - $('#id_main_page_news').select2({ + if( $("#id_main_page_news" ).length ) { + $('#id_main_page_news').select2({ placeholder: "Новости", multiple: true, width: '100%', ajax: { - url: "/admin/ajax/get-news/", - dataType: "json", - quietMillis: 200, - data: function(term, page){ - - return {term: term, - page: page}; - }, - - results: function (data) { - var results = []; - $.each(data, function(index, item){ - results.push({ - id: item.id, - text: item.label + url: "/admin/ajax/get-news/", + dataType: "json", + quietMillis: 200, + data: function(term, page){ + + return {term: term, + page: page}; + }, + + results: function (data) { + var results = []; + $.each(data, function(index, item){ + results.push({ + id: item.id, + text: item.label + }); }); + return {results: results}; + } + }, + initSelection : function(element, callback) { + var text = $(element).attr('data-init-text'); + var data = $.parseJSON(text); + console.log(data); + /* + var data = []; + $(element.val().split(",")).each(function(i) { + var item = this.split(':'); + data.push({ + id: item[0], + text: item[1] + }); }); - return {results: results}; + console.log(data) + */ + callback(data); + } - }, - initSelection : function(element, callback) { - var text = $(element).attr('data-init-text'); - var data = $.parseJSON(text); - console.log(data); - /* - var data = []; - $(element.val().split(",")).each(function(i) { - var item = this.split(':'); - data.push({ - id: item[0], - text: item[1] - }); - }); - console.log(data) - */ - callback(data); - } + }); - }); + } - // selects - $('#id_tag').select2({ + if( $("#id_main_page_news" ).length ) { + $('#id_tag').select2({ placeholder: "Теги", width: '550px', multiple: true, ajax: { - url: "/admin/theme/tag/search/", - dataType: "json", - quietMillis: 200, - multiple: true, - - data: function(term, page, theme){ - var theme = $('#id_theme').serialize(); - return {term: term, - page: page, - theme: theme}; - }, - - results: function (data) { - var results = []; - $.each(data, function(index, item){ - results.push({ - id: item.id, - text: item.label + url: "/admin/theme/tag/search/", + dataType: "json", + quietMillis: 200, + multiple: true, + + data: function(term, page, theme){ + var theme = $('#id_theme').serialize(); + return {term: term, + page: page, + theme: theme}; + }, + + results: function (data) { + var results = []; + $.each(data, function(index, item){ + results.push({ + id: item.id, + text: item.label + }); }); + return {results: results}; + } + }, + initSelection : function(element, callback) { + var data = []; + $(element.val().split(",")).each(function(i) { + var item = this.split(':'); + data.push({ + id: item[0], + text: item[1] + }); }); - return {results: results}; + callback(data); + } - }, - initSelection : function(element, callback) { - var data = []; - $(element.val().split(",")).each(function(i) { - var item = this.split(':'); - data.push({ - id: item[0], - text: item[1] - }); - }); - callback(data); - } + }); + } + - }); // end selects /* */ @@ -486,12 +489,14 @@ $(document).ready(function(){ } }); }); - + if($('select').length){ $('select').select2({ width: 'element', allowClear: true - });//end select + }); + } + }); diff --git a/templates/admin/newsletters/popup_count.html b/templates/admin/newsletters/popup_count.html index e58cd809..1554f073 100644 --- a/templates/admin/newsletters/popup_count.html +++ b/templates/admin/newsletters/popup_count.html @@ -17,8 +17,8 @@
- - + {{ form.fr }} + {{ form.to }}
@@ -32,15 +32,15 @@ Показов - {{ object.cnt }} + {{ popups }} Подписалось - {{ object.subscr }} + {{ subscribed }} Подтверждено - {{ object.active }} + {{ activated }} @@ -52,9 +52,9 @@