def __call__(self, data): field = self.parent.fields[self.field_name] value = field.get_attribute(data) if value is None: return data if callable(self.patient): patient = self.patient() elif isinstance(self.patient, Patient): patient = self.patient else: patient_field = self.parent.fields[self.patient] patient = patient_field.get_attribute(data) if patient is None: return data if is_datetime(value): value_date = datetime_to_date(value) else: value_date = value earliest_date_of_birth = patient.earliest_date_of_birth if earliest_date_of_birth is not None and value_date < earliest_date_of_birth: message = "Value is before the patient's date of birth ({}).".format( safe_strftime(earliest_date_of_birth, HUMAN_DATE_FORMAT) ) raise ValidationError({self.field_name: message}) return data
def before_f(value): if is_date(value): value_dt = date_to_datetime(value) else: value_dt = value if value_dt > max_dt: raise ValidationError('Value is after %s.' % safe_strftime(max_dt, dt_format)) return value
def before_f(value): if is_date(value): value_dt = date_to_datetime(value) else: value_dt = value if value_dt > max_dt: raise ValidationError('Value is after %s.' % safe_strftime(max_dt, dt_format)) return value
def test_f_lower(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, 123, tzinfo=pytz.utc), '%f') == '000123'
def test_b_lower(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Jan' assert safe_strftime(datetime(1234, 2, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Feb' assert safe_strftime(datetime(1234, 3, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Mar' assert safe_strftime(datetime(1234, 4, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Apr' assert safe_strftime(datetime(1234, 5, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'May' assert safe_strftime(datetime(1234, 6, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Jun' assert safe_strftime(datetime(1234, 7, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Jul' assert safe_strftime(datetime(1234, 8, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Aug' assert safe_strftime(datetime(1234, 9, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Sep' assert safe_strftime(datetime(1234, 10, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Oct' assert safe_strftime(datetime(1234, 11, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Nov' assert safe_strftime(datetime(1234, 12, 2, 3, 4, 5, tzinfo=pytz.utc), '%b') == 'Dec'
def test_p_lower(): assert safe_strftime(datetime(1234, 1, 2, 0, 0, 0, tzinfo=pytz.utc), '%p') == 'AM' assert safe_strftime(datetime(1234, 1, 2, 11, 0, 0, tzinfo=pytz.utc), '%p') == 'AM' assert safe_strftime(datetime(1234, 1, 2, 12, 0, 0, tzinfo=pytz.utc), '%p') == 'PM' assert safe_strftime(datetime(1234, 1, 2, 13, 0, 0, tzinfo=pytz.utc), '%p') == 'PM' assert safe_strftime(datetime(1234, 1, 2, 23, 0, 0, tzinfo=pytz.utc), '%p') == 'PM'
def test_s_upper(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%S') == '05'
def test_i_upper_pm(): assert safe_strftime(datetime(1234, 1, 2, 15, 4, 5, tzinfo=pytz.utc), '%I') == '03'
def test_iso_format(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%Y-%m-%dT%H:%M:%SZ') == '1234-01-02T03:04:05Z'
def test_d_lower(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%d') == '02'
def test_h_upper_am(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%H') == '03'
def test_b_upper(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'January' assert safe_strftime(datetime(1234, 2, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'February' assert safe_strftime(datetime(1234, 3, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'March' assert safe_strftime(datetime(1234, 4, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'April' assert safe_strftime(datetime(1234, 5, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'May' assert safe_strftime(datetime(1234, 6, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'June' assert safe_strftime(datetime(1234, 7, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'July' assert safe_strftime(datetime(1234, 8, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'August' assert safe_strftime(datetime(1234, 9, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'September' assert safe_strftime(datetime(1234, 10, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'October' assert safe_strftime(datetime(1234, 11, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'November' assert safe_strftime(datetime(1234, 12, 2, 3, 4, 5, tzinfo=pytz.utc), '%B') == 'December'
def safe_strftime_template_filter(value, format): if value is None: return '' else: return safe_strftime(value, format)
def test_ddmmyyyy(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%d/%m/%Y') == '02/01/1234'
def test_yyyymmdd(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%Y-%m-%d') == '1234-01-02' assert safe_strftime(date(1234, 1, 2), '%Y-%m-%d') == '1234-01-02'
def test_blank(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '') == ''
def test_invalid_directive(): with pytest.raises(ValueError) as e: safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '%e') assert e.value.args[0] == 'Invalid format string'
def test_percent(): assert safe_strftime(datetime(1234, 1, 2, 3, 4, 5, tzinfo=pytz.utc), '100%%') == '100%'