Commits

Matthew Schinckel committed 4d40ee2

Initial import

  • Participants

Comments (0)

Files changed (6)

File .bookmarks

Empty file added.
+recursive-include uuidfield *
+django-uuidfield
+====================
+
+There are a few UUID fields floating around out on the interwebs.
+
+Here is one packaged up as a pip installable one. It is mostly based on
+http://gist.github.com/374662, but has some changes, and will get some
+unit tests.
+
+Installation
+--------------
+
+Download or clone the repository, and from inside the root folder::
+
+  $ python setup.py install
+  
+Or, use pip::
+
+  $ pip install -e https://bitbucket.org/schinckel/django-uuidfield#egg=django-uuidfield
+
+Even better, do this from inside your virtualenv.
+
+You are using virtualenv, right?
+
+
+Django setup
+--------------
+
+You don't really need to install into your INSTALLED_APPS, but you may
+want to. I generally put everything in there that I use, just so any tests
+on it will be run when you test your project.
+
+Then, in your model file::
+
+    from django.db import models
+    import uuidfield
+  
+    class MyModel(models.Model):
+        uuid = uuidfield.UUIDField()
+
+Advanced use
+--------------
+
+The following arguments can be passed to the UUIDField:
+  
+  * ``auto`` - a boolean value as to if values should be generated
+    automatically.
+from distutils.core import setup
+
+setup(
+    name = "uuidfield",
+    version = "0.6",
+    description = "JSONField for django models",
+    url = "http://bitbucket.org/schinckel/django-uuidfield/",
+    author = "Matthew Schinckel",
+    author_email = "matt@schinckel.net",
+    packages = [
+        "uuidfield",
+    ],
+    classifiers = [
+        'Programming Language :: Python',
+        'License :: OSI Approved :: BSD License',
+        'Operating System :: OS Independent',
+        'Framework :: Django',
+    ],
+)

File uuidfield/__init__.py

+from fields import UUIDField

File uuidfield/fields.py

+from django.forms.util import ValidationError
+from django import forms
+from django.db import models
+from django.utils.encoding import smart_unicode
+from django.utils.translation import ugettext as _
+
+import uuid
+
+class UUIDField(models.CharField):
+    
+    def __init__(self, auto=False, *args, **kwargs):
+        if kwargs.get('primary_key', False):
+            assert auto, _("Must pass auto=True when using UUIDField as primary key")
+        
+        self.auto = auto
+        
+        kwargs['max_length'] = 32
+        if auto:
+            kwargs['editable'] = False
+            kwargs['blank'] = True
+            kwargs['null'] = True
+        
+        super(UUIDField, self).__init__(*args, **kwargs)
+        
+    
+    def db_type(self):
+        return 'uuid'
+    
+    def pre_save(self, model_instance, add):
+        value = getattr(model_instance, self.attname, None)
+        if not value and self.auto:
+            value = uuid.uuid4().hex
+            setattr(model_instance, self.attname, value)
+        return super(UUIDField, self).pre_save(model_instance, value, add)
+    
+    def to_python(self, value):
+        if not value:
+            return None
+        if len(value) != 32:
+            value = value.replace('-', '')
+        assert len(value) == 32
+        return value
+    
+    
+    def value_to_string(self, obj):
+        val = self._get_val_from_obj(obj)
+        if val is None:
+            data = ''
+        else:
+            data = unicode(val)
+        return data
+    
+try:
+    from south.modelsinspector import add_introspection_rules
+except ImportError:
+    pass
+else:
+    add_introspection_rules([
+        (
+            [UUIDField], # Class(es) these apply to
+            [],         # Positional arguments (not used)
+            {           # Keyword argument
+                "auto": ["auto", {"default": "False"}],
+            },
+        ),
+    ], ["^uuidfield\.fields\.UUIDField"]) # XXX Change this to where yours is stored. Better solution?