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

merge in changes from pypi version 0.5.0 #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 15 additions & 20 deletions uuidfield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,37 @@ def __init__(self, *args, **kwargs):

super(StringUUID, self).__init__(*args, **kwargs)

def __unicode__(self):
return unicode(str(self))

def __str__(self):
if self.hyphenate:
return super(StringUUID, self).__str__()

return self.hex

def __len__(self):
return len(self.__str__())
return len(self.__unicode__())


class UUIDField(Field):
"""
A field which stores a UUID value in hex format. This may also have the
Boolean attribute 'auto' which will set the value on initial save to a new
UUID value. Note that while all UUIDs are expected to be unique we enforce
this with a DB constraint.
A field which stores a UUID value in hex format. This may also have
the Boolean attribute 'auto' which will set the value on initial save to a
new UUID value (calculated using the UUID1 method). Note that while all
UUIDs are expected to be unique we enforce this with a DB constraint.
"""
# TODO: support binary storage types
__metaclass__ = SubfieldBase

def __init__(self, version=4, node=None, clock_seq=None,
namespace=None, name=None, auto=False, hyphenate=False,
*args, **kwargs):
assert version in (1, 3, 4, 5), "UUID version {ver}is not supported."\
.format(ver=version)
namespace=None, name=None, auto=False, hyphenate=False, *args, **kwargs):
assert version in (1, 3, 4, 5), "UUID version %s is not supported." % version
self.auto = auto
self.version = version
self.hyphenate = hyphenate

if hyphenate:
# We store UUIDs in string format, which is fixed at 36 characters.
kwargs['max_length'] = 36
else:
# We store UUIDs in hex format, which is fixed at 32 characters.
kwargs['max_length'] = 32

# We store UUIDs in hex format, which is fixed at 32 characters.
kwargs['max_length'] = 32
if auto:
# Do not let the user edit UUIDs if they are auto-assigned.
kwargs['editable'] = False
Expand Down Expand Up @@ -152,7 +147,7 @@ def value_to_string(self, obj):
if val is None:
data = ''
else:
data = str(val)
data = unicode(val)
return data

def to_python(self, value):
Expand All @@ -165,8 +160,8 @@ def to_python(self, value):
if not value:
return None
# attempt to parse a UUID including cases in which value is a UUID
# instance already to be able to get our StringUUID in.
return StringUUID(smart_unicode(value), hyphenate=self.hyphenate)
# and return the __unicode__ representation of the StringUUID instance
return StringUUID(smart_unicode(value), hyphenate=self.hyphenate).__unicode__()

def formfield(self, **kwargs):
defaults = {
Expand Down