From a1a07793c1a585523505f4ad528b09a5d5e8ec41 Mon Sep 17 00:00:00 2001 From: Sebastian Steins Date: Tue, 20 Apr 2021 09:17:24 +0200 Subject: [PATCH 1/2] Proposal: Minimal Django integration as proposed in #10. Closes #10 --- timeflake/__init__.py | 2 +- timeflake/extensions/django/__init__.py | 73 ++----------------------- 2 files changed, 7 insertions(+), 68 deletions(-) diff --git a/timeflake/__init__.py b/timeflake/__init__.py index 88fa127..77afdf9 100644 --- a/timeflake/__init__.py +++ b/timeflake/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.4.0" +__version__ = "1.0.0" __all__ = ["Timeflake", "random", "from_values"] import os diff --git a/timeflake/extensions/django/__init__.py b/timeflake/extensions/django/__init__.py index 01b8622..83020c5 100644 --- a/timeflake/extensions/django/__init__.py +++ b/timeflake/extensions/django/__init__.py @@ -7,76 +7,15 @@ from django.utils.translation import ugettext_lazy as _ -def _parse(value) -> timeflake.Timeflake: - if value is None: - return value - elif isinstance(value, timeflake.Timeflake): - return value - elif isinstance(value, bytes): - return timeflake.parse(from_bytes=value) - elif isinstance(value, int): - return timeflake.parse(from_int=value) - elif isinstance(value, str): - size = len(value) - # hex - if size == 32: - return timeflake.parse(from_hex=value) - # base62 - elif size == 22: - return timeflake.parse(from_base62=value) - elif isinstance(value, uuid.UUID): - return timeflake.parse(from_bytes=value.bytes) - raise ValueError(f"Could not parse Timeflake from {value}") - - -class TimeflakeBinary(models.Field): +class TimeflakeBinary(models.UUIDField): description = "Timeflake UUID (128-bit)" - def __init__(self, *args, **kwargs): - super(TimeflakeBinary, self).__init__(*args, **kwargs) - - def deconstruct(self): - name, path, args, kwargs = super(TimeflakeBinary, self).deconstruct() - return name, path, args, kwargs - - def db_type(self, connection): - vendor = connection.vendor - if vendor == "mysql": - return "binary(16)" - elif vendor == "sqlite": - return "blob" - elif vendor == "postgresql": - return "uuid" - raise NotImplementedError( - f"Unsupported database vendor: {vendor} for field: TimeflakeBinary" - ) - - def rel_db_type(self, connection): - return self.db_type(connection) - - def from_db_value(self, value, expression, connection): - return self.to_python(value) - def to_python(self, value): - try: - return _parse(value) - except (AttributeError, ValueError): - raise exceptions.ValidationError( - self.error_messages["invalid"], code="invalid", params={"value": value}, - ) - - def get_db_prep_value(self, value, connection, prepared=False): - value = super().get_db_prep_value(value, connection, prepared) - if value is None: - return value - if not isinstance(value, timeflake.Timeflake): - value = self.to_python(value) - if connection.vendor == "postgresql": - return value.uuid - return value.bytes - - def formfield(self, **kwargs): - return super().formfield(**{"form_class": forms.UUIDField, **kwargs,}) + value = super().to_python(value) + if value is not None: + if isinstance(value, uuid.UUID): + return timeflake.Timeflake(from_bytes=value.bytes) + return value class TimeflakePrimaryKeyBinary(TimeflakeBinary): From eadb5d44301dcc501666a6912726e55a31a4d930 Mon Sep 17 00:00:00 2001 From: Sebastian Steins Date: Tue, 20 Apr 2021 09:20:23 +0200 Subject: [PATCH 2/2] Add exception --- timeflake/extensions/django/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/timeflake/extensions/django/__init__.py b/timeflake/extensions/django/__init__.py index 83020c5..d9bb3b2 100644 --- a/timeflake/extensions/django/__init__.py +++ b/timeflake/extensions/django/__init__.py @@ -15,6 +15,12 @@ def to_python(self, value): if value is not None: if isinstance(value, uuid.UUID): return timeflake.Timeflake(from_bytes=value.bytes) + else: + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) return value