<commit_before>'''
Basic filterspecs that can also be used as startpoint for custom ones.
'''
import datetime
import warnings

from django.utils.translation import ugettext_lazy as _
from django import forms

from datafilters.filterspec import FilterSpec

__all__ = (
    'BoolFilterSpec',
    'ContainsFilterSpec',
    'DateFieldFilterSpec',
    'DatePickFilterSpec',
    'GenericSpec',
    'GreaterThanFilterSpec',
    'GreaterThanZeroFilterSpec',
    'SelectBoolFilterSpec',
)


class GenericSpec(FilterSpec):

    def __init__(self, *args, **kwargs):
        warnings.warn(
            'GenericSpec is deprecated. Use datafilters.filterspec.FilterSpec',
            DeprecationWarning)
        super(GenericSpec, self).__init__(*args, **kwargs)


class DateFieldFilterSpec(FilterSpec):

    field_cls = forms.ChoiceField

    def __init__(self, field_name, label=None, is_datetime=True,
            base_date_fun=datetime.date.today, **field_kwargs):

        field_kwargs['label'] = label
        super(DateFieldFilterSpec, self).__init__(field_name, **field_kwargs)
        self.is_datetime = is_datetime
        self.base_date_fun = base_date_fun
        self.lookup_kwarg_since = '%s__gte' % self.field_name
        self.lookup_kwarg_until = '%s__lt' % self.field_name

        self.filter_choices = {
            'all': lambda today, tomorrow: {},
            'today': lambda today, tomorrow: {
                self.lookup_kwarg_since: str(today),
                self.lookup_kwarg_until: str(tomorrow),
            },
            'this_week': lambda today, tomorrow: {
                self.lookup_kwarg_since: str(today - datetime.timedelta(days=7)),
                self.lookup_kwarg_until: str(tomorrow),
            },
            'this_month': lambda today, tomorrow: {
                self.lookup_kwarg_since: str(today.replace(day=1)),
                self.lookup_kwarg_until: str(tomorrow),
            },
            'this_year': lambda today, tomorrow: {
                self.lookup_kwarg_since: str(today.replace(month=1, day=1)),
                self.lookup_kwarg_until: str(tomorrow),
            },
        }

    def get_field_kwargs(self):
        kwargs = super(DateFieldFilterSpec, self).get_field_kwargs()
        kwargs['choices'] = (
            ('all', _('Any date')),
            ('today', _('Today')),
            ('this_week', _('Past 7 days')),
            ('this_month', _('This month')),
            ('this_year', _('This year')),<commit_msg>Allows the use of date ranges (from and to dates in models).<commit_after>'''
Basic filterspecs that can also be used as startpoint for custom ones.
'''
import datetime
import warnings

from django.utils.translation import ugettext_lazy as _
from django import forms

from datafilters.filterspec import FilterSpec

__all__ = (
    'BoolFilterSpec',
    'ContainsFilterSpec',
    'DateFieldFilterSpec',
    'DatePickFilterSpec',
    'GenericSpec',
    'GreaterThanFilterSpec',
    'GreaterThanZeroFilterSpec',
    'SelectBoolFilterSpec',
)


class GenericSpec(FilterSpec):

    def __init__(self, *args, **kwargs):
        warnings.warn(
            'GenericSpec is deprecated. Use datafilters.filterspec.FilterSpec',
            DeprecationWarning)
        super(GenericSpec, self).__init__(*args, **kwargs)


class DateFieldFilterSpec(FilterSpec):

    field_cls = forms.ChoiceField

    def __init__(self, field_name, label=None, is_datetime=True,
            base_date_fun=datetime.date.today, **field_kwargs):

        field_kwargs['label'] = label
        super(DateFieldFilterSpec, self).__init__(field_name, **field_kwargs)
        self.is_datetime = is_datetime
        self.base_date_fun = base_date_fun

        if isinstance(self.field_name, (tuple, list)):
            since_field = self.field_name[0]
            till_field = self.field_name[1]
        else:
            till_field = since_field = self.field_name


        self.lookup_kwarg_since = '%s__gte' % till_field
        self.lookup_kwarg_until = '%s__lt' % since_field

        self.filter_choices = {
            'all': lambda today, tomorrow: {},
            'today': lambda today, tomorrow: {
                self.lookup_kwarg_since: str(today),
                self.lookup_kwarg_until: str(tomorrow),
            },
            'this_week': lambda today, tomorrow: {
                self.lookup_kwarg_since: str(today - datetime.timedelta(days=7)),
                self.lookup_kwarg_until: str(tomorrow),
            },
            'this_month': lambda today, tomorrow: {
                self.lookup_kwarg_since: str(today.replace(day=1)),
                self.lookup_kwarg_until: str(tomorrow),
            },
            'this_year': lambda today, tomorrow: {
                self.lookup_kwarg_since: str(today.replace(month=1, day=1)),
                self.lookup_kwarg_until: str(tomorrow),
            },
        }

    def get_field_kwargs(self):
        kwargs = super(DateFieldFilterSpec, self).get_field_kwargs()
        kwargs['choices'] = (
            ('all', _('Any date')),
            ('today', _('Today')),
            ('this_week', _('Past 7 days')),
            ('this_month', _('This month')),
            ('this_year', _('This year')),
{'fraction_non_alphanumeric': 0.11415963161933998, 'fraction_numerical': 0.0023023791250959325, 'mean_word_length': 2.551089918256131, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7121830', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1503, 'n_words': 304}