def validate_params(self, params): start_date_str = params['start_date'] end_date_str = params['end_date'] enrollment_statuses = params['enrollment_statuses'] awardees = params['awardees'] stratification = params['stratification'] # Validate dates if not start_date_str or not end_date_str: raise BadRequest('Start date and end date should not be empty') try: start_date = datetime.datetime.strptime(start_date_str, DATE_FORMAT).date() except ValueError: raise BadRequest('Invalid start date: %s' % start_date_str) try: end_date = datetime.datetime.strptime(end_date_str, DATE_FORMAT).date() except ValueError: raise BadRequest('Invalid end date: %s' % end_date_str) date_diff = abs((end_date - start_date).days) if date_diff > DAYS_LIMIT: raise BadRequest('Difference between start date and end date ' \ 'should not be greater than %s days' % DAYS_LIMIT) params['start_date'] = start_date params['end_date'] = end_date # Validate awardees, get ID list awardee_ids = [] for awardee in awardees: awardee_id = get_awardee_id_from_name({'awardee': awardee}, self.hpo_dao) if awardee_id == None: raise BadRequest('Invalid awardee name: %s' % awardee) awardee_ids.append(awardee_ids) params['awardee_ids'] = awardee_ids # Validate enrollment statuses try: params["enrollment_statuses"] = [ EnrollmentStatus(val) for val in enrollment_statuses ] except TypeError: valid_enrollment_statuses = EnrollmentStatus.to_dict() for enrollment_status in enrollment_statuses: if enrollment_status not in valid_enrollment_statuses: raise BadRequest('Invalid enrollment status: %s' % enrollment_status) # Validate stratifications try: params['stratification'] = Stratifications( params['stratification']) except TypeError: raise BadRequest('Invalid stratification: %s' % stratification) return params
def validate_params(self, start_date_str, end_date_str, stratification_str, enrollment_statuses, awardees, history): """Validates URL parameters, and converts human-friendly values to canonical form :param start_date_str: Start date string, e.g. '2018-01-01' :param end_date_str: End date string, e.g. '2018-01-31' :param stratification_str: How to stratify (layer) results, as in a stacked bar chart :param enrollment_statuses: enrollment level filters :param awardees: awardee name filters :param history: indicate if it's for historical data :return: Validated parameters in canonical form """ params = {} # Validate stratifications try: params['stratification'] = Stratifications(stratification_str) except TypeError: raise BadRequest('Invalid stratification: %s' % stratification_str) if params['stratification'] in [ Stratifications.FULL_STATE, Stratifications.FULL_CENSUS, Stratifications.FULL_AWARDEE, Stratifications.LIFECYCLE ]: # Validate dates if not end_date_str: raise BadRequest('end date should not be empty') start_date = datetime.datetime.strptime('2017-01-01', DATE_FORMAT).date() try: end_date = datetime.datetime.strptime(end_date_str, DATE_FORMAT).date() except ValueError: raise BadRequest('Invalid end date: %s' % end_date_str) params['start_date'] = start_date params['end_date'] = end_date else: # Validate dates if not start_date_str or not end_date_str: raise BadRequest('Start date and end date should not be empty') try: start_date = datetime.datetime.strptime( start_date_str, DATE_FORMAT).date() except ValueError: raise BadRequest('Invalid start date: %s' % start_date_str) try: end_date = datetime.datetime.strptime(end_date_str, DATE_FORMAT).date() except ValueError: raise BadRequest('Invalid end date: %s' % end_date_str) date_diff = abs((end_date - start_date).days) if history != 'TRUE' and date_diff > DAYS_LIMIT_FOR_REALTIME_DATA: raise BadRequest('Difference between start date and end date ' 'should not be greater than %s days' % DAYS_LIMIT_FOR_REALTIME_DATA) if history == 'TRUE' and date_diff > DAYS_LIMIT_FOR_HISTORY_DATA: raise BadRequest('Difference between start date and end date ' 'should not be greater than %s days' % DAYS_LIMIT_FOR_HISTORY_DATA) params['start_date'] = start_date params['end_date'] = end_date # Validate awardees, get ID list awardee_ids = [] if awardees is not None: awardees = awardees.split(',') for awardee in awardees: if awardee != '': awardee_id = get_awardee_id_from_name({'awardee': awardee}, self.hpo_dao) if awardee_id == None: raise BadRequest('Invalid awardee name: %s' % awardee) awardee_ids.append(awardee_id) params['awardee_ids'] = awardee_ids # Validate enrollment statuses if enrollment_statuses is not None: enrollment_statuses = enrollment_statuses.split(',') try: params['enrollment_statuses'] = [ EnrollmentStatus(val) for val in enrollment_statuses ] except TypeError: valid_enrollment_statuses = EnrollmentStatus.to_dict() for enrollment_status in enrollment_statuses: if enrollment_status != '': if enrollment_status not in valid_enrollment_statuses: raise BadRequest('Invalid enrollment status: %s' % enrollment_status) return params
def validate_params(self, params): filters = {} # Validate stratifications try: filters['stratification'] = Stratifications(params['stratification']) except TypeError: raise BadRequest('Invalid stratification: %s' % params['stratification']) if filters['stratification'] in [Stratifications.FULL_STATE, Stratifications.FULL_CENSUS, Stratifications.FULL_AWARDEE, Stratifications.GEO_STATE, Stratifications.GEO_CENSUS, Stratifications.GEO_AWARDEE, Stratifications.LIFECYCLE]: # Validate dates if not params['end_date']: raise BadRequest('end date should not be empty') try: end_date = datetime.datetime.strptime(params['end_date'], DATE_FORMAT).date() start_date = end_date except ValueError: raise BadRequest('Invalid end date: %s' % params['end_date']) filters['start_date'] = start_date filters['end_date'] = end_date else: # Validate dates if not params['start_date'] or not params['end_date']: raise BadRequest('Start date and end date should not be empty') try: start_date = datetime.datetime.strptime(params['start_date'], DATE_FORMAT).date() except ValueError: raise BadRequest('Invalid start date: %s' % params['start_date']) try: end_date = datetime.datetime.strptime(params['end_date'], DATE_FORMAT).date() except ValueError: raise BadRequest('Invalid end date: %s' % params['end_date']) date_diff = abs((end_date - start_date).days) if params['history'] != 'TRUE' and date_diff > DAYS_LIMIT_FOR_REALTIME_DATA: raise BadRequest('Difference between start date and end date ' 'should not be greater than %s days' % DAYS_LIMIT_FOR_REALTIME_DATA) if params['history'] == 'TRUE' and date_diff > DAYS_LIMIT_FOR_HISTORY_DATA: raise BadRequest('Difference between start date and end date ' 'should not be greater than %s days' % DAYS_LIMIT_FOR_HISTORY_DATA) filters['start_date'] = start_date filters['end_date'] = end_date # Validate awardees, get ID list awardee_ids = [] if params['awardees'] is not None: awardees = params['awardees'].split(',') for awardee in awardees: if awardee != '': awardee_id = get_awardee_id_from_name({'awardee': awardee}, self.hpo_dao) if awardee_id is None: raise BadRequest('Invalid awardee name: %s' % awardee) awardee_ids.append(awardee_id) filters['awardee_ids'] = awardee_ids # Validate enrollment statuses enrollment_status_strs = [] if params['enrollment_statuses'] is not None: enrollment_statuses = params['enrollment_statuses'].split(',') try: enrollment_status_strs = [str(EnrollmentStatus(val)) for val in enrollment_statuses] except TypeError: valid_enrollment_statuses = EnrollmentStatus.to_dict() for enrollment_status in enrollment_statuses: if enrollment_status != '': if enrollment_status not in valid_enrollment_statuses: raise BadRequest('Invalid enrollment status: %s' % enrollment_status) filters['enrollment_statuses'] = enrollment_status_strs if params['sample_time_def'] and params['sample_time_def'] not in ['STORED', 'ORDERED']: raise BadRequest('Invalid value for parameter filterBy: %s' % params['sample_time_def']) else: filters['sample_time_def'] = params['sample_time_def'] if params['history'] and params['history'] not in ['TRUE', 'FALSE']: raise BadRequest('Invalid value for parameter history: %s' % params['history']) else: filters['history'] = params['history'] return filters