def test_to_python_int_values(self): self.assertEqual( ULIDField().to_python(0), ulid.parse('00000000-0000-0000-0000-000000000000') ) # Works for integers less than 128 bits. self.assertEqual( ULIDField().to_python((2 ** 128) - 1), ulid.parse('ffffffff-ffff-ffff-ffff-ffffffffffff') )
def process_result_value(self, value, dialect): """文字列からULIDに変換""" if value is not None: if not isinstance(value, str): raise TypeError("value should have str type") return ulid.parse(value) return value
def run(unfurl, node): if not node.data_type.startswith('ulid'): # ULIDs are Base32 encoded and allow dashes to be added as desired for clarity # Ref: # - https://github.com/ulid/spec # - https://www.crockford.com/base32.html # # Filtering down to reasonable timeframes to reduce FPs: # - 019AHCNC00SM9CSFQFXG3VC1FK <- 2015-01-01 # - 01JGFJJZ00TG242KAWHD959K7S <- 2025-01-01 m = re.match(r'(?P<ulid>01[90A-HJ][A-HJKMNP-Z0-9]{23})', str(node.value).replace('-', '')) if m: u = m.group('ulid') unfurl.add_to_queue(data_type='ulid', key=None, value=u, label='ULID: {}'.format(u), hover='ULIDs are identifiers that are comprised of a timestamp and a random number. ' '<a href="https://github.com/ulid/spec" target="_blank">[ref]</a>', parent_id=node.node_id, incoming_edge_config=uuid_edge, extra_options={'widthConstraint': {'maximum': 300}}) elif node.data_type == 'ulid': u = ulid.parse(node.value) timestamp = u.timestamp().int unfurl.add_to_queue(data_type='ulid-parsed', key=None, value=timestamp, label='Timestamp: {}'.format(timestamp), parent_id=node.node_id, incoming_edge_config=uuid_edge)
def to_python(self, value): if value is None: return None try: return ulid.parse(value) except (AttributeError, ValueError): raise exceptions.ValidationError( _("'%(value)s' is not a valid ULID."), code='invalid', params={'value': value})
def to_python(self, value): value = super().to_python(value) if value in self.empty_values: return None try: return ulid.parse(value) except (AttributeError, ValueError): raise exceptions.ValidationError(_('Enter a valid ULID.'), code='invalid')
def to_python(self, value): if value is not None and not isinstance(value, ulid.ulid.ULID): try: return ulid.parse(value) except (AttributeError, ValueError): raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, ) return value
def to_python(self, value): value = super().to_python(value) if value in self.empty_values: return None if not isinstance(value, ulid.ulid.ULID): try: value = ulid.parse(value) except ValueError: raise ValidationError(self.error_messages['invalid'], code='invalid') return value
def from_db_value(self, value, expression, connection): if value is None: return value return ulid.parse(value)
def prepare_value(self, value): return str(ulid.parse(value))
def to_representation(self, value): return str(ulid.parse(value))
def to_internal_value(self, data): try: return ulid.parse(data) except (AttributeError, ValueError): self.fail('invalid', value=data)
def test_loading(self): instance = list(serializers.deserialize('json', self.test_data))[0].object self.assertEqual(instance.field, ulid.parse('2N1T201RMV87AAE5J4CSAM8000'))
def test_dumping(self): instance = ULIDModel(field=ulid.parse('2N1T201RMV87AAE5J4CSAM8000')) data = serializers.serialize('json', [instance]) self.assertEqual(json.loads(data), json.loads(self.test_data))
def test_ulidfield_4(self): field = ULIDField() value = field.prepare_value(ulid.parse('2N1T201RMV87AAE5J4CSAM8000')) self.assertEqual(value, '2N1T201RMV87AAE5J4CSAM8000')
def test_clean_value_with_dashes(self): field = ULIDField() value = field.clean('550e8400-e29b-41d4-a716-446655440000') self.assertEqual(value, ulid.parse('550e8400e29b41d4a716446655440000'))
def test_ulidfield_1(self): field = ULIDField() value = field.clean('550e8400e29b41d4a716446655440000') self.assertEqual(value, ulid.parse('550e8400e29b41d4a716446655440000'))