Permalink
Browse files

Initial commit

  • Loading branch information...
dcramer committed Sep 1, 2010
0 parents commit 460b38a240b235395a67367384fe164523b7de6c
Showing with 92 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +12 −0 LICENSE
  3. +2 −0 MANIFEST.in
  4. +21 −0 setup.py
  5. +54 −0 uuidfield/__init__.py
@@ -0,0 +1,3 @@
+*.pyc
+/build
+*.egg-info/
12 LICENSE
@@ -0,0 +1,12 @@
+Copyright (c) 2009 David Cramer and individual contributors.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+ 3. Neither the name of the django-uuidfield nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,2 @@
+include setup.py README.rst LICENSE MANIFEST.in
+global-exclude *~
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+from setuptools import setup, find_packages
+
+setup(
+ name='django-uuidfield',
+ version=".".join(map(str, __import__('uuidfield').__version__)),
+ author='David Cramer',
+ author_email='dcramer@gmail.com',
+ description='UUIDField in Django',
+ url='http://github.com/dcramer/django-uuidfield',
+ packages=find_packages(),
+ include_package_data=True,
+ classifiers=[
+ "Framework :: Django",
+ "Intended Audience :: Developers",
+ "Intended Audience :: System Administrators",
+ "Operating System :: OS Independent",
+ "Topic :: Software Development"
+ ],
+)
@@ -0,0 +1,54 @@
+__version__ = (0, 1)
+
+from django.db.models import Field
+
+import uuid
+
+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 (calculated using the UUID1 method). Note that while all
+ UUIDs are expected to be unique we enforce this with a DB constraint.
+ """
+ # __metaclass__ = models.SubfieldBase
+
+ def __init__(self, version=4, node=None, clock_seq=None, namespace=None, name=None, auto=False, *args, **kwargs):
+ assert(version in (1, 3, 4, 5), "UUID version %s is not supported." % (version,))
+ self.auto = auto
+ self.version = version
+ # 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
+ kwargs['blank'] = True
+ kwargs['unique'] = True
+ if version == 1:
+ self.node, self.clock_seq = node, clock_seq
+ elif version in (3, 5):
+ self.namespace, self.name = namespace, name
+ super(UUIDField, self).__init__(*args, **kwargs)
+
+ def _create_uuid(self):
+ if self.version == 1:
+ args = (self.node, self.clock_seq)
+ elif self.version in (3, 5):
+ args = (self.namespace, self.name)
+ else:
+ args = ()
+ return getattr(uuid, 'uuid%s' % (self.version,))(*args)
+
+ def db_type(self):
+ return 'char(%s)' % (self.max_length,)
+
+ def pre_save(self, model_instance, add):
+ """ see CharField.pre_save
+ This is used to ensure that we auto-set values if required.
+ """
+ value = getattr(model_instance, self.attname, None)
+ if self.auto and add and not value:
+ # Assign a new value for this attribute if required.
+ value = self._create_uuid().hex
+ setattr(model_instance, self.attname, value)
+ return value

0 comments on commit 460b38a

Please sign in to comment.