<commit_before>from rest_framework import viewsets

from muckrock.jurisdiction.models import Jurisdiction, Exemption
from muckrock.jurisdiction.serializers import JurisdictionSerializer, ExemptionSerializer

class JurisdictionViewSet(viewsets.ModelViewSet):
    """API views for Jurisdiction"""
    # pylint: disable=too-many-ancestors
    # pylint: disable=too-many-public-methods
    queryset = Jurisdiction.objects.select_related('parent__parent').order_by()
    serializer_class = JurisdictionSerializer
    filter_fields = ('name', 'abbrev', 'level', 'parent')


class ExemptionViewSet(viewsets.ModelViewSet):
    """API views for Exemption"""
    queryset = (Exemption.objects.select_related('jurisdiction__parent__parent')
                                 .prefetch_related('example_appeals'))
    serializer_class = ExemptionSerializer
    filter_fields = ('name', 'jurisdiction')
<commit_msg>adds search endpoint to exemption api<commit_after>from django.db.models import Q

from rest_framework.decorators import list_route
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from muckrock.jurisdiction.models import Jurisdiction, Exemption
from muckrock.jurisdiction.serializers import JurisdictionSerializer, ExemptionSerializer

class JurisdictionViewSet(ModelViewSet):
    """API views for Jurisdiction"""
    # pylint: disable=too-many-ancestors
    # pylint: disable=too-many-public-methods
    queryset = Jurisdiction.objects.select_related('parent__parent').order_by()
    serializer_class = JurisdictionSerializer
    filter_fields = ('name', 'abbrev', 'level', 'parent')


class ExemptionViewSet(ModelViewSet):
    """
    The Exemption model provides a list of individual exemption cases along with some
    example appeal language.

    Search exemptions with the API at the `/exemption/search/` endpoint.
    """
    queryset = (Exemption.objects.select_related('jurisdiction__parent__parent')
                                 .prefetch_related('example_appeals'))
    serializer_class = ExemptionSerializer
    filter_fields = ('name', 'jurisdiction')

    @list_route()
    def search(self, request):
        """
        Allow searches against the collection of exemptions.
        Jurisdiction is an optional filter.
        """
        query = request.query_params.get('q')
        jurisdiction = request.query_params.get('jurisdiction')
        if query is None:
            raise ValidationError({'Error': 'Must provide a query'})
        results = self.queryset.filter(Q(name__contains=query)|Q(basis__contains=query))
        if jurisdiction:
            results = results.filter(jurisdiction__pk=jurisdiction)
        page = self.paginate_queryset(results)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = self.get_serializer(results, many=True)
        return Response(serializer.data)
{'fraction_non_alphanumeric': 0.08812890496547189, 'fraction_numerical': 0.0, 'mean_word_length': 4.011532125205931, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7228230', 'n_tokens_mistral': 858, 'n_tokens_neox': 790, 'n_words': 189}