<commit_before># -*- coding: utf-8 -*-
# Views relating to accepting/rejecting a reviewed proposal.
from django.contrib import messages

from django.http import HttpResponseRedirect
from django.template.defaultfilters import pluralize
from django.urls import reverse

from conf_site.core.views import SuperuserOnlyView
from conf_site.proposals.models import Proposal
from conf_site.reviews.models import ProposalNotification, ProposalResult
from conf_site.reviews.views import ProposalListView
from symposion.schedule.models import Presentation


class ProposalChangeResultPostView(SuperuserOnlyView):
    """A view to allow superusers to change a proposal's voting result."""

    http_method_names = ["get"]

    def get(self, *args, **kwargs):
        """Update an individual ProposalResult object."""
        proposal = Proposal.objects.get(pk=kwargs["pk"])
        result = ProposalResult.objects.get_or_create(proposal=proposal)[0]
        result.status = kwargs["status"]
        result.save()

        return HttpResponseRedirect(
            reverse("review_proposal_detail", args=[proposal.id])
        )


class ProposalResultListView(SuperuserOnlyView, ProposalListView):
    def get(self, request, *args, **kwargs):
        self.status = kwargs["status"]
        if self.status == ProposalResult.RESULT_UNDECIDED:
            # Create ProposalResults for proposals that do not have them.
            proposals_without_result = Proposal.objects.filter(
                review_result=None
            )
            for proposal in proposals_without_result:
                ProposalResult.objects.create(
                    proposal=proposal, status=ProposalResult.RESULT_UNDECIDED
                )
        return super(ProposalResultListView, self).get(
            request, *args, **kwargs
        )

    def get_queryset(self):
        return Proposal.objects.order_by("pk").filter(
            review_result__status=self.status
        )

    def get_context_data(self, **kwargs):
        context = super(ProposalResultListView, self).get_context_data(
            **kwargs
        )
        temp_result = ProposalResult(status=self.status)
        context["proposal_category"] = temp_result.get_status_display()
        return context


class ProposalMultieditPostView(SuperuserOnlyView):
    """A view to let superusers modify multiple proposals' results."""

    http_method_names = ["post"]

    def post(self, *args, **kwargs):
        proposal_pks = self.request.POST.getlist("proposal_pk")
        proposals = Proposal.objects.filter(pk__in=proposal_pks)
        new_status = self.request.POST.get("mark_status")
        if new_status:
            # <queryset>.update() will not work here because
            # the status field lives in the related model
            # ProposalResult.
            for proposal in proposals:
                try:<commit_msg>Exclude cancelled proposals from result views.<commit_after># -*- coding: utf-8 -*-
# Views relating to accepting/rejecting a reviewed proposal.
from django.contrib import messages

from django.http import HttpResponseRedirect
from django.template.defaultfilters import pluralize
from django.urls import reverse

from conf_site.core.views import SuperuserOnlyView
from conf_site.proposals.models import Proposal
from conf_site.reviews.models import ProposalNotification, ProposalResult
from conf_site.reviews.views import ProposalListView
from symposion.schedule.models import Presentation


class ProposalChangeResultPostView(SuperuserOnlyView):
    """A view to allow superusers to change a proposal's voting result."""

    http_method_names = ["get"]

    def get(self, *args, **kwargs):
        """Update an individual ProposalResult object."""
        proposal = Proposal.objects.get(pk=kwargs["pk"])
        result = ProposalResult.objects.get_or_create(proposal=proposal)[0]
        result.status = kwargs["status"]
        result.save()

        return HttpResponseRedirect(
            reverse("review_proposal_detail", args=[proposal.id])
        )


class ProposalResultListView(SuperuserOnlyView, ProposalListView):
    def get(self, request, *args, **kwargs):
        self.status = kwargs["status"]
        if self.status == ProposalResult.RESULT_UNDECIDED:
            # Create ProposalResults for proposals that do not have them.
            proposals_without_result = Proposal.objects.filter(
                review_result=None
            )
            for proposal in proposals_without_result:
                ProposalResult.objects.create(
                    proposal=proposal, status=ProposalResult.RESULT_UNDECIDED
                )
        return super(ProposalResultListView, self).get(
            request, *args, **kwargs
        )

    def get_queryset(self):
        return (
            Proposal.objects.order_by("pk")
            .exclude(cancelled=True)
            .filter(review_result__status=self.status)
            .select_related("speaker", "review_result")
        )

    def get_context_data(self, **kwargs):
        context = super(ProposalResultListView, self).get_context_data(
            **kwargs
        )
        temp_result = ProposalResult(status=self.status)
        context["proposal_category"] = temp_result.get_status_display()
        return context


class ProposalMultieditPostView(SuperuserOnlyView):
    """A view to let superusers modify multiple proposals' results."""

    http_method_names = ["post"]

    def post(self, *args, **kwargs):
        proposal_pks = self.request.POST.getlist("proposal_pk")
        proposals = Proposal.objects.filter(pk__in=proposal_pks)
        new_status = self.request.POST.get("mark_status")
        if new_status:
            # <queryset>.update() will not work here because
            # the status field lives in the related model
            # ProposalResult.
            for proposal in proposals:
                try:
{'fraction_non_alphanumeric': 0.08798101373114087, 'fraction_numerical': 0.0006780810306831666, 'mean_word_length': 2.9464882943143813, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3084507', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1543, 'n_words': 373}