<commit_before>import datetime

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

from datetimewidget.widgets import DateWidget
from haystack.query import SearchQuerySet


class SearchForm(forms.Form):
    q = forms.CharField(required=False, label=_('Text'),
                        widget=forms.TextInput())  # attrs={'type': 'search'}))
    # has_releases = forms.BooleanField(label=_("Has Releases"),
    #                                   required=False, initial=True)
    mixed_name = forms.CharField(required=False, label=_('Mixed Name'),
                                 widget=forms.TextInput())
    first_name = forms.CharField(required=False, label=_('First Name'),
                                 widget=forms.TextInput())
    last_name = forms.CharField(required=False, label=_('Last Name'),
                                widget=forms.TextInput())
    initiated_name = forms.CharField(required=False, label=_('Initiated Name'),
                                     widget=forms.TextInput())
    email = forms.CharField(required=False, label=_('Email'),
                            widget=forms.TextInput())
    pan_card_number = forms.CharField(required=False,
                                      label=_('Pan Card Number'),
                                      widget=forms.TextInput())
    serial_number = forms.CharField(required=False, label=_('Serial Number'),
                                    widget=forms.TextInput())
    phone_number = forms.CharField(required=False, label=_('Phone Number'),
                                   widget=forms.TextInput())
    book_number = forms.CharField(required=False, label=_('Book Number'),
                                  widget=forms.TextInput())
    slip_number = forms.CharField(required=False, label=_('Slip Number'),
                                  widget=forms.TextInput())
    transaction_id = forms.CharField(required=False, label=_('Transaction ID'),
                                     widget=forms.TextInput())
    record_id = forms.CharField(required=False, label=_('Record ID'),
                                widget=forms.TextInput())

    DATE_TYPE_CHOICES = (
        (u'receipt_date', _('Receipt Date')),
        (u'cleared_on', _('Cleared On')),
        (u'dated', _('Dated')),
        (u'created', _('Created on')),
        (u'modified', _('Last modified on')),
    )

    date_type = forms.ChoiceField(required=False, label='Date Type',
                                  choices=DATE_TYPE_CHOICES)
    date_from = forms.CharField(
        required=False, label=_('Date From'),
        widget=DateWidget(attrs={'id': "date-from"},
                          usel10n=True, bootstrap_version=3))
    date_to = forms.CharField(
        required=False, label=_('Date To'),
        widget=DateWidget(attrs={'id': "date-to"},
                          usel10n=True, bootstrap_version=3))

    def __init__(self, *args, **kwargs):
        self.searchqueryset = kwargs.pop('searchqueryset', None)
        self.load_all = kwargs.pop('load_all', False)

        if self.searchqueryset is None:
            self.searchqueryset = SearchQuerySet()

        super(SearchForm, self).__init__(*args, **kwargs)

    def search(self):
        if not self.is_valid():
            return self.show_all()

        sqs = self.show_all()

        if self.cleaned_data.get('q'):
            # sqs = sqs.auto_query(self.cleaned_data['q'])
            sqs = sqs.filter(text__contains=self.cleaned_data['q'])

        # fields which are filtered with __contains
        contains = [
            'email', 'serial_number', 'phone_number',
        ]

        for field_name in contains:
            if self.cleaned_data.get(field_name):
                sqs = sqs.filter(**{'%s__contains' % field_name:
                                    self.cleaned_data[field_name]})

        # fields which are filtered with __startswith
        starts_with = [
            'mixed_name', 'initiated_name', 'first_name', 'middle_name',
            'last_name', 'pan_card_number', 'record_id',
            'book_number', 'slip_number', 'transaction_id']

        for field_name in starts_with:
            if self.cleaned_data.get(field_name):
                sqs = sqs.filter(**{'%s__startswith' % field_name:
                                    self.cleaned_data[field_name]})

        print(self.DATE_TYPE_CHOICES)
        print(self.cleaned_data.get('date_type'))
        date_type = 'cleared_on'
        field_name = 'date_from'
        if self.cleaned_data.get(field_name):
            filter_date = datetime.datetime.strptime(
                self.cleaned_data.get(field_name), '%Y-%m-%d')
            sqs = sqs.filter(**{'%s__gte' % date_type: filter_date})

        field_name = 'date_to'
        if self.cleaned_data.get(field_name):
            filter_date = datetime.datetime.strptime(
                self.cleaned_data.get(field_name), '%Y-%m-%d')
            sqs = sqs.filter(**{'%s__lte' % date_type: filter_date})

        return sqs

    def show_all(self):
        return self.searchqueryset.order_by('-modified')<commit_msg>Set date_type filter for search.<commit_after>import datetime

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

from datetimewidget.widgets import DateWidget
from haystack.query import SearchQuerySet


class SearchForm(forms.Form):
    q = forms.CharField(required=False, label=_('Text'),
                        widget=forms.TextInput())  # attrs={'type': 'search'}))
    # has_releases = forms.BooleanField(label=_("Has Releases"),
    #                                   required=False, initial=True)
    mixed_name = forms.CharField(required=False, label=_('Mixed Name'),
                                 widget=forms.TextInput())
    first_name = forms.CharField(required=False, label=_('First Name'),
                                 widget=forms.TextInput())
    last_name = forms.CharField(required=False, label=_('Last Name'),
                                widget=forms.TextInput())
    initiated_name = forms.CharField(required=False, label=_('Initiated Name'),
                                     widget=forms.TextInput())
    email = forms.CharField(required=False, label=_('Email'),
                            widget=forms.TextInput())
    pan_card_number = forms.CharField(required=False,
                                      label=_('Pan Card Number'),
                                      widget=forms.TextInput())
    serial_number = forms.CharField(required=False, label=_('Serial Number'),
                                    widget=forms.TextInput())
    phone_number = forms.CharField(required=False, label=_('Phone Number'),
                                   widget=forms.TextInput())
    book_number = forms.CharField(required=False, label=_('Book Number'),
                                  widget=forms.TextInput())
    slip_number = forms.CharField(required=False, label=_('Slip Number'),
                                  widget=forms.TextInput())
    transaction_id = forms.CharField(required=False, label=_('Transaction ID'),
                                     widget=forms.TextInput())
    record_id = forms.CharField(required=False, label=_('Record ID'),
                                widget=forms.TextInput())

    DATE_TYPE_CHOICES = (
        (u'receipt_date', _('Receipt Date')),
        (u'cleared_on', _('Cleared On')),
        (u'dated', _('Dated')),
        (u'created', _('Created on')),
        (u'modified', _('Last modified on')),
    )

    date_type = forms.ChoiceField(required=False, label='Date Type',
                                  choices=DATE_TYPE_CHOICES)
    date_from = forms.CharField(
        required=False, label=_('Date From'),
        widget=DateWidget(attrs={'id': "date-from"},
                          usel10n=True, bootstrap_version=3))
    date_to = forms.CharField(
        required=False, label=_('Date To'),
        widget=DateWidget(attrs={'id': "date-to"},
                          usel10n=True, bootstrap_version=3))

    def __init__(self, *args, **kwargs):
        self.searchqueryset = kwargs.pop('searchqueryset', None)
        self.load_all = kwargs.pop('load_all', False)

        if self.searchqueryset is None:
            self.searchqueryset = SearchQuerySet()

        super(SearchForm, self).__init__(*args, **kwargs)

    def search(self):
        if not self.is_valid():
            return self.show_all()

        sqs = self.show_all()

        if self.cleaned_data.get('q'):
            # sqs = sqs.auto_query(self.cleaned_data['q'])
            sqs = sqs.filter(text__contains=self.cleaned_data['q'])

        # fields which are filtered with __contains
        contains = [
            'email', 'serial_number', 'phone_number',
        ]

        for field_name in contains:
            if self.cleaned_data.get(field_name):
                sqs = sqs.filter(**{'%s__contains' % field_name:
                                    self.cleaned_data[field_name]})

        # fields which are filtered with __startswith
        starts_with = [
            'mixed_name', 'initiated_name', 'first_name', 'middle_name',
            'last_name', 'pan_card_number', 'record_id',
            'book_number', 'slip_number', 'transaction_id']

        for field_name in starts_with:
            if self.cleaned_data.get(field_name):
                sqs = sqs.filter(**{'%s__startswith' % field_name:
                                    self.cleaned_data[field_name]})

        date_type = self.cleaned_data.get('date_type')
        field_name = 'date_from'
        if self.cleaned_data.get(field_name):
            filter_date = datetime.datetime.strptime(
                self.cleaned_data.get(field_name), '%Y-%m-%d')
            sqs = sqs.filter(**{'%s__gte' % date_type: filter_date})

        field_name = 'date_to'
        if self.cleaned_data.get(field_name):
            filter_date = datetime.datetime.strptime(
                self.cleaned_data.get(field_name), '%Y-%m-%d')
            sqs = sqs.filter(**{'%s__lte' % date_type: filter_date})

        return sqs

    def show_all(self):
        return self.searchqueryset.order_by('-modified')
{'fraction_non_alphanumeric': 0.1242760380877589, 'fraction_numerical': 0.001177971925002454, 'mean_word_length': 2.1089411046689044, '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': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1163129', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2855, 'n_words': 493}