<commit_before>import django_filters

from datetime import date, timedelta
from fec_alerts.models import new_filing
from summary_data.models import Committee_Overlay, Authorized_Candidate_Committees, DistrictWeekly, District, Candidate_Overlay
from formdata.models import SkedE
from django.db.models import Q
from summary_data.utils.weekly_update_utils import get_week_number



class NFFilter(django_filters.FilterSet):
    
    # can create both ends of a range like this: 
    min_raised = django_filters.NumberFilter(name='tot_raised', lookup_type='gte')
    min_spent = django_filters.NumberFilter(name='tot_spent', lookup_type='gte')
    min_coh = django_filters.NumberFilter(name='coh_end', lookup_type='gte')

    
    filed_before = django_filters.DateFilter(name='filed_date', lookup_type='lte') 
    filed_after = django_filters.DateFilter(name='filed_date', lookup_type='gte') 
    

    class Meta:
        model = new_filing
        fields = ['fec_id', 'committee_name', 'filing_number', 'form_type', 'filed_date', 'coverage_from_date', 'coverage_to_date', 'is_superpac', 'committee_designation', 'committee_type', 'committee_slug', 'party', 'coh_end', 'new_loans', 'tot_raised', 'tot_spent', 'lines_present', 'is_superceded']
        

class COFilter(django_filters.FilterSet):

    # can create both ends of a range like this: 
    min_raised = django_filters.NumberFilter(name='total_receipts', lookup_type='gte')
    min_spent = django_filters.NumberFilter(name='total_disbursements', lookup_type='gte')
    min_coh = django_filters.NumberFilter(name='cash_on_hand', lookup_type='gte')

    class Meta:
        model = Committee_Overlay
        fields = ['fec_id', 'name', 'slug', 'cycle', 'term_class', 'total_receipts', 'total_disbursements', 'outstanding_loans', 'cash_on_hand', 'cash_on_hand_date', 'ctype', 'political_orientation']



class OSFilter(django_filters.FilterSet):

    # can create both ends of a range like this: 
    min_ies = django_filters.NumberFilter(name='total_indy_expenditures', lookup_type='gte')

    class Meta:
        model = Committee_Overlay
        fields = ['fec_id', 'name', 'total_receipts', 'total_disbursements', 'outstanding_loans', 'ctype', 'total_indy_expenditures','ie_support_dems', 'ie_oppose_dems', 'ie_support_reps', 'ie_oppose_reps', 'political_orientation', 'political_orientation_verified']


class DistrictFilter(django_filters.FilterSet):
    
    class Meta:
        model = District
        fields = ['id', 'cycle', 'state', 'office', 'office_district', 'term_class', 'incumbent_name', 'incumbent_party', 'next_election_date', 'next_election_code', 'open_seat', 'candidate_raised', 'candidate_spending', 'outside_spending', 'total_spending', 'rothenberg_rating_id', 'rothenberg_rating_text']
    


class CandidateFilter(django_filters.FilterSet):

    class Meta:
        model = Candidate_Overlay
        fields = ['name', 'fec_id', 'pcc', 'party', 'is_incumbent', 'cycle', 'not_seeking_reelection', 'other_office_sought', 'other_fec_id', 'election_year', 'state', 'office', 'office_district', 'term_class', 'candidate_status', 'total_expenditures', 'expenditures_supporting', 'expenditures_opposing', 'total_receipts', 'total_contributions', 'total_disbursements', 'cash_on_hand', 'cash_on_hand_date']


    
class DWFilter(django_filters.FilterSet):
    
    week_start = django_filters.NumberFilter(name='cycle_week_number', lookup_type='gte')
    week_end = django_filters.NumberFilter(name='cycle_week_number', lookup_type='lte')
    

    class Meta:
        model = DistrictWeekly
        fields = ['start_date', 'end_date', 'cycle_week_number', 'outside_spending']
    

class SkedEFilter(django_filters.FilterSet):
    
    min_spent = django_filters.NumberFilter(name='expenditure_amount', lookup_type='gte')
    
    class Meta:
        model = SkedE
        fields=('form_type', 'candidate_id_checked', 'candidate_party_checked', 'candidate_office_checked', 'candidate_state_checked', 'candidate_district_checked', 'support_oppose_checked', 'payee_state', 'expenditure_date_formatted', 'expenditure_amount', 'filer_committee_id_number', 'district_checked')
        
def yearFilter(queryset, querydict):
    try:
        year=int(querydict['year_covered'])
        queryset = queryset.filter(Q(coverage_from_date__gte=date(year,1,1), coverage_to_date__lte=date(year,12,31)))
    
    except (KeyError, ValueError):
        pass
    return queryset
    

def DWDistrictFilter(queryset, querydict):
    try:
        district_list=querydict['districts']
        
        # if there's no commas, it's just a single district
        if district_list.find(',') < 0:
            queryset = queryset.filter(district__pk=district_list)
            
        # if there's a comma, it's a comma-delimited list
        else:
            district_ids = district_list.split(',')
            queryset = queryset.filter(district__pk__in=district_ids)
    
    except KeyError:
        pass
        
    return queryset

def candidatedistrictFilter(queryset, querydict):
    try:
        id=int(querydict['district'])
        queryset = queryset.filter(district__pk=id)

    except (KeyError, ValueError):
        pass
        
    return queryset

def districtIDFilter(queryset, querydict):
    try:
        id=int(querydict['pk'])
        queryset = queryset.filter(pk=id)

    except (KeyError, ValueError):
        pass
    return queryset

def weekFilter(queryset, querydict):
    try:
        week=querydict['week']

        # if there's no commas, it's just a single district
        if week.upper() == "NOW":
            queryset = queryset.filter(cycle_week_number = get_week_number(date.today()) )
        if week.upper() == "LAST":
            queryset = queryset.filter(cycle_week_number = get_week_number(date.today())-1 )


    except KeyError:
        pass

    return queryset

def periodTypeFilter(queryset, querydict):
    try:
        period_type=querydict['period_type']
        if period_type.startswith('Q'):
            if period_type == 'Q1':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,1,1), coverage_to_date=date(2013,3,31))|Q(coverage_from_date=date(2014,1,1), coverage_to_date=date(2014,3,31)))
        
            elif period_type == 'Q2':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,4,1), coverage_to_date=date(2013,6,30))|Q(coverage_from_date=date(2014,4,1), coverage_to_date=date(2014,6,30)))

            elif period_type == 'Q3':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,7,1), coverage_to_date=date(2013,9,30))|Q(coverage_from_date=date(2014,7,1), coverage_to_date=date(2014,9,30)))

            elif period_type == 'Q4':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,10,1), coverage_to_date=date(2013,12,31))|Q(coverage_from_date=date(2014,10,1), coverage_to_date=date(2014,9,30)))


        elif period_type.startswith('M'):
                        
            if period_type == 'M1':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,1,1), coverage_to_date=date(2013,1,31))|Q(coverage_from_date=date(2014,1,1), coverage_to_date=date(2014,1,31)))

            elif period_type == 'M2':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,2,1), coverage_to_date=date(2013,2,28))|Q(coverage_from_date=date(2014,2,1), coverage_to_date=date(2014,2,28)))

            elif period_type == 'M3':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,3,1), coverage_to_date=date(2013,3,31))|Q(coverage_from_date=date(2014,3,1), coverage_to_date=date(2014,3,31)))

            elif period_type == 'M4':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,4,1), coverage_to_date=date(2013,4,30))|Q(coverage_from_date=date(2014,4,1), coverage_to_date=date(2014,4,30)))

            elif period_type == 'M5':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,5,1), coverage_to_date=date(2013,5,31))|Q(coverage_from_date=date(2014,5,1), coverage_to_date=date(2014,5,31)))

            elif period_type == 'M6':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,6,1), coverage_to_date=date(2013,6,30))|Q(coverage_from_date=date(2014,6,1), coverage_to_date=date(2014,6,30)))

            elif period_type == 'M7':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,7,1), coverage_to_date=date(2013,7,31))|Q(coverage_from_date=date(2014,7,1), coverage_to_date=date(2014,7,31)))

            elif period_type == 'M8':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,8,1), coverage_to_date=date(2013,8,31))|Q(coverage_from_date=date(2014,8,1), coverage_to_date=date(2014,8,31)))

            elif period_type == 'M9':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,9,1), coverage_to_date=date(2013,9,30))|Q(coverage_from_date=date(2014,9,1), coverage_to_date=date(2014,9,30)))

            elif period_type == 'M10':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,10,1), coverage_to_date=date(2013,10,31))|Q(coverage_from_date=date(2014,10,1), coverage_to_date=date(2014,10,31)))

            elif period_type == 'M11':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,11,1), coverage_to_date=date(2013,11,30))|Q(coverage_from_date=date(2014,11,1), coverage_to_date=date(2014,11,30)))

            elif period_type == 'M12':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,12,1), coverage_to_date=date(2013,12,31))|Q(coverage_from_date=date(2014,12,1), coverage_to_date=date(2014,12,31)))
<commit_msg>2015 for jan, feb<commit_after>import django_filters

from datetime import date, timedelta
from fec_alerts.models import new_filing
from summary_data.models import Committee_Overlay, Authorized_Candidate_Committees, DistrictWeekly, District, Candidate_Overlay
from formdata.models import SkedE
from django.db.models import Q
from summary_data.utils.weekly_update_utils import get_week_number



class NFFilter(django_filters.FilterSet):
    
    # can create both ends of a range like this: 
    min_raised = django_filters.NumberFilter(name='tot_raised', lookup_type='gte')
    min_spent = django_filters.NumberFilter(name='tot_spent', lookup_type='gte')
    min_coh = django_filters.NumberFilter(name='coh_end', lookup_type='gte')

    
    filed_before = django_filters.DateFilter(name='filed_date', lookup_type='lte') 
    filed_after = django_filters.DateFilter(name='filed_date', lookup_type='gte') 
    

    class Meta:
        model = new_filing
        fields = ['fec_id', 'committee_name', 'filing_number', 'form_type', 'filed_date', 'coverage_from_date', 'coverage_to_date', 'is_superpac', 'committee_designation', 'committee_type', 'committee_slug', 'party', 'coh_end', 'new_loans', 'tot_raised', 'tot_spent', 'lines_present', 'is_superceded']
        

class COFilter(django_filters.FilterSet):

    # can create both ends of a range like this: 
    min_raised = django_filters.NumberFilter(name='total_receipts', lookup_type='gte')
    min_spent = django_filters.NumberFilter(name='total_disbursements', lookup_type='gte')
    min_coh = django_filters.NumberFilter(name='cash_on_hand', lookup_type='gte')

    class Meta:
        model = Committee_Overlay
        fields = ['fec_id', 'name', 'slug', 'cycle', 'term_class', 'total_receipts', 'total_disbursements', 'outstanding_loans', 'cash_on_hand', 'cash_on_hand_date', 'ctype', 'political_orientation']



class OSFilter(django_filters.FilterSet):

    # can create both ends of a range like this: 
    min_ies = django_filters.NumberFilter(name='total_indy_expenditures', lookup_type='gte')

    class Meta:
        model = Committee_Overlay
        fields = ['fec_id', 'name', 'total_receipts', 'total_disbursements', 'outstanding_loans', 'ctype', 'total_indy_expenditures','ie_support_dems', 'ie_oppose_dems', 'ie_support_reps', 'ie_oppose_reps', 'political_orientation', 'political_orientation_verified']


class DistrictFilter(django_filters.FilterSet):
    
    class Meta:
        model = District
        fields = ['id', 'cycle', 'state', 'office', 'office_district', 'term_class', 'incumbent_name', 'incumbent_party', 'next_election_date', 'next_election_code', 'open_seat', 'candidate_raised', 'candidate_spending', 'outside_spending', 'total_spending', 'rothenberg_rating_id', 'rothenberg_rating_text']
    


class CandidateFilter(django_filters.FilterSet):

    class Meta:
        model = Candidate_Overlay
        fields = ['name', 'fec_id', 'pcc', 'party', 'is_incumbent', 'cycle', 'not_seeking_reelection', 'other_office_sought', 'other_fec_id', 'election_year', 'state', 'office', 'office_district', 'term_class', 'candidate_status', 'total_expenditures', 'expenditures_supporting', 'expenditures_opposing', 'total_receipts', 'total_contributions', 'total_disbursements', 'cash_on_hand', 'cash_on_hand_date']


    
class DWFilter(django_filters.FilterSet):
    
    week_start = django_filters.NumberFilter(name='cycle_week_number', lookup_type='gte')
    week_end = django_filters.NumberFilter(name='cycle_week_number', lookup_type='lte')
    

    class Meta:
        model = DistrictWeekly
        fields = ['start_date', 'end_date', 'cycle_week_number', 'outside_spending']
    

class SkedEFilter(django_filters.FilterSet):
    
    min_spent = django_filters.NumberFilter(name='expenditure_amount', lookup_type='gte')
    
    class Meta:
        model = SkedE
        fields=('form_type', 'candidate_id_checked', 'candidate_party_checked', 'candidate_office_checked', 'candidate_state_checked', 'candidate_district_checked', 'support_oppose_checked', 'payee_state', 'expenditure_date_formatted', 'expenditure_amount', 'filer_committee_id_number', 'district_checked')
        
def yearFilter(queryset, querydict):
    try:
        year=int(querydict['year_covered'])
        queryset = queryset.filter(Q(coverage_from_date__gte=date(year,1,1), coverage_to_date__lte=date(year,12,31)))
    
    except (KeyError, ValueError):
        pass
    return queryset
    

def DWDistrictFilter(queryset, querydict):
    try:
        district_list=querydict['districts']
        
        # if there's no commas, it's just a single district
        if district_list.find(',') < 0:
            queryset = queryset.filter(district__pk=district_list)
            
        # if there's a comma, it's a comma-delimited list
        else:
            district_ids = district_list.split(',')
            queryset = queryset.filter(district__pk__in=district_ids)
    
    except KeyError:
        pass
        
    return queryset

def candidatedistrictFilter(queryset, querydict):
    try:
        id=int(querydict['district'])
        queryset = queryset.filter(district__pk=id)

    except (KeyError, ValueError):
        pass
        
    return queryset

def districtIDFilter(queryset, querydict):
    try:
        id=int(querydict['pk'])
        queryset = queryset.filter(pk=id)

    except (KeyError, ValueError):
        pass
    return queryset

def weekFilter(queryset, querydict):
    try:
        week=querydict['week']

        # if there's no commas, it's just a single district
        if week.upper() == "NOW":
            queryset = queryset.filter(cycle_week_number = get_week_number(date.today()) )
        if week.upper() == "LAST":
            queryset = queryset.filter(cycle_week_number = get_week_number(date.today())-1 )


    except KeyError:
        pass

    return queryset

def periodTypeFilter(queryset, querydict):
    try:
        period_type=querydict['period_type']
        if period_type.startswith('Q'):
            if period_type == 'Q1':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,1,1), coverage_to_date=date(2013,3,31))|Q(coverage_from_date=date(2014,1,1), coverage_to_date=date(2014,3,31)))
        
            elif period_type == 'Q2':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,4,1), coverage_to_date=date(2013,6,30))|Q(coverage_from_date=date(2014,4,1), coverage_to_date=date(2014,6,30)))

            elif period_type == 'Q3':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,7,1), coverage_to_date=date(2013,9,30))|Q(coverage_from_date=date(2014,7,1), coverage_to_date=date(2014,9,30)))

            elif period_type == 'Q4':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,10,1), coverage_to_date=date(2013,12,31))|Q(coverage_from_date=date(2014,10,1), coverage_to_date=date(2014,9,30)))


        elif period_type.startswith('M'):
                        
            if period_type == 'M1':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,1,1), coverage_to_date=date(2013,1,31))|Q(coverage_from_date=date(2014,1,1), coverage_to_date=date(2014,1,31))|Q(coverage_from_date=date(2015,1,1), coverage_to_date=date(2015,1,31)))

            elif period_type == 'M2':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,2,1), coverage_to_date=date(2013,2,28))|Q(coverage_from_date=date(2014,2,1), coverage_to_date=date(2014,2,28))|Q(coverage_from_date=date(2015,2,1), coverage_to_date=date(2015,2,28)))

            elif period_type == 'M3':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,3,1), coverage_to_date=date(2013,3,31))|Q(coverage_from_date=date(2014,3,1), coverage_to_date=date(2014,3,31)))

            elif period_type == 'M4':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,4,1), coverage_to_date=date(2013,4,30))|Q(coverage_from_date=date(2014,4,1), coverage_to_date=date(2014,4,30)))

            elif period_type == 'M5':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,5,1), coverage_to_date=date(2013,5,31))|Q(coverage_from_date=date(2014,5,1), coverage_to_date=date(2014,5,31)))

            elif period_type == 'M6':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,6,1), coverage_to_date=date(2013,6,30))|Q(coverage_from_date=date(2014,6,1), coverage_to_date=date(2014,6,30)))

            elif period_type == 'M7':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,7,1), coverage_to_date=date(2013,7,31))|Q(coverage_from_date=date(2014,7,1), coverage_to_date=date(2014,7,31)))

            elif period_type == 'M8':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,8,1), coverage_to_date=date(2013,8,31))|Q(coverage_from_date=date(2014,8,1), coverage_to_date=date(2014,8,31)))

            elif period_type == 'M9':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,9,1), coverage_to_date=date(2013,9,30))|Q(coverage_from_date=date(2014,9,1), coverage_to_date=date(2014,9,30)))

            elif period_type == 'M10':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,10,1), coverage_to_date=date(2013,10,31))|Q(coverage_from_date=date(2014,10,1), coverage_to_date=date(2014,10,31)))

            elif period_type == 'M11':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,11,1), coverage_to_date=date(2013,11,30))|Q(coverage_from_date=date(2014,11,1), coverage_to_date=date(2014,11,30)))

            elif period_type == 'M12':
                queryset = queryset.filter(Q(coverage_from_date=date(2013,12,1), coverage_to_date=date(2013,12,31))|Q(coverage_from_date=date(2014,12,1), coverage_to_date=date(2014,12,31)))

{'fraction_non_alphanumeric': 0.13213627992633517, 'fraction_numerical': 0.04839369756496828, 'mean_word_length': 4.48820887142055, 'pattern_counts': {'":': 4, '<': 5, '<?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': '3185646', 'n_tokens_mistral': 7295, 'n_tokens_neox': 6541, 'n_words': 943}