<commit_before>from django.shortcuts import get_object_or_404
from django.views.generic import (
    CreateView, ListView, DetailView, FormView, UpdateView)
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

from braces.views import (
    LoginRequiredMixin, PermissionRequiredMixin, FormValidMessageMixin)

from jenkins.models import Build
from projects.models import (
    Project, Dependency, ProjectDependency, ProjectBuild,
    ProjectBuildDependency)
from projects.forms import (
    ProjectForm, DependencyForm, ProjectBuildForm, ProjectBuildArchiveForm)
from projects.helpers import build_project, build_dependency
from projects.utils import get_build_table_for_project


class ProjectCreateView(
    LoginRequiredMixin, PermissionRequiredMixin, FormValidMessageMixin,
        CreateView):

    permission_required = "projects.add_project"
    form_valid_message = "Project created"
    model = Project
    form_class = ProjectForm

    def get_success_url(self):
        return reverse("project_detail", kwargs={"pk": self.object.pk})


class ProjectListView(LoginRequiredMixin, ListView):

    model = Project


class ProjectUpdateView(
    LoginRequiredMixin, PermissionRequiredMixin, FormValidMessageMixin,
        UpdateView):

    permission_required = "projects.change_project"
    form_valid_message = "Project updated"
    model = Project
    form_class = ProjectForm

    def get_success_url(self):
        return reverse("project_detail", kwargs={"pk": self.object.pk})


class InitiateProjectBuildView(LoginRequiredMixin, FormView):
    """
    Starts building a project and redirects to the newly created ProjectBuild.
    """
    form_class = ProjectBuildForm
    template_name = "projects/projectbuild_form.html"

    def get_form(self, form_class):
        """
        Returns an instance of the form to be used in this view.
        """
        project = get_object_or_404(Project, pk=self.kwargs["pk"])
        form = form_class(**self.get_form_kwargs())
        dependencies = project.dependencies
        form.fields["dependencies"].queryset = dependencies
        initial_pks = [x.pk for x in dependencies.all()]
        form.fields["dependencies"].initial = initial_pks
        form.fields["project"].initial = project
        return form

    def form_valid(self, form):
        project = form.cleaned_data["project"]
        projectbuild = build_project(
            project, user=self.request.user,
            dependencies=form.cleaned_data["dependencies"])
        messages.add_message(
            self.request, messages.INFO,
            "Build '%s' queued." % projectbuild.build_id)

        url_args = {"project_pk": project.pk, "build_pk": projectbuild.pk}
        url = reverse("project_projectbuild_detail", kwargs=url_args)
        return HttpResponseRedirect(url)


class ProjectBuildListView(LoginRequiredMixin, ListView):

    context_object_name = "projectbuilds"
    model = ProjectBuild

    def get_queryset(self):
        return ProjectBuild.objects.filter(
            project=self._get_project_from_url())

    def _get_project_from_url(self):
        return get_object_or_404(Project, pk=self.kwargs["pk"])

    def get_context_data(self, **kwargs):
        """
        Supplement the projectbuilds with the project:
        """
        context = super(
            ProjectBuildListView, self).get_context_data(**kwargs)
        context["project"] = self._get_project_from_url()
        return context


class ProjectBuildDetailView(LoginRequiredMixin, DetailView):

    template_name = "projects/projectbuild_detail.html"

    def get_object(self):
        project_pk = self.kwargs["project_pk"]
        build_pk = self.kwargs["build_pk"]
        return get_object_or_404(
            ProjectBuild, project__pk=project_pk, pk=build_pk)

    def _get_project_from_url(self):
        return get_object_or_404(Project, pk=self.kwargs["project_pk"])

    def _get_build_from_url(self):
        project_pk = self.kwargs["project_pk"]
        build_pk = self.kwargs["build_pk"]
        return get_object_or_404(
            ProjectBuild, project__pk=project_pk, pk=build_pk)

    def _get_build_dependencies(self, projectbuild):
        return ProjectBuildDependency.objects.filter(projectbuild=projectbuild)

    def get_context_data(self, **kwargs):
        """
        Supplement the projectbuilds with the project:
        """
        context = super(
            ProjectBuildDetailView, self).get_context_data(**kwargs)
        context["project"] = self._get_project_from_url()
        context["projectbuild"] = self._get_build_from_url()
        context["dependencies"] = self._get_build_dependencies(
            context["projectbuild"])
        return context


class ProjectDetailView(LoginRequiredMixin, DetailView):

    model = Project
    context_object_name = "project"

    def get_context_data(self, **kwargs):
        """
        Supplement the project with its dependencies.
        """
        context = super(
            ProjectDetailView, self).get_context_data(**kwargs)
        context["dependencies"] = ProjectDependency.objects.filter(
            project=context["project"])
        context["projectbuilds"] = ProjectBuild.objects.filter(
            project=context["project"]).order_by("-build_id")[:5]
        return context


class DependencyCreateView(
    LoginRequiredMixin, PermissionRequiredMixin, FormValidMessageMixin,
        CreateView):

    permission_required = "projects.add_dependency"
    form_valid_message = "Dependency created"
    form_class = DependencyForm
    model = Dependency

    def get_success_url(self):
        url_args = {"pk": self.object.pk}
        return reverse("dependency_detail", kwargs=url_args)


class DependencyListView(LoginRequiredMixin, ListView):

    context_object_name = "dependencies"
    model = Dependency


class DependencyDetailView(LoginRequiredMixin, DetailView):

    context_object_name = "dependency"
    model = Dependency

    def get_context_data(self, **kwargs):
        """
        Supplement the dependency.
        """<commit_msg>Add raise_exception to the permission required views.<commit_after>from django.shortcuts import get_object_or_404
from django.views.generic import (
    CreateView, ListView, DetailView, FormView, UpdateView)
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

from braces.views import (
    LoginRequiredMixin, PermissionRequiredMixin, FormValidMessageMixin)

from jenkins.models import Build
from projects.models import (
    Project, Dependency, ProjectDependency, ProjectBuild,
    ProjectBuildDependency)
from projects.forms import (
    ProjectForm, DependencyForm, ProjectBuildForm, ProjectBuildArchiveForm)
from projects.helpers import build_project, build_dependency
from projects.utils import get_build_table_for_project


class ProjectCreateView(
    LoginRequiredMixin, PermissionRequiredMixin, FormValidMessageMixin,
        CreateView):

    raise_exception = True
    permission_required = "projects.add_project"
    form_valid_message = "Project created"
    model = Project
    form_class = ProjectForm

    def get_success_url(self):
        return reverse("project_detail", kwargs={"pk": self.object.pk})


class ProjectListView(LoginRequiredMixin, ListView):

    model = Project


class ProjectUpdateView(
    LoginRequiredMixin, PermissionRequiredMixin, FormValidMessageMixin,
        UpdateView):

    raise_exception = True
    permission_required = "projects.change_project"
    form_valid_message = "Project updated"
    model = Project
    form_class = ProjectForm

    def get_success_url(self):
        return reverse("project_detail", kwargs={"pk": self.object.pk})


class InitiateProjectBuildView(LoginRequiredMixin, FormView):
    """
    Starts building a project and redirects to the newly created ProjectBuild.
    """
    form_class = ProjectBuildForm
    template_name = "projects/projectbuild_form.html"

    def get_form(self, form_class):
        """
        Returns an instance of the form to be used in this view.
        """
        project = get_object_or_404(Project, pk=self.kwargs["pk"])
        form = form_class(**self.get_form_kwargs())
        dependencies = project.dependencies
        form.fields["dependencies"].queryset = dependencies
        initial_pks = [x.pk for x in dependencies.all()]
        form.fields["dependencies"].initial = initial_pks
        form.fields["project"].initial = project
        return form

    def form_valid(self, form):
        project = form.cleaned_data["project"]
        projectbuild = build_project(
            project, user=self.request.user,
            dependencies=form.cleaned_data["dependencies"])
        messages.add_message(
            self.request, messages.INFO,
            "Build '%s' queued." % projectbuild.build_id)

        url_args = {"project_pk": project.pk, "build_pk": projectbuild.pk}
        url = reverse("project_projectbuild_detail", kwargs=url_args)
        return HttpResponseRedirect(url)


class ProjectBuildListView(LoginRequiredMixin, ListView):

    context_object_name = "projectbuilds"
    model = ProjectBuild

    def get_queryset(self):
        return ProjectBuild.objects.filter(
            project=self._get_project_from_url())

    def _get_project_from_url(self):
        return get_object_or_404(Project, pk=self.kwargs["pk"])

    def get_context_data(self, **kwargs):
        """
        Supplement the projectbuilds with the project:
        """
        context = super(
            ProjectBuildListView, self).get_context_data(**kwargs)
        context["project"] = self._get_project_from_url()
        return context


class ProjectBuildDetailView(LoginRequiredMixin, DetailView):

    template_name = "projects/projectbuild_detail.html"

    def get_object(self):
        project_pk = self.kwargs["project_pk"]
        build_pk = self.kwargs["build_pk"]
        return get_object_or_404(
            ProjectBuild, project__pk=project_pk, pk=build_pk)

    def _get_project_from_url(self):
        return get_object_or_404(Project, pk=self.kwargs["project_pk"])

    def _get_build_from_url(self):
        project_pk = self.kwargs["project_pk"]
        build_pk = self.kwargs["build_pk"]
        return get_object_or_404(
            ProjectBuild, project__pk=project_pk, pk=build_pk)

    def _get_build_dependencies(self, projectbuild):
        return ProjectBuildDependency.objects.filter(projectbuild=projectbuild)

    def get_context_data(self, **kwargs):
        """
        Supplement the projectbuilds with the project:
        """
        context = super(
            ProjectBuildDetailView, self).get_context_data(**kwargs)
        context["project"] = self._get_project_from_url()
        context["projectbuild"] = self._get_build_from_url()
        context["dependencies"] = self._get_build_dependencies(
            context["projectbuild"])
        return context


class ProjectDetailView(LoginRequiredMixin, DetailView):

    model = Project
    context_object_name = "project"

    def get_context_data(self, **kwargs):
        """
        Supplement the project with its dependencies.
        """
        context = super(
            ProjectDetailView, self).get_context_data(**kwargs)
        context["dependencies"] = ProjectDependency.objects.filter(
            project=context["project"])
        context["projectbuilds"] = ProjectBuild.objects.filter(
            project=context["project"]).order_by("-build_id")[:5]
        return context


class DependencyCreateView(
    LoginRequiredMixin, PermissionRequiredMixin, FormValidMessageMixin,
        CreateView):

    raise_exception = True
    permission_required = "projects.add_dependency"
    form_valid_message = "Dependency created"
    form_class = DependencyForm
    model = Dependency

    def get_success_url(self):
        url_args = {"pk": self.object.pk}
        return reverse("dependency_detail", kwargs=url_args)


class DependencyListView(LoginRequiredMixin, ListView):

    context_object_name = "dependencies"
    model = Dependency


class DependencyDetailView(LoginRequiredMixin, DetailView):

    context_object_name = "dependency"
    model = Dependency

    def get_context_data(self, **kwargs):
        """
        Supplement the dependency.
        """
{'fraction_non_alphanumeric': 0.08916069847911805, 'fraction_numerical': 0.0030578578900780557, 'mean_word_length': 3.527504553734062, 'pattern_counts': {'":': 10, '<': 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': '1256712', 'n_tokens_mistral': 3496, 'n_tokens_neox': 3343, 'n_words': 712}