def test_unidecode_limits_length(self): long_str = 'this string has more than fifty characters in it......' decoded_str = safe_unidecode(long_str, 50) assert_greater(len(long_str), 50) assert_equal(len(decoded_str), 50) assert_equal(decoded_str, long_str[0:50])
def test_unidecode_shortens_mystery_characters(self): uni_str = u"these \u2214\u220e\u2219 don't translate" decoded_str = safe_unidecode(uni_str, 100) assert_true('?' in decoded_str) assert_true('[?]' not in decoded_str)
def test_unidecode_limits_length_with_unicode(self): uni_str = u'This is gr\xe6t' decoded_str = safe_unidecode(uni_str, len(uni_str)) assert_equal(len(decoded_str), len(uni_str)) assert_not_equal(decoded_str[-1], str(uni_str[-1]))
def build_request_group(self, rg_dict, scheduled_requests=None, ignore_ipp=False): if scheduled_requests is None: scheduled_requests = {} rg_id = int(rg_dict['id']) operator = rg_dict['operator'].lower() ipp_value = rg_dict.get('ipp_value', 1.0) submitter = rg_dict.get('submitter', '') if ignore_ipp: # if we want to ignore ipp in the scheduler, then set it to 1.0 here and it will not modify the priority ipp_value = 1.0 requests, invalid_requests = self.build_requests(rg_dict, scheduled_requests, is_staff=rg_dict.get( 'is_staff', False)) if invalid_requests: msg = "Found %s." % pl(len(invalid_requests), 'invalid Request') log.warn(msg) for _, error_msg in invalid_requests: tag = "InvalidRequest" RequestGroup.emit_request_group_feedback(rg_id, error_msg, tag) if operator.lower() == 'and': msg = "Invalid request found within 'AND' RG %s making RG invalid" % rg_id tag = "InvalidRequestGroup" RequestGroup.emit_request_group_feedback(rg_id, msg, tag) raise RequestError(msg) if not requests: msg = "No valid Requests for RG %s" % rg_id tag = "InvalidRequestGroup" RequestGroup.emit_request_group_feedback(rg_id, msg, tag) raise RequestError(msg) proposal = self.get_proposal_details(rg_dict['proposal']) # Validate we are an allowed type of UR valid_observation_types = ['NORMAL', 'RAPID_RESPONSE', 'TIME_CRITICAL'] observation_type = rg_dict['observation_type'] if observation_type not in valid_observation_types: msg = "RequestGroup observation_type must be one of %s" % valid_observation_types raise RequestError(msg) # Calculate the maximum window time as the expire time max_window_time = datetime(1000, 1, 1) for req in requests: for windows in req.windows.windows_for_resource.values(): for window in windows: max_window_time = max(max_window_time, window.end) # Truncate the expire time by the current semester's end semester_details = self.get_semester_details(datetime.utcnow()) if semester_details: max_window_time = min(max_window_time, semester_details['end']) request_group = RequestGroup( operator=operator, requests=requests, proposal=proposal, rg_id=rg_id, is_staff=rg_dict.get('is_staff', False), observation_type=observation_type, ipp_value=ipp_value, name=rg_dict['name'], expires=max_window_time, submitter=safe_unidecode(submitter, 50), ) # Return only the invalid request and not the error message invalid_requests = [ir[0] for ir in invalid_requests] return request_group, invalid_requests