2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-04-30 01:58:00 +08:00
|
|
|
import json
|
2014-09-05 21:12:14 +08:00
|
|
|
import warnings
|
2012-04-30 01:58:00 +08:00
|
|
|
|
2010-02-25 03:06:59 +08:00
|
|
|
from django.db import models
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2015-07-16 09:18:07 +08:00
|
|
|
from django.utils.deconstruct import deconstructible
|
2015-06-23 01:54:35 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango110Warning
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.encoding import force_text, python_2_unicode_compatible
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
# Catch warning about subfieldbase -- remove in Django 1.10
|
2014-09-05 21:12:14 +08:00
|
|
|
warnings.filterwarnings(
|
|
|
|
'ignore',
|
|
|
|
'SubfieldBase has been deprecated. Use Field.from_db_value instead.',
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango110Warning
|
2014-09-05 21:12:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-16 09:18:07 +08:00
|
|
|
@deconstructible
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2010-02-25 03:06:59 +08: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
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-07-21 16:00:10 +08:00
|
|
|
return '%s%s' % (force_text(self.first), force_text(self.second))
|
2010-02-25 03:06:59 +08:00
|
|
|
|
2014-04-12 11:58:56 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
return self.first == other.first and self.second == other.second
|
|
|
|
return False
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-08-15 06:31:06 +08:00
|
|
|
class SmallField(six.with_metaclass(models.SubfieldBase, models.Field)):
|
2010-02-25 03:06:59 +08:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
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-31 01:48:42 +08:00
|
|
|
def get_db_prep_save(self, value, connection):
|
2012-07-20 20:48:51 +08:00
|
|
|
return six.text_type(value)
|
2010-02-25 03:06:59 +08:00
|
|
|
|
2010-03-23 22:11:41 +08:00
|
|
|
def get_prep_lookup(self, lookup_type, value):
|
2010-02-25 03:06:59 +08:00
|
|
|
if lookup_type == 'exact':
|
2012-07-21 16:00:10 +08:00
|
|
|
return force_text(value)
|
2010-02-25 03:06:59 +08:00
|
|
|
if lookup_type == 'in':
|
2012-07-21 16:00:10 +08:00
|
|
|
return [force_text(v) for v in value]
|
2010-02-25 03:06:59 +08:00
|
|
|
if lookup_type == 'isnull':
|
|
|
|
return []
|
2010-03-23 22:11:41 +08:00
|
|
|
raise TypeError('Invalid lookup type: %r' % lookup_type)
|
2010-02-25 03:06:59 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-11-03 02:40:53 +08:00
|
|
|
class SmallerField(SmallField):
|
|
|
|
pass
|
|
|
|
|
2010-02-25 03:06:59 +08:00
|
|
|
|
2012-08-15 06:31:06 +08:00
|
|
|
class JSONField(six.with_metaclass(models.SubfieldBase, models.TextField)):
|
2010-09-13 04:03:48 +08:00
|
|
|
|
2014-03-02 22:25:53 +08:00
|
|
|
description = ("JSONField automatically serializes and deserializes values to "
|
2010-02-25 03:06:59 +08:00
|
|
|
"and from JSON.")
|
2010-09-13 04:03:48 +08:00
|
|
|
|
2010-02-25 03:06:59 +08:00
|
|
|
def to_python(self, value):
|
|
|
|
if not value:
|
|
|
|
return None
|
2010-09-13 04:03:48 +08:00
|
|
|
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(value, six.string_types):
|
2010-02-25 03:06:59 +08:00
|
|
|
value = json.loads(value)
|
|
|
|
return value
|
2010-09-13 04:03:48 +08:00
|
|
|
|
2011-03-31 01:48:42 +08:00
|
|
|
def get_db_prep_save(self, value, connection):
|
2010-02-25 03:06:59 +08:00
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
return json.dumps(value)
|
2014-03-15 06:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomTypedField(models.TextField):
|
|
|
|
def db_type(self, connection):
|
|
|
|
return 'custom_field'
|