<commit_before>import datetime

from django import forms
from django.db.models import Q
from selectable.forms import AutoCompleteWidget, AutoCompleteSelectField

from nc import models as stops
from nc.lookups import AgencyLookup


class SearchForm(forms.Form):
    agency = forms.CharField(
        label='Agency Name',
        widget=AutoCompleteWidget(AgencyLookup),
        help_text="ex: Durham Police Department"
    )
    officer = forms.CharField(required=False, help_text="ex: 227")
    start_date = forms.DateField(required=False,
                                 help_text="ex: 8/13/2012")
    end_date = forms.DateField(required=False,
                               help_text="ex: 8/13/2012")
    purpose = forms.MultipleChoiceField(required=False,
                                        choices=stops.PURPOSE_CHOICES,
                                        widget=forms.CheckboxSelectMultiple)
    action = forms.MultipleChoiceField(required=False,
                                       choices=stops.ACTION_CHOICES,
                                       widget=forms.CheckboxSelectMultiple)

    def clean(self):
        cleaned_data = super(SearchForm, self).clean()

        start_date = cleaned_data.get('start_date')
        end_date = cleaned_data.get('end_date')
        if start_date is not None and end_date is not None:

            if start_date > end_date:
                err = "End date must be greater than or equal to start-date"
                self.add_error('end_date', err)

            dt = end_date - start_date
            if dt.days > 366:  # allow for leap-year
                err = "Date-range must be less than or equal to one-year"
                self.add_error('end_date', err)

        return cleaned_data

    def clean_agency(self):
        agency = self.cleaned_data['agency']
        if agency:
            try:
                agency = stops.Agency.objects.get(name=agency)
            except stops.Agency.DoesNotExist:
                agency = None
        return agency

    def get_query(self):
        query = Q()
        agency = self.cleaned_data['agency']
        if agency:
            query &= Q(stop__agency=agency)
        officer = self.cleaned_data['officer']
        if officer:
            query &= Q(stop__officer_id=officer)
        start_date = self.cleaned_data['start_date']
        if start_date:
            query &= Q(stop__date__gte=start_date)
        end_date = self.cleaned_data['end_date']
        if end_date:
            query &= Q(stop__date__lte=end_date + datetime.timedelta(days=1))
        purpose = self.cleaned_data['purpose']
        if purpose:
            query &= Q(stop__purpose__in=purpose)
        action = self.cleaned_data['action']
        if action:<commit_msg>added age, race, and ethnicity to search form<commit_after>import datetime

from django import forms
from django.db.models import Q
from selectable.forms import AutoCompleteWidget, AutoCompleteSelectField

from nc import models as stops
from nc.lookups import AgencyLookup


def addNoneOpt(choices):
    opts = list(choices)
    opts.insert(0, (None, "---"))
    return opts


class SearchForm(forms.Form):
    agency = forms.CharField(
        label='Agency Name',
        widget=AutoCompleteWidget(AgencyLookup),
        help_text="ex: Durham Police Department"
    )
    officer = forms.CharField(required=False, help_text="ex: 227")
    start_date = forms.DateField(required=False,
                                 help_text="ex: 8/13/2012")
    end_date = forms.DateField(required=False,
                               help_text="ex: 8/13/2012")
    purpose = forms.MultipleChoiceField(required=False,
                                        choices=stops.PURPOSE_CHOICES,
                                        widget=forms.CheckboxSelectMultiple)
    action = forms.MultipleChoiceField(required=False,
                                       choices=stops.ACTION_CHOICES,
                                       widget=forms.CheckboxSelectMultiple)
    age = forms.IntegerField(
        required=False,
        min_value=0,
        max_value=130)
    race = forms.ChoiceField(
        required=False,
        initial=None,
        choices=addNoneOpt(stops.RACE_CHOICES))
    ethnicity = forms.ChoiceField(
        required=False,
        initial=None,
        choices=addNoneOpt(stops.ETHNICITY_CHOICES))

    def clean(self):
        cleaned_data = super(SearchForm, self).clean()

        start_date = cleaned_data.get('start_date')
        end_date = cleaned_data.get('end_date')
        if start_date is not None and end_date is not None:

            if start_date > end_date:
                err = "End date must be greater than or equal to start-date"
                self.add_error('end_date', err)

            dt = end_date - start_date
            if dt.days > 366:  # allow for leap-year
                err = "Date-range must be less than or equal to one-year"
                self.add_error('end_date', err)

        return cleaned_data

    def clean_agency(self):
        agency = self.cleaned_data['agency']
        if agency:
            try:
                agency = stops.Agency.objects.get(name=agency)
            except stops.Agency.DoesNotExist:
                agency = None
        return agency

    def get_query(self):
        query = Q()
        agency = self.cleaned_data['agency']
        if agency:
            query &= Q(stop__agency=agency)
        officer = self.cleaned_data['officer']
        if officer:
            query &= Q(stop__officer_id=officer)
        start_date = self.cleaned_data['start_date']
        if start_date:
            query &= Q(stop__date__gte=start_date)
        end_date = self.cleaned_data['end_date']
        if end_date:
            query &= Q(stop__date__lte=end_date + datetime.timedelta(days=1))
        age = self.cleaned_data['age']
        if age:
            query &= Q(stop__person__age=age) & Q(stop__person__type="D")
        race = self.cleaned_data['race']
        if race:
            query &= Q(stop__person__race=race) & Q(stop__person__type="D")
        ethnicity = self.cleaned_data['ethnicity']
        if ethnicity:
            query &= Q(stop__person__ethnicity=ethnicity) & Q(stop__person__type="D")
        purpose = self.cleaned_data['purpose']
        if purpose:
            query &= Q(stop__purpose__in=purpose)
        action = self.cleaned_data['action']
        if action:
{'fraction_non_alphanumeric': 0.09024541783162472, 'fraction_numerical': 0.00730040385212799, 'mean_word_length': 2.051658767772512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '854667', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1788, 'n_words': 386}