Skip to content

Commit

Permalink
Add StringDictProperty
Browse files Browse the repository at this point in the history
Ensures that all dict values are string types. The DictProperty
converts the value '802.11' (see example below) into a Decimal
property. The StringDictProperty prevents this conversion.

Example:

  {
    "name" : {
      "de" : "Stadt",
      "en" : "City",
      "xx" : "802.11"
    }
  }
  • Loading branch information
Dominik Fässler committed Aug 15, 2013
1 parent 3d3b8c4 commit 907cbdb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion couchdbkit/__init__.py
Expand Up @@ -26,7 +26,7 @@
DocumentSchema, DocumentBase, Document, StaticDocument, contain,
QueryMixin, AttachmentMixin,
SchemaProperty, SchemaListProperty, SchemaDictProperty,
ListProperty, DictProperty, StringListProperty, SetProperty
ListProperty, DictProperty, StringDictProperty, StringListProperty, SetProperty
)

import logging
Expand Down
5 changes: 3 additions & 2 deletions couchdbkit/ext/django/schema.py
Expand Up @@ -37,8 +37,8 @@
'value_to_python', 'dict_to_python', 'list_to_python',
'convert_property', 'DocumentSchema', 'Document',
'SchemaProperty', 'SchemaListProperty', 'ListProperty',
'DictProperty', 'StringListProperty', 'SchemaDictProperty',
'SetProperty',]
'DictProperty', 'StringDictProperty', 'StringListProperty',
'SchemaDictProperty', 'SetProperty',]


DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering',
Expand Down Expand Up @@ -169,6 +169,7 @@ def get_db(cls):
SchemaListProperty = schema.SchemaListProperty
ListProperty = schema.ListProperty
DictProperty = schema.DictProperty
StringDictProperty = schema.StringDictProperty
StringListProperty = schema.StringListProperty
SchemaDictProperty = schema.SchemaDictProperty
SetProperty = schema.SetProperty
Expand Down
1 change: 1 addition & 0 deletions couchdbkit/schema/__init__.py
Expand Up @@ -168,6 +168,7 @@ class A(Dcoument):
DateProperty,
TimeProperty,
DictProperty,
StringDictProperty,
ListProperty,
StringListProperty,
SetProperty,
Expand Down
20 changes: 18 additions & 2 deletions couchdbkit/schema/properties.py
Expand Up @@ -25,8 +25,8 @@ def is_iterable(c):
__all__ = ['ALLOWED_PROPERTY_TYPES', 'Property', 'StringProperty',
'IntegerProperty', 'DecimalProperty', 'BooleanProperty',
'FloatProperty', 'DateTimeProperty', 'DateProperty',
'TimeProperty', 'DictProperty', 'ListProperty',
'StringListProperty',
'TimeProperty', 'DictProperty', 'StringDictProperty',
'ListProperty', 'StringListProperty',
'dict_to_json', 'list_to_json',
'value_to_json', 'MAP_TYPES_PROPERTIES', 'value_to_python',
'dict_to_python', 'list_to_python', 'convert_property',
Expand Down Expand Up @@ -452,6 +452,22 @@ def to_json(self, value):



class StringDictProperty(DictProperty):

def to_python(self, value):
return LazyDict(value, item_type=basestring)

def validate_dict_contents(self, value):
try:
value = validate_dict_content(value, basestring)
except BadValueError:
raise BadValueError(
'Items of %s dict must all be in %s' %
(self.name, basestring))
return value



class ListProperty(Property):
"""A property that stores a list of things.
Expand Down

0 comments on commit 907cbdb

Please sign in to comment.