Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One proposal to resolve #10 #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion timeflake/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.4.0"
__version__ = "1.0.0"
__all__ = ["Timeflake", "random", "from_values"]

import os
Expand Down
79 changes: 12 additions & 67 deletions timeflake/extensions/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,21 @@
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)
else:
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
)
return value


class TimeflakePrimaryKeyBinary(TimeflakeBinary):
Expand Down