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

FieldUUID #71

Merged
merged 1 commit into from
May 14, 2016
Merged
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
65 changes: 64 additions & 1 deletion djangocassandra/db/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.db.models import (
Field,
AutoField,
SubfieldBase
SubfieldBase,
CharField
)
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -75,6 +76,68 @@ def value_to_string(
])


class FieldUUID(with_metaclass(SubfieldBase, CharField)):
description = _('UUID')

default_error_messages = {
'invalid': _("'%(value)s' value must be a valid UUID."),
}

def __init__(
self,
*args,
**kwargs
):
if 'default' not in kwargs:
kwargs['default'] = uuid.uuid4

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

def to_python(
self,
value
):
if isinstance(value, uuid.UUID):
return str(value)

else:
return value

def get_prep_value(self, value):
value = super(FieldUUID, self).get_prep_value(value)
if (
value is None or
isinstance(value, uuid.UUID)
):
return value

return uuid.UUID(value)

def get_internal_type(self):
return 'FieldUUID'

@staticmethod
def get_auto_value(self):
return uuid.uuid4()

def value_to_string(self, value):
if isinstance(value, basestring):
return value

try:
return str(value)

except (TypeError, ValueError):
raise Exception(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
)


class AutoFieldUUID(with_metaclass(SubfieldBase, AutoField)):
description = _('UUID')

Expand Down
1 change: 1 addition & 0 deletions djangocassandra/db/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
internal_type_to_column_map = {
'AutoField': columns.Integer,
'AutoFieldUUID': columns.UUID,
'FieldUUID': columns.UUID,
'RelatedAutoField': columns.UUID,
'ForeignKey': columns.UUID,
'OneToOneField': columns.UUID,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='djangocassandra',
version='0.4.2',
version='0.4.3',
description='Cassandra support for the Django web framework',
long_description=(
'The Cassandra database backend for Django has been '
Expand Down
10 changes: 9 additions & 1 deletion tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
ForeignKey
)

from djangocassandra.db.fields import AutoFieldUUID
from djangocassandra.db.fields import (
AutoFieldUUID,
FieldUUID
)
from djangocassandra.db.models import (
ColumnFamilyModel,
ColumnFamilyManager
Expand Down Expand Up @@ -157,6 +160,11 @@ class RelatedModelC(Model):
)


class UUIDFieldModel(Model):
id = AutoFieldUUID(primary_key=True)
uuid = FieldUUID()


class ClusterPrimaryKeyModel(ColumnFamilyModel):
class Cassandra:
clustering_keys = ['field_2', 'field_3']
Expand Down
38 changes: 38 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import uuid
from unittest import TestCase

from .models import (
UUIDFieldModel
)

from .util import (
connect_db,
destroy_db,
create_model
)


class UUIDFieldModelTestCase(TestCase):
def setUp(self):
self.connection = connect_db()
self.cached_rows = {}

create_model(
self.connection,
UUIDFieldModel
)

import django
django.setup()

def tearDown(self):
destroy_db(self.connection)

def test_create_uuid_field_model(self):
instance = UUIDFieldModel.objects.create()

self.assertIsNotNone(instance)
uuid.UUID(instance.uuid)
uuid.UUID(instance.id)

UUIDFieldModel.objects.get(pk=instance.pk)