<commit_before>from django.db.models import Max, Q

from rest_framework.decorators import list_route
from rest_framework.response import Response

from resolwe.flow.models import Collection
from resolwe.flow.views import CollectionViewSet


class SampleViewSet(CollectionViewSet):
    queryset = Collection.objects.filter(descriptor_schema__slug='sample').prefetch_related('descriptor_schema')

    @list_route(methods=[u'get'])
    def annotated(self, request):
        """Return list of annotated `Samples`."""
        queryset = self.get_queryset().annotate(
            latest_date=Max('data__created')
        ).filter(
            Q(descriptor__has_key='geo') & Q(descriptor__geo__has_key='annotator')
        ).order_by('-latest_date')
        serializer = self.serializer_class(queryset, many=True, context={'request': request})

        return Response(serializer.data)

    @list_route(methods=[u'get'])
    def unannotated(self, request):
        """Return list of unannotated `Samples`."""
        queryset = self.get_queryset().annotate(
            latest_date=Max('data__created')
        ).filter(
            ~Q(descriptor__has_key='geo') | ~Q(descriptor__geo__has_key='annotator')
        ).order_by('-latest_date')
        serializer = self.serializer_class(queryset, many=True, context={'request': request})

        return Response(serializer.data)
<commit_msg>Change sample viewset to filter based on query params<commit_after>from __future__ import absolute_import, division, print_function, unicode_literals

from django.db.models import Max, Q

from resolwe.flow.models import Collection
from resolwe.flow.views import CollectionViewSet


class SampleViewSet(CollectionViewSet):

    def get_queryset(self):
        queryset = Collection.objects.annotate(
            latest_date=Max('data__created')
        ).filter(
            descriptor_schema__slug='sample'
        ).prefetch_related('descriptor_schema')

        annotated = self.request.query_params.get('annotated', None)

        if annotated == "1":
            queryset = queryset.filter(Q(descriptor__has_key='geo') & Q(descriptor__geo__has_key='annotator'))
        elif annotated == "0":
            queryset = queryset.filter(~Q(descriptor__has_key='geo') | ~Q(descriptor__geo__has_key='annotator'))

        return queryset.order_by('-latest_date')

{'fraction_non_alphanumeric': 0.11315115286080274, 'fraction_numerical': 0.0008539709649871904, 'mean_word_length': 3.9430379746835444, 'pattern_counts': {'":': 2, '<': 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': '3709733', 'n_tokens_mistral': 677, 'n_tokens_neox': 654, 'n_words': 126}