<commit_before>"""
This module contains Mixins.

"""
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.urlresolvers import reverse
from django.http import HttpResponseForbidden
from django.template.loader import render_to_string
from django.utils.text import slugify

from rest_framework import status
from rest_framework.response import Response

from . permissions import (
    ContentPermissions,
    permission_required,
    superuser_required
)
from . utils import num_user_selections


# Mixins for Views
# ----------------
class DeleteMultipleMixin:
    """A Mixin that allows deleting multiple objects when a DELETE request is
    sent with a list of modelnames and object IDs.

    """
    def delete(self, request, *args, **kwargs):
        # XXX why is auth not handled by the authentication_classes, here?

        model = self.get_queryset().model
        model_name = model.__name__.lower()

        if isinstance(request.data, list) and request.user.is_authenticated():
            # We're deleting multiple items; just assume they exist?
            params = {
                "pk__in": filter(None, (d[model_name] for d in request.data)),
                "user": request.user.id,
            }
            model.objects.filter(**params).delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
        return Response(status=status.HTTP_400_BAD_REQUEST)


class StateFilterMixin:
    """A mixin that provides a default filter for objects with a `state`."""

    def get(self, request, *args, **kwargs):
        self.state_filter = request.GET.get('state', 'draft')
        return super().get(request, *args, **kwargs)

    def get_queryset(self):
        queryset = super().get_queryset()
        states = ['draft', 'published', 'pending-review', 'declined']
        if self.state_filter in states:<commit_msg>Save the last-used state filter in a session<commit_after>"""
This module contains Mixins.

"""
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.urlresolvers import reverse
from django.http import HttpResponseForbidden
from django.template.loader import render_to_string
from django.utils.text import slugify

from rest_framework import status
from rest_framework.response import Response

from . permissions import (
    ContentPermissions,
    permission_required,
    superuser_required
)
from . utils import num_user_selections


# Mixins for Views
# ----------------
class DeleteMultipleMixin:
    """A Mixin that allows deleting multiple objects when a DELETE request is
    sent with a list of modelnames and object IDs.

    """
    def delete(self, request, *args, **kwargs):
        # XXX why is auth not handled by the authentication_classes, here?

        model = self.get_queryset().model
        model_name = model.__name__.lower()

        if isinstance(request.data, list) and request.user.is_authenticated():
            # We're deleting multiple items; just assume they exist?
            params = {
                "pk__in": filter(None, (d[model_name] for d in request.data)),
                "user": request.user.id,
            }
            model.objects.filter(**params).delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
        return Response(status=status.HTTP_400_BAD_REQUEST)


class StateFilterMixin:
    """A mixin that provides a default filter for objects with a `state`."""

    def get(self, request, *args, **kwargs):
        current_state_filter = request.session.get('state-filter', 'draft')
        self.state_filter = request.GET.get('state', current_state_filter)

        # If the filter has changed, save it as the current state in the session
        if self.state_filter != current_state_filter:
            request.session['state-filter'] = self.state_filter

        return super().get(request, *args, **kwargs)

    def get_queryset(self):
        queryset = super().get_queryset()
        states = ['draft', 'published', 'pending-review', 'declined']
        if self.state_filter in states:
{'fraction_non_alphanumeric': 0.09498327759197324, 'fraction_numerical': 0.0026755852842809363, 'mean_word_length': 3.3468992248062017, 'pattern_counts': {'":': 4, '<': 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': '7635433', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1178, 'n_words': 372}