2014-03-15 01:34:49 +08:00
|
|
|
import json
|
|
|
|
|
2015-01-11 00:11:15 +08:00
|
|
|
from django.contrib.postgres import forms, lookups
|
2014-03-15 01:34:49 +08:00
|
|
|
from django.contrib.postgres.fields.array import ArrayField
|
|
|
|
from django.core import exceptions
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models import Field, TextField, Transform
|
2014-03-15 01:34:49 +08:00
|
|
|
from django.utils import six
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
__all__ = ['HStoreField']
|
|
|
|
|
|
|
|
|
|
|
|
class HStoreField(Field):
|
|
|
|
empty_strings_allowed = False
|
|
|
|
description = _('Map of strings to strings')
|
|
|
|
default_error_messages = {
|
|
|
|
'not_a_string': _('The value of "%(key)s" is not a string.'),
|
|
|
|
}
|
|
|
|
|
|
|
|
def db_type(self, connection):
|
|
|
|
return 'hstore'
|
|
|
|
|
|
|
|
def get_transform(self, name):
|
|
|
|
transform = super(HStoreField, self).get_transform(name)
|
|
|
|
if transform:
|
|
|
|
return transform
|
|
|
|
return KeyTransformFactory(name)
|
|
|
|
|
|
|
|
def validate(self, value, model_instance):
|
|
|
|
super(HStoreField, self).validate(value, model_instance)
|
|
|
|
for key, val in value.items():
|
|
|
|
if not isinstance(val, six.string_types):
|
|
|
|
raise exceptions.ValidationError(
|
|
|
|
self.error_messages['not_a_string'],
|
|
|
|
code='not_a_string',
|
|
|
|
params={'key': key},
|
|
|
|
)
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
if isinstance(value, six.string_types):
|
|
|
|
value = json.loads(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def value_to_string(self, obj):
|
2015-04-26 14:30:46 +08:00
|
|
|
return json.dumps(self.value_from_object(obj))
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
|
|
|
defaults = {
|
|
|
|
'form_class': forms.HStoreField,
|
|
|
|
}
|
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(HStoreField, self).formfield(**defaults)
|
|
|
|
|
|
|
|
|
2015-01-11 00:11:15 +08:00
|
|
|
HStoreField.register_lookup(lookups.DataContains)
|
|
|
|
HStoreField.register_lookup(lookups.ContainedBy)
|
2015-05-31 04:22:36 +08:00
|
|
|
HStoreField.register_lookup(lookups.HasKey)
|
|
|
|
HStoreField.register_lookup(lookups.HasKeys)
|
|
|
|
HStoreField.register_lookup(lookups.HasAnyKeys)
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
class KeyTransform(Transform):
|
|
|
|
output_field = TextField()
|
|
|
|
|
|
|
|
def __init__(self, key_name, *args, **kwargs):
|
|
|
|
super(KeyTransform, self).__init__(*args, **kwargs)
|
|
|
|
self.key_name = key_name
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
lhs, params = compiler.compile(self.lhs)
|
2015-05-13 21:52:19 +08:00
|
|
|
return "(%s -> '%s')" % (lhs, self.key_name), params
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
class KeyTransformFactory(object):
|
|
|
|
|
|
|
|
def __init__(self, key_name):
|
|
|
|
self.key_name = key_name
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
return KeyTransform(self.key_name, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
@HStoreField.register_lookup
|
2015-08-03 10:30:06 +08:00
|
|
|
class KeysTransform(Transform):
|
2014-03-15 01:34:49 +08:00
|
|
|
lookup_name = 'keys'
|
2015-01-11 00:11:15 +08:00
|
|
|
function = 'akeys'
|
2014-03-15 01:34:49 +08:00
|
|
|
output_field = ArrayField(TextField())
|
|
|
|
|
|
|
|
|
|
|
|
@HStoreField.register_lookup
|
2015-08-03 10:30:06 +08:00
|
|
|
class ValuesTransform(Transform):
|
2014-03-15 01:34:49 +08:00
|
|
|
lookup_name = 'values'
|
2015-01-11 00:11:15 +08:00
|
|
|
function = 'avals'
|
2014-03-15 01:34:49 +08:00
|
|
|
output_field = ArrayField(TextField())
|