def reporting_levels_for_election(state, election_date, election_type, raw=False): """ Retrieve available reporting levels for an election. Args: state (string): State abbreviation. election_date (string): String representing election start date in format "YYYYMMDD". election_type: Election type. For example, general, primary, etc. raw: Consider available reporting levels for raw results. The default is to consider standardized/cleaned results. Returns: A list of available reporting levels. """ if raw: result_class = RawResult else: result_class = Result q = (Q(election_id__contains=format_date(election_date)) & Q(election_id__contains=election_type)) return result_class.objects.filter( state__iexact=state).filter(q).distinct('reporting_level')
def reporting_levels_for_election(state, election_date, election_type, raw=False): """ Retrieve available reporting levels for an election. Args: state (string): State abbreviation. election_date (string): String representing election start date in format "YYYYMMDD". election_type: Election type. For example, general, primary, etc. raw: Consider available reporting levels for raw results. The default is to consider standardized/cleaned results. Returns: A list of available reporting levels. """ if raw: result_class = RawResult else: result_class = Result q = (Q(election_id__contains=format_date(election_date)) & Q(election_id__contains=election_type)) return result_class.objects.filter(state__iexact=state).filter(q).distinct('reporting_level')
def build_date_filters(cls, datefilter): """ Create a query object of filters based on a date string. Arguments: datefilter (string): String representation of date. Returns: Q object of filters based on date string. """ filters = {} if not datefilter: return Q() # For now we filter on the date string in the election IDs # under the assumption that this will be faster than filtering # across a reference. filters['election_id__contains'] = format_date(datefilter) # Return a Q object rather than just a dict because the non-date # filters might also filter with a ``election_id__contains`` keyword # argument, clobbering the date filter, or vice-versa return Q(**filters)
def test_format_date(self): test_values = [ ("20101106", "2010-11-06"), ("201011", "2010-11"), ("2010", "2010"), ] for input_date, expected in test_values: self.assertEqual(format_date(input_date), expected) self.assertRaises(ValueError, format_date, "201011-06")
def get_elections(state, datefilter=None): """ Get all elections. Args: state: Required. Postal code for a state. For example, "md". datefilter: Date specified in "YYYY" or "YYYYMMDD" used to filter elections before they are baked. Returns: A list of dictionaries, each describing an election for the specified state. The elections are sorted by date. """ elections = elec_api.find(state.upper()) if datefilter: date_prefix = format_date(datefilter) elections = [elec for elec in elections if elec['start_date'].startswith(date_prefix)] return sorted(elections, key=lambda x: x['start_date'])
def get_elections(state, datefilter): """ Get all elections. Args: state: Required. Postal code for a state. For example, "md". datefilter: Date specified in "YYYY" or "YYYYMMDD" used to filter elections before they are baked. Returns: A list of dictionaries, each describing an election for the specified state. The elections are sorted by date. """ elections = elec_api.find(state.upper(), datefilter) if datefilter: date_prefix = format_date(datefilter) elections = [ elec for elec in elections if elec['start_date'].startswith(date_prefix) ] return sorted(elections, key=lambda x: x['start_date'])