<commit_before># -*- coding:utf-8 -*-

from __future__ import unicode_literals

from django.contrib.admin.models import LogEntry, CHANGE
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.db.models import F, Q
from django.http import HttpResponse, HttpResponseRedirect
from django.utils import six
from django.utils.encoding import force_text
from django.utils.itercompat import is_iterable
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from django.views.generic import (
    RedirectView,
    TemplateView,
    View,
)

from yepes.apps import apps
from yepes.utils.views import decorate_view
from yepes.utils.aggregates import SumIf
from yepes.views import FormView, UpdateView

Click = apps.get_model('newsletters', 'Click')
Delivery = apps.get_model('newsletters', 'Delivery')
Message = apps.get_model('newsletters', 'Message')
Open = apps.get_model('newsletters', 'Open')
Subscriber = apps.get_model('newsletters', 'Subscriber')
Subscription = apps.get_model('newsletters', 'Subscription')

ProfileForm = apps.get_class('newsletters.forms', 'ProfileForm')
DispatchForm = apps.get_class('newsletters.forms', 'DispatchForm')
SubscriptionForm = apps.get_class('newsletters.forms', 'SubscriptionForm')
UnsubscriptionForm = apps.get_class('newsletters.forms', 'UnsubscriptionForm')
UnsubscriptionReasonForm = apps.get_class('newsletters.forms', 'UnsubscriptionReasonForm')

ImageMixin = apps.get_class('newsletters.view_mixins', 'ImageMixin')
LinkMixin = apps.get_class('newsletters.view_mixins', 'LinkMixin')
MessageMixin = apps.get_class('newsletters.view_mixins', 'MessageMixin')
NewsletterMixin = apps.get_class('newsletters.view_mixins', 'NewsletterMixin')
SubscriberMixin = apps.get_class('newsletters.view_mixins', 'SubscriberMixin')

prerender = apps.get_class('newsletters.utils', 'prerender')
render = apps.get_class('newsletters.utils', 'render')


class DispatchView(UpdateView):

    context_object_name = 'original'
    delivery_model = Delivery
    form_class = DispatchForm
    model = Message
    subscriber_model = Subscriber
    subtitle = _('When and to whom do you want to send it?')
    title = _('Send Message')

    def form_valid(self, form):
        form_data = form.cleaned_data

        previous_deliveries = self.delivery_model.objects.get_queryset(
        ).filter(message=self.object
        ).values_list('subscriber_id')

        subscribers = self.subscriber_model.objects.get_queryset(
        ).filter(~Q(pk__in=previous_deliveries)
        ).enabled()

        if form_data.get('score_filter'):
            subscribers = subscribers.filter(**{
                'score__{0}'.format(form_data['score_filter']):
                form_data['score_filter_value'],
            })

        if form_data.get('bounce_filter'):
            subscribers = subscribers.annotate(
                bounce_count=SumIf(1, Q(deliveries__is_bounced=True)),
            ).filter(**{
                'bounce_count__{0}'.format(form_data['bounce_filter']):
                form_data['bounce_filter_value'],
            })

        if form_data.get('click_filter'):
            subscribers = subscribers.annotate(
                click_count=SumIf(1, Q(deliveries__is_clicked=True)),
            ).filter(**{
                'click_count__{0}'.format(form_data['click_filter']):
                form_data['click_filter_value'],
            })

        if form_data.get('open_filter'):
            subscribers = subscribers.annotate(
                open_count=SumIf(1, Q(deliveries__is_opened=True)),
            ).filter(**{
                'open_count__{0}'.format(form_data['open_filter']):
                form_data['open_filter_value'],
            })

        if form_data.get('tags_filter') == 'eq':
            subscribers = subscribers.filter(
                tags=form_data['tags_filter_value'],
            )
        elif form_data.get('tags_filter') == 'ne':
            subscribers = subscribers.filter(
                ~Q(tags=form_data['tags_filter_value']),
            )

        subscribers = subscribers.values_list(
            'pk',
            'email_domain_id',
        ).order_by('?')

        deliveries = []
        for subscriber_id, domain_id in subscribers:
            delivery = self.delivery_model()
            delivery.message = self.object
            delivery.newsletter_id = self.object.newsletter_id
            delivery.subscriber_id = subscriber_id
            delivery.domain_id = domain_id
            delivery.date = form_data['date']
            deliveries.append(delivery)

        self.delivery_model.objects.bulk_create(deliveries)<commit_msg>Avoid duplicate newsletter deliveries<commit_after># -*- coding:utf-8 -*-

from __future__ import unicode_literals

from django.contrib.admin.models import LogEntry, CHANGE
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.db.models import F, Q
from django.http import HttpResponse, HttpResponseRedirect
from django.utils import six
from django.utils.encoding import force_text
from django.utils.itercompat import is_iterable
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from django.views.generic import (
    RedirectView,
    TemplateView,
    View,
)

from yepes.apps import apps
from yepes.utils.views import decorate_view
from yepes.utils.aggregates import SumIf
from yepes.views import FormView, UpdateView

Click = apps.get_model('newsletters', 'Click')
Delivery = apps.get_model('newsletters', 'Delivery')
Message = apps.get_model('newsletters', 'Message')
Open = apps.get_model('newsletters', 'Open')
Subscriber = apps.get_model('newsletters', 'Subscriber')
Subscription = apps.get_model('newsletters', 'Subscription')

ProfileForm = apps.get_class('newsletters.forms', 'ProfileForm')
DispatchForm = apps.get_class('newsletters.forms', 'DispatchForm')
SubscriptionForm = apps.get_class('newsletters.forms', 'SubscriptionForm')
UnsubscriptionForm = apps.get_class('newsletters.forms', 'UnsubscriptionForm')
UnsubscriptionReasonForm = apps.get_class('newsletters.forms', 'UnsubscriptionReasonForm')

ImageMixin = apps.get_class('newsletters.view_mixins', 'ImageMixin')
LinkMixin = apps.get_class('newsletters.view_mixins', 'LinkMixin')
MessageMixin = apps.get_class('newsletters.view_mixins', 'MessageMixin')
NewsletterMixin = apps.get_class('newsletters.view_mixins', 'NewsletterMixin')
SubscriberMixin = apps.get_class('newsletters.view_mixins', 'SubscriberMixin')

prerender = apps.get_class('newsletters.utils', 'prerender')
render = apps.get_class('newsletters.utils', 'render')


class DispatchView(UpdateView):

    context_object_name = 'original'
    delivery_model = Delivery
    form_class = DispatchForm
    model = Message
    subscriber_model = Subscriber
    subtitle = _('When and to whom do you want to send it?')
    title = _('Send Message')

    def form_valid(self, form):
        form_data = form.cleaned_data

        previous_deliveries = self.delivery_model.objects.get_queryset(
        ).filter(message=self.object
        ).values_list('subscriber_id')

        subscribers = self.subscriber_model.objects.get_queryset(
        ).filter(~Q(pk__in=previous_deliveries)
        ).enabled()

        if form_data.get('score_filter'):
            subscribers = subscribers.filter(**{
                'score__{0}'.format(form_data['score_filter']):
                form_data['score_filter_value'],
            })

        if form_data.get('bounce_filter'):
            subscribers = subscribers.annotate(
                bounce_count=SumIf(1, Q(deliveries__is_bounced=True)),
            ).filter(**{
                'bounce_count__{0}'.format(form_data['bounce_filter']):
                form_data['bounce_filter_value'],
            })

        if form_data.get('click_filter'):
            subscribers = subscribers.annotate(
                click_count=SumIf(1, Q(deliveries__is_clicked=True)),
            ).filter(**{
                'click_count__{0}'.format(form_data['click_filter']):
                form_data['click_filter_value'],
            })

        if form_data.get('open_filter'):
            subscribers = subscribers.annotate(
                open_count=SumIf(1, Q(deliveries__is_opened=True)),
            ).filter(**{
                'open_count__{0}'.format(form_data['open_filter']):
                form_data['open_filter_value'],
            })

        if form_data.get('tags_filter') == 'eq':
            subscribers = subscribers.filter(
                tags=form_data['tags_filter_value'],
            )
        elif form_data.get('tags_filter') == 'ne':
            subscribers = subscribers.filter(
                ~Q(tags=form_data['tags_filter_value']),
            )

        subscribers = subscribers.values_list(
            'pk',
            'email_domain_id',
        ).order_by('?')

        deliveries = {}
        for subscriber_id, domain_id in subscribers:
            delivery = self.delivery_model()
            delivery.message = self.object
            delivery.newsletter_id = self.object.newsletter_id
            delivery.subscriber_id = subscriber_id
            delivery.domain_id = domain_id
            delivery.date = form_data['date']
            deliveries[subscriber_id] = delivery

        self.delivery_model.objects.bulk_create(six.itervalues(deliveries))
{'fraction_non_alphanumeric': 0.11136340179585097, 'fraction_numerical': 0.0016513572092063165, 'mean_word_length': 3.3181818181818183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3379609', 'n_tokens_mistral': 2934, 'n_tokens_neox': 2816, 'n_words': 491}