2012-06-07 18:08:47 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-04-29 19:58:00 +02:00
|
|
|
import json
|
|
|
|
|
2010-02-24 20:06:59 +01:00
|
|
|
from django.db import models
|
2012-07-21 10:00:10 +02:00
|
|
|
from django.utils.encoding import force_text
|
2012-07-20 14:22:00 +02:00
|
|
|
from django.utils import six
|
2010-02-24 20:06:59 +01:00
|
|
|
|
2011-10-13 20:04:12 +02:00
|
|
|
|
2010-02-24 20:06:59 +01:00
|
|
|
class Small(object):
|
|
|
|
"""
|
|
|
|
A simple class to show that non-trivial Python objects can be used as
|
|
|
|
attributes.
|
|
|
|
"""
|
|
|
|
def __init__(self, first, second):
|
|
|
|
self.first, self.second = first, second
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2012-07-21 10:00:10 +02:00
|
|
|
return '%s%s' % (force_text(self.first), force_text(self.second))
|
2010-02-24 20:06:59 +01:00
|
|
|
|
|
|
|
class SmallField(models.Field):
|
|
|
|
"""
|
|
|
|
Turns the "Small" class into a Django field. Because of the similarities
|
|
|
|
with normal character fields and the fact that Small.__unicode__ does
|
|
|
|
something sensible, we don't need to implement a lot here.
|
|
|
|
"""
|
|
|
|
__metaclass__ = models.SubfieldBase
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs['max_length'] = 2
|
|
|
|
super(SmallField, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return 'CharField'
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
if isinstance(value, Small):
|
|
|
|
return value
|
|
|
|
return Small(value[0], value[1])
|
|
|
|
|
2011-03-30 19:48:42 +02:00
|
|
|
def get_db_prep_save(self, value, connection):
|
2012-07-20 14:48:51 +02:00
|
|
|
return six.text_type(value)
|
2010-02-24 20:06:59 +01:00
|
|
|
|
2010-03-23 15:11:41 +01:00
|
|
|
def get_prep_lookup(self, lookup_type, value):
|
2010-02-24 20:06:59 +01:00
|
|
|
if lookup_type == 'exact':
|
2012-07-21 10:00:10 +02:00
|
|
|
return force_text(value)
|
2010-02-24 20:06:59 +01:00
|
|
|
if lookup_type == 'in':
|
2012-07-21 10:00:10 +02:00
|
|
|
return [force_text(v) for v in value]
|
2010-02-24 20:06:59 +01:00
|
|
|
if lookup_type == 'isnull':
|
|
|
|
return []
|
2010-03-23 15:11:41 +01:00
|
|
|
raise TypeError('Invalid lookup type: %r' % lookup_type)
|
2010-02-24 20:06:59 +01:00
|
|
|
|
2010-11-02 19:40:53 +01:00
|
|
|
class SmallerField(SmallField):
|
|
|
|
pass
|
|
|
|
|
2010-02-24 20:06:59 +01:00
|
|
|
|
|
|
|
class JSONField(models.TextField):
|
|
|
|
__metaclass__ = models.SubfieldBase
|
2010-09-12 22:03:48 +02:00
|
|
|
|
2010-02-24 20:06:59 +01:00
|
|
|
description = ("JSONField automatically serializes and desializes values to "
|
|
|
|
"and from JSON.")
|
2010-09-12 22:03:48 +02:00
|
|
|
|
2010-02-24 20:06:59 +01:00
|
|
|
def to_python(self, value):
|
|
|
|
if not value:
|
|
|
|
return None
|
2010-09-12 22:03:48 +02:00
|
|
|
|
2012-07-20 14:22:00 +02:00
|
|
|
if isinstance(value, six.string_types):
|
2010-02-24 20:06:59 +01:00
|
|
|
value = json.loads(value)
|
|
|
|
return value
|
2010-09-12 22:03:48 +02:00
|
|
|
|
2011-03-30 19:48:42 +02:00
|
|
|
def get_db_prep_save(self, value, connection):
|
2010-02-24 20:06:59 +01:00
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
return json.dumps(value)
|