def test_validate(self):
     for t in self.pas_explicit_range:
         print("PASS validate: {}".format(t))
         gd = SolrDaterange.validate(t)
     for t in self.fail_explicit_range:
         print("FAIL validate: {}".format(t))
         assert_raises(Invalid, SolrDaterange.validate, t)
 def test_validate(self):
     for t in self.pas_explicit_range:
         print("PASS validate: {}".format(t))
         gd = SolrDaterange.validate(t)
     for t in self.fail_explicit_range:
         print("FAIL validate: {}".format(t))
         assert_raises(Invalid, SolrDaterange.validate, t)
Пример #3
0
def vali_daterange(values):
    '''
    Initial conversion, 2 possibilities:
    1) <values> is a json representation of a list of DateRange-strings
       (output of validator repeating_text),
    2) <values> is one DateRange-string (output from ordinary text field)

    Both are initially converted to a list of DateRange strings
    
    Then add some slack to proper format of timerange before submitting
    to real validator:
      + insert trailing "Z" for points in time if necessary
      + add brackets if necessary
    '''
    def _fix_timestamp(ts):
        try:
            fixed = (ts + "Z" if len(ts.split(":")) == 3 and ts[-1] != "Z"
                     else ts)
        except:
            pass
        return fixed
    def _to_list_of_strings(valuestring):
        try:
            values = json.loads(valuestring)
        except ValueError:
            values = [valuestring]
        return(values)

    values = _to_list_of_strings(values)
    valid_values = []
    for value in values:
        value = value.strip()
        try:
            timestamps = [x.strip('[]') for x in value.split(" TO ")]
        except ValueError:
            pass
        else:
            timestamps = [_fix_timestamp(ts) for ts in timestamps]
            value = " TO ".join(timestamps)
            value = "[" + value + "]" if len(timestamps) == 2 else value 
        valid_values.append(SolrDaterange.validate(value))
    valid_values = json.dumps(valid_values)
    return valid_values