<commit_before># Miro Community - Easiest way to make a video website
#
# Copyright (C) 2010, 2011, 2012 Participatory Culture Foundation
#
# Miro Community is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# Miro Community is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Miro Community.  If not, see <http://www.gnu.org/licenses/>.

import logging
import operator
import sys

from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
from django.db.models.query import QuerySet
from django.utils.translation import ugettext_lazy as _
from django.utils.datastructures import SortedDict
from haystack.forms import SearchForm
from haystack.query import SearchQuerySet
from tagging.models import Tag, TaggedItem
from tagging.utils import get_tag_list

from localtv.models import Video, Category, Feed
from localtv.playlists.models import Playlist
from localtv.search.query import SmartSearchQuerySet
from localtv.search.utils import (BestDateSort, FeaturedSort, ApprovedSort,
                                  PopularSort, DummySort, _q_for_queryset)
from localtv.settings import USE_HAYSTACK


class SmartSearchForm(SearchForm):
    def __init__(self, *args, **kwargs):
        sqs = kwargs.get('searchqueryset', None)
        if sqs is None:
            kwargs['searchqueryset'] = SmartSearchQuerySet()
        super(SmartSearchForm, self).__init__(*args, **kwargs)

    def no_query_found(self):
        return self.searchqueryset.all()


class FilterField(forms.ModelMultipleChoiceField):
    widget = forms.CheckboxSelectMultiple

    def __init__(self, queryset, cache_choices=False, required=False, *args,
                 **kwargs):
        super(FilterField, self).__init__(queryset, cache_choices, required,
                                          *args, **kwargs)

    def filter(self, queryset, values):
        """
        Returns a queryset filtered to match any of the given ``values`` in
        :attr:`field`.

        """
        pks = [instance.pk for instance in values]
        qs = [_q_for_queryset(queryset, lookup, pks)
              for lookup in self.field_lookups]
        return queryset.filter(reduce(operator.or_, qs))


class ModelFilterField(FilterField):
    def __init__(self, queryset, field_lookups=None, cache_choices=False,
                 required=False, widget=None, label=None, *args, **kwargs):
        self.field_lookups = field_lookups
        if label is None:
            label = queryset.model._meta.verbose_name_plural
        super(ModelFilterField, self).__init__(queryset, cache_choices,
                                               required, widget, label, *args,
                                               **kwargs)

    def clean(self, value):
        if (isinstance(value, QuerySet) or
            (isinstance(value, (list, tuple)) and
             isinstance(value[0], self.queryset.model))):
            key = self.to_field_name or 'pk'
            value = [getattr(o, key) for o in value]
        return super(ModelFilterField, self).clean(value)


class TagFilterField(ModelFilterField):
    def __init__(self, *args, **kwargs):
        queryset = Tag.objects.usage_for_queryset(Video.objects.filter(
                                              site=Site.objects.get_current(),
                                              status=Video.ACTIVE))
        super(TagFilterField, self).__init__(queryset, *args, **kwargs)

    def clean(self, value):
        if self.required and not value:
            raise ValidationError(self.error_messages['required'])
        elif not self.required and not value:
            return []
        try:
            return get_tag_list(value)
        except ValueError:
            raise ValidationError(self.error_messages['list'])

    def filter(self, queryset, values):
        if not isinstance(queryset, SearchQuerySet):
            return TaggedItem.objects.get_union_by_model(queryset, values)
        return super(TagFilterField, self).filter(queryset, values)


class SortFilterForm(SmartSearchForm):
    """
    Handles searching, filtering, and sorting for video queries.

    """
    sorts = SortedDict((
        ('newest', BestDateSort()),
        ('oldest', BestDateSort(descending=False)),<commit_msg>Avoid getting current Site during TagFilterField init.<commit_after># Miro Community - Easiest way to make a video website
#
# Copyright (C) 2010, 2011, 2012 Participatory Culture Foundation
#
# Miro Community is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# Miro Community is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Miro Community.  If not, see <http://www.gnu.org/licenses/>.

import logging
import operator
import sys

from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db.models.query import QuerySet
from django.utils.translation import ugettext_lazy as _
from django.utils.datastructures import SortedDict
from haystack.forms import SearchForm
from haystack.query import SearchQuerySet
from tagging.models import Tag, TaggedItem
from tagging.utils import get_tag_list

from localtv.models import Video, Category, Feed
from localtv.playlists.models import Playlist
from localtv.search.query import SmartSearchQuerySet
from localtv.search.utils import (BestDateSort, FeaturedSort, ApprovedSort,
                                  PopularSort, DummySort, _q_for_queryset)
from localtv.settings import USE_HAYSTACK


class SmartSearchForm(SearchForm):
    def __init__(self, *args, **kwargs):
        sqs = kwargs.get('searchqueryset', None)
        if sqs is None:
            kwargs['searchqueryset'] = SmartSearchQuerySet()
        super(SmartSearchForm, self).__init__(*args, **kwargs)

    def no_query_found(self):
        return self.searchqueryset.all()


class FilterField(forms.ModelMultipleChoiceField):
    widget = forms.CheckboxSelectMultiple

    def __init__(self, queryset, cache_choices=False, required=False, *args,
                 **kwargs):
        super(FilterField, self).__init__(queryset, cache_choices, required,
                                          *args, **kwargs)

    def filter(self, queryset, values):
        """
        Returns a queryset filtered to match any of the given ``values`` in
        :attr:`field`.

        """
        pks = [instance.pk for instance in values]
        qs = [_q_for_queryset(queryset, lookup, pks)
              for lookup in self.field_lookups]
        return queryset.filter(reduce(operator.or_, qs))


class ModelFilterField(FilterField):
    def __init__(self, queryset, field_lookups=None, cache_choices=False,
                 required=False, widget=None, label=None, *args, **kwargs):
        self.field_lookups = field_lookups
        if label is None:
            label = queryset.model._meta.verbose_name_plural
        super(ModelFilterField, self).__init__(queryset, cache_choices,
                                               required, widget, label, *args,
                                               **kwargs)

    def clean(self, value):
        if (isinstance(value, QuerySet) or
            (isinstance(value, (list, tuple)) and
             isinstance(value[0], self.queryset.model))):
            key = self.to_field_name or 'pk'
            value = [getattr(o, key) for o in value]
        return super(ModelFilterField, self).clean(value)


class TagFilterField(ModelFilterField):
    def __init__(self, *args, **kwargs):
        queryset = Tag.objects.usage_for_queryset(Video.objects.filter(
                                              site=settings.SITE_ID,
                                              status=Video.ACTIVE))
        super(TagFilterField, self).__init__(queryset, *args, **kwargs)

    def clean(self, value):
        if self.required and not value:
            raise ValidationError(self.error_messages['required'])
        elif not self.required and not value:
            return []
        try:
            return get_tag_list(value)
        except ValueError:
            raise ValidationError(self.error_messages['list'])

    def filter(self, queryset, values):
        if not isinstance(queryset, SearchQuerySet):
            return TaggedItem.objects.get_union_by_model(queryset, values)
        return super(TagFilterField, self).filter(queryset, values)


class SortFilterForm(SmartSearchForm):
    """
    Handles searching, filtering, and sorting for video queries.

    """
    sorts = SortedDict((
        ('newest', BestDateSort()),
        ('oldest', BestDateSort(descending=False)),
{'fraction_non_alphanumeric': 0.08336794019933555, 'fraction_numerical': 0.0029069767441860465, 'mean_word_length': 2.859375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '886122', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2491, 'n_words': 801}