<commit_before>from django.apps import apps
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.messages.views import SuccessMessageMixin
from django.core.urlresolvers import reverse
from django.http import Http404
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django.views import generic
from django.views.generic.detail import SingleObjectMixin

from adhocracy4.filters import views as filter_views
from adhocracy4.modules import models as module_models
from adhocracy4.phases import models as phase_models
from adhocracy4.projects import models as project_models
from adhocracy4.projects.mixins import ProjectMixin

from . import filter
from . import forms
from . import get_project_dashboard
from . import mixins
from . import signals
from .blueprints import get_blueprints

User = get_user_model()


def get_object_or_none(*args, **kwargs):
    try:
        return get_object_or_404(*args, **kwargs)
    except Http404:
        return None


class ProjectListView(mixins.DashboardBaseMixin,
                      mixins.DashboardProjectDuplicateMixin,
                      filter_views.FilteredListView):
    model = project_models.Project
    paginate_by = 12
    template_name = 'a4dashboard/project_list.html'
    permission_required = 'a4projects.add_project'
    menu_item = 'project'
    filter_set = filter.ProjectFilterSet

    def get_queryset(self):
        return super().get_queryset().filter(organisation=self.organisation)

    def get_permission_object(self):
        return self.organisation


class BlueprintListView(mixins.DashboardBaseMixin,
                        generic.TemplateView):
    template_name = 'a4dashboard/blueprint_list.html'
    permission_required = 'a4projects.add_project'
    menu_item = 'project'

    @property
    def blueprints(self):
        return get_blueprints()

    def get_permission_object(self):
        return self.organisation


class ProjectCreateView(mixins.DashboardBaseMixin,
                        mixins.BlueprintMixin,
                        SuccessMessageMixin,
                        generic.CreateView):
    model = project_models.Project
    slug_url_kwarg = 'project_slug'
    form_class = forms.ProjectCreateForm
    template_name = 'a4dashboard/project_create_form.html'
    permission_required = 'a4projects.add_project'
    menu_item = 'project'
    success_message = _('Project successfully created.')

    def get_permission_object(self):
        return self.organisation

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

    def get_success_url(self):
        return reverse('a4dashboard:project-edit',
                       kwargs={'project_slug': self.object.slug})

    def form_valid(self, form):
        response = super().form_valid(form)
        signals.project_created.send(sender=None,
                                     project=self.object,
                                     user=self.request.user)

        self._create_modules_and_phases(self.object)

        return response

    def _create_modules_and_phases(self, project):
        module = module_models.Module(
            name='Onlinebeteiligung',
            description=project.description,
            weight=1,
            project=project,
        )
        module.save()
        signals.module_created.send(sender=None,
                                    module=module,
                                    user=self.request.user)

        self._create_module_settings(module)
        self._create_phases(module, self.blueprint.content)

    def _create_module_settings(self, module):
        if self.blueprint.settings_model:
            settings_model = apps.get_model(*self.blueprint.settings_model)
            module_settings = settings_model(module=module)
            module_settings.save()

    def _create_phases(self, module, blueprint_phases):
        for phase_content in blueprint_phases:
            phase = phase_models.Phase(
                type=phase_content.identifier,
                name=phase_content.name,
                description=phase_content.description,
                weight=phase_content.weight,
                module=module,
            )
            phase.save()


class ProjectUpdateView(generic.RedirectView):
    def get_redirect_url(self, *args, **kwargs):
        project = get_object_or_404(project_models.Project,
                                    slug=kwargs['project_slug'])
        dashboard = get_project_dashboard(project)<commit_msg>set weight by index<commit_after>from django.apps import apps
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.messages.views import SuccessMessageMixin
from django.core.urlresolvers import reverse
from django.http import Http404
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django.views import generic
from django.views.generic.detail import SingleObjectMixin

from adhocracy4.filters import views as filter_views
from adhocracy4.modules import models as module_models
from adhocracy4.phases import models as phase_models
from adhocracy4.projects import models as project_models
from adhocracy4.projects.mixins import ProjectMixin

from . import filter
from . import forms
from . import get_project_dashboard
from . import mixins
from . import signals
from .blueprints import get_blueprints

User = get_user_model()


def get_object_or_none(*args, **kwargs):
    try:
        return get_object_or_404(*args, **kwargs)
    except Http404:
        return None


class ProjectListView(mixins.DashboardBaseMixin,
                      mixins.DashboardProjectDuplicateMixin,
                      filter_views.FilteredListView):
    model = project_models.Project
    paginate_by = 12
    template_name = 'a4dashboard/project_list.html'
    permission_required = 'a4projects.add_project'
    menu_item = 'project'
    filter_set = filter.ProjectFilterSet

    def get_queryset(self):
        return super().get_queryset().filter(organisation=self.organisation)

    def get_permission_object(self):
        return self.organisation


class BlueprintListView(mixins.DashboardBaseMixin,
                        generic.TemplateView):
    template_name = 'a4dashboard/blueprint_list.html'
    permission_required = 'a4projects.add_project'
    menu_item = 'project'

    @property
    def blueprints(self):
        return get_blueprints()

    def get_permission_object(self):
        return self.organisation


class ProjectCreateView(mixins.DashboardBaseMixin,
                        mixins.BlueprintMixin,
                        SuccessMessageMixin,
                        generic.CreateView):
    model = project_models.Project
    slug_url_kwarg = 'project_slug'
    form_class = forms.ProjectCreateForm
    template_name = 'a4dashboard/project_create_form.html'
    permission_required = 'a4projects.add_project'
    menu_item = 'project'
    success_message = _('Project successfully created.')

    def get_permission_object(self):
        return self.organisation

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

    def get_success_url(self):
        return reverse('a4dashboard:project-edit',
                       kwargs={'project_slug': self.object.slug})

    def form_valid(self, form):
        response = super().form_valid(form)
        signals.project_created.send(sender=None,
                                     project=self.object,
                                     user=self.request.user)

        self._create_modules_and_phases(self.object)

        return response

    def _create_modules_and_phases(self, project):
        module = module_models.Module(
            name='Onlinebeteiligung',
            description=project.description,
            weight=1,
            project=project,
        )
        module.save()
        signals.module_created.send(sender=None,
                                    module=module,
                                    user=self.request.user)

        self._create_module_settings(module)
        self._create_phases(module, self.blueprint.content)

    def _create_module_settings(self, module):
        if self.blueprint.settings_model:
            settings_model = apps.get_model(*self.blueprint.settings_model)
            module_settings = settings_model(module=module)
            module_settings.save()

    def _create_phases(self, module, blueprint_phases):
        for index, phase_content in enumerate(blueprint_phases):
            phase = phase_models.Phase(
                type=phase_content.identifier,
                name=phase_content.name,
                description=phase_content.description,
                weight=index,
                module=module,
            )
            phase.save()


class ProjectUpdateView(generic.RedirectView):
    def get_redirect_url(self, *args, **kwargs):
        project = get_object_or_404(project_models.Project,
                                    slug=kwargs['project_slug'])
        dashboard = get_project_dashboard(project)
{'fraction_non_alphanumeric': 0.07169136970461473, 'fraction_numerical': 0.00630715862503942, 'mean_word_length': 2.6875968992248063, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4712525', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2518, 'n_words': 507}