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

Python3 support. #52

Open
wants to merge 1 commit 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
9 changes: 8 additions & 1 deletion runtests_sqlite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
import sys
from django.conf import settings
from optparse import OptionParser

Expand All @@ -25,6 +24,14 @@
from django_nose import NoseTestSuiteRunner


try:
# django 1.7 standalone app setup
import django
django.setup()
except AttributeError:
pass


from runtests import runtests

if __name__ == '__main__':
Expand Down
19 changes: 15 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
[testenv]
[tox]
envlist =
py2,
py3

[base]
downloadcache = {toxworkdir}/_download/
deps =
Django==1.3.1
psycopg2==2.4.1
Django>=1.3.1
psycopg2>=2.4.1
django-nose
commands =
{envbindir}/python setup.py test
{envpython} setup.py test

[testenv:py2]
basepython=python2

[testenv:py3]
basepython=python3
10 changes: 8 additions & 2 deletions uuidfield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from django.utils.encoding import smart_unicode
except ImportError:
from django.utils.encoding import smart_text as smart_unicode
try:
from django.utils import six
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you recall which version of Django introduced this?

I'm ok with dropping support for 1.3 if i.e. it came in 1.4.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to

https://docs.djangoproject.com/en/dev/topics/python3/

Writing compatible code is much easier if you target Python ≥ 2.6. Django 1.5 introduces compatibility tools such as django.utils.six, which is a customized version of the six module. For convenience, forwards-compatible aliases were introduced in Django 1.4.2. If your application takes advantage of these tools, it will require Django ≥ 1.4.2.

except ImportError:
import six
from django.utils.encoding import python_2_unicode_compatible

try:
# psycopg2 needs us to register the uuid type
Expand All @@ -15,6 +20,7 @@
pass


@python_2_unicode_compatible
class StringUUID(uuid.UUID):
def __init__(self, *args, **kwargs):
# get around UUID's immutable setter
Expand All @@ -32,15 +38,15 @@ def __len__(self):
return len(self.__str__())


class UUIDField(Field):
class UUIDField(six.with_metaclass(SubfieldBase, 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.
"""
# TODO: support binary storage types
__metaclass__ = SubfieldBase
# __metaclass__ = SubfieldBase

def __init__(self, version=4, node=None, clock_seq=None,
namespace=None, name=None, auto=False, hyphenate=False,
Expand Down
4 changes: 2 additions & 2 deletions uuidfield/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from tests import *
from models import *
from .tests import *
from .models import *
20 changes: 12 additions & 8 deletions uuidfield/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import uuid

from django.db import connection, IntegrityError
from django.db import IntegrityError
from django.test import TestCase
try:
from django.utils.encoding import smart_unicode
except ImportError:
from django.utils.encoding import smart_text as smart_unicode

from uuidfield.tests.models import (AutoUUIDField, ManualUUIDField,
NamespaceUUIDField, BrokenNamespaceUUIDField, HyphenatedUUIDField)
from uuidfield.tests.models import (
AutoUUIDField, ManualUUIDField,
NamespaceUUIDField, BrokenNamespaceUUIDField, HyphenatedUUIDField
)


class UUIDFieldTestCase(TestCase):
Expand Down Expand Up @@ -40,8 +46,7 @@ def test_hyphenated(self):
obj = HyphenatedUUIDField.objects.create(name='test')
uuid = obj.uuid

self.assertTrue('-' in unicode(uuid))
self.assertTrue('-' in str(uuid))
self.assertTrue('-' in smart_unicode(uuid))

self.assertEquals(len(uuid), 36)

Expand All @@ -56,9 +61,8 @@ def test_hyphenated(self):

def test_can_use_hyphenated_uuids_in_filter_and_get(self):
obj = AutoUUIDField.objects.create()
obj_uuid = uuid.UUID(str(obj.uuid))
self.assertTrue('-' in unicode(obj_uuid))
self.assertTrue('-' in str(obj_uuid))
obj_uuid = uuid.UUID(smart_unicode(obj.uuid))
self.assertTrue('-' in smart_unicode(obj_uuid))
inserted_obj = AutoUUIDField.objects.get(uuid=obj_uuid)
filtered_obj = AutoUUIDField.objects.filter(uuid=obj_uuid)[0]
self.assertEqual(inserted_obj.uuid, obj_uuid)
Expand Down