<commit_before>from django.apps import apps
from django.conf import settings
from django.contrib import auth
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.views import generic

from adhocracy4.follows.models import Follow
from adhocracy4.rules import mixins as rules_mixins

from . import emails
from . import forms
from . import models

Organisation = apps.get_model(settings.A4_ORGANISATIONS_MODEL)
User = auth.get_user_model()


class NewsletterCreateView(rules_mixins.PermissionRequiredMixin,
                           generic.CreateView):
    model = models.Newsletter
    form_class = forms.NewsletterForm
    permission_required = 'is_superuser'

    def get_email_kwargs(self):
        kwargs = {}
        kwargs.update({'organisation_pk': None})
        return kwargs

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = self.request.user

        if hasattr(self, 'organisation'):
            sender_name = self.organisation.name
        else:
            sender_name = settings.WAGTAIL_SITE_NAME

        kwargs['initial'] = {
            'sender_name': sender_name,
            'sender': settings.CONTACT_EMAIL
        }
        return kwargs

    def get_success_url(self):
        return reverse('meinberlin_newsletters:newsletter-create')

    def form_valid(self, form):
        # Check if the current user is allowed to send to the selected org
        organisation = form.cleaned_data['organisation']
        if not (self.request.user.is_superuser or
                Organisation.objects.get(pk=organisation.pk)
                    .has_initiator(self.request.user)):
            raise PermissionDenied

        instance = form.save(commit=False)
        instance.creator = self.request.user
        instance.sent = timezone.now()
        instance.save()
        form.save_m2m()

        receivers = int(form.cleaned_data['receivers'])
        participant_ids = []

        if receivers == models.PROJECT:
            participant_ids = Follow.objects.filter(
                project=form.cleaned_data['project'].pk,
                enabled=True
            ).values_list('creator', flat=True)

        elif receivers == models.ORGANISATION:
            participant_ids = Follow.objects.filter(
                project__organisation=organisation.pk,
                enabled=True
            ).values_list('creator', flat=True).distinct()

        elif receivers == models.PLATFORM:
            participant_ids = User.objects.all().values_list('pk',
                                                             flat=True)

        elif receivers == models.INITIATOR:
            participant_ids = Organisation.objects.get(
                pk=organisation.pk).initiators.all()\
                .values_list('pk', flat=True)

        emails.NewsletterEmail.send(instance,
                                    participant_ids=list(participant_ids),
                                    **self.get_email_kwargs())
        messages.success(self.request,
                         _('Newsletter has been queued to be send.'))<commit_msg>Change success message<commit_after>from django.apps import apps
from django.conf import settings
from django.contrib import auth
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.views import generic

from adhocracy4.follows.models import Follow
from adhocracy4.rules import mixins as rules_mixins

from . import emails
from . import forms
from . import models

Organisation = apps.get_model(settings.A4_ORGANISATIONS_MODEL)
User = auth.get_user_model()


class NewsletterCreateView(rules_mixins.PermissionRequiredMixin,
                           generic.CreateView):
    model = models.Newsletter
    form_class = forms.NewsletterForm
    permission_required = 'is_superuser'

    def get_email_kwargs(self):
        kwargs = {}
        kwargs.update({'organisation_pk': None})
        return kwargs

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = self.request.user

        if hasattr(self, 'organisation'):
            sender_name = self.organisation.name
        else:
            sender_name = settings.WAGTAIL_SITE_NAME

        kwargs['initial'] = {
            'sender_name': sender_name,
            'sender': settings.CONTACT_EMAIL
        }
        return kwargs

    def get_success_url(self):
        return reverse('meinberlin_newsletters:newsletter-create')

    def form_valid(self, form):
        # Check if the current user is allowed to send to the selected org
        organisation = form.cleaned_data['organisation']
        if not (self.request.user.is_superuser or
                Organisation.objects.get(pk=organisation.pk)
                    .has_initiator(self.request.user)):
            raise PermissionDenied

        instance = form.save(commit=False)
        instance.creator = self.request.user
        instance.sent = timezone.now()
        instance.save()
        form.save_m2m()

        receivers = int(form.cleaned_data['receivers'])
        participant_ids = []

        if receivers == models.PROJECT:
            participant_ids = Follow.objects.filter(
                project=form.cleaned_data['project'].pk,
                enabled=True
            ).values_list('creator', flat=True)

        elif receivers == models.ORGANISATION:
            participant_ids = Follow.objects.filter(
                project__organisation=organisation.pk,
                enabled=True
            ).values_list('creator', flat=True).distinct()

        elif receivers == models.PLATFORM:
            participant_ids = User.objects.all().values_list('pk',
                                                             flat=True)

        elif receivers == models.INITIATOR:
            participant_ids = Organisation.objects.get(
                pk=organisation.pk).initiators.all()\
                .values_list('pk', flat=True)

        emails.NewsletterEmail.send(instance,
                                    participant_ids=list(participant_ids),
                                    **self.get_email_kwargs())
        messages.success(self.request,
                         _('Newsletter has been saved and '
                           'will be sent to the recipients.'))
{'fraction_non_alphanumeric': 0.08377896613190731, 'fraction_numerical': 0.0011883541295306002, 'mean_word_length': 2.2636936500242366, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '392876', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1738, 'n_words': 375}