1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """ Wrapper of couchdbkit Document and Properties for django. It also
18 add possibility to a document to register itself in CouchdbkitHandler
19 """
20 import re
21 import sys
22
23 from django.conf import settings
24 from django.db.models.options import get_verbose_name
25 from django.utils.translation import activate, deactivate_all, get_language, \
26 string_concat
27 from django.utils.encoding import force_unicode
28
29 from couchdbkit import schema
30 from couchdbkit.ext.django.loading import get_schema, register_schema, \
31 get_db
32
33 __all__ = ['Property', 'StringProperty', 'IntegerProperty',
34 'DecimalProperty', 'BooleanProperty', 'FloatProperty',
35 'DateTimeProperty', 'DateProperty', 'TimeProperty',
36 'dict_to_json', 'list_to_json', 'value_to_json',
37 'value_to_python', 'dict_to_python', 'list_to_python',
38 'convert_property', 'DocumentSchema', 'Document',
39 'SchemaProperty', 'SchemaListProperty', 'ListProperty',
40 'DictProperty', 'StringListProperty', 'SchemaDictProperty']
41
42
43 DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering',
44 'app_label')
47 """ class based on django.db.models.options. We only keep
48 useful bits."""
49
50 - def __init__(self, meta, app_label=None):
51 self.module_name, self.verbose_name = None, None
52 self.verbose_name_plural = None
53 self.object_name, self.app_label = None, app_label
54 self.meta = meta
55 self.admin = None
56
58 from django.db.backends.util import truncate_name
59
60 cls._meta = self
61 self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
62
63 self.object_name = cls.__name__
64 self.module_name = self.object_name.lower()
65 self.verbose_name = get_verbose_name(self.object_name)
66
67
68 if self.meta:
69 meta_attrs = self.meta.__dict__.copy()
70 for name in self.meta.__dict__:
71
72
73
74 if name.startswith('_'):
75 del meta_attrs[name]
76 for attr_name in DEFAULT_NAMES:
77 if attr_name in meta_attrs:
78 setattr(self, attr_name, meta_attrs.pop(attr_name))
79 elif hasattr(self.meta, attr_name):
80 setattr(self, attr_name, getattr(self.meta, attr_name))
81
82
83
84 setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's')))
85
86
87 if meta_attrs != {}:
88 raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
89 else:
90 self.verbose_name_plural = string_concat(self.verbose_name, 's')
91 del self.meta
92
94 return "%s.%s" % (smart_str(self.app_label), smart_str(self.module_name))
95
97 """
98 There are a few places where the untranslated verbose name is needed
99 (so that we get the same value regardless of currently active
100 locale).
101 """
102 lang = get_language()
103 deactivate_all()
104 raw = force_unicode(self.verbose_name)
105 activate(lang)
106 return raw
107 verbose_name_raw = property(verbose_name_raw)
108
140
142 """ Document object for django extension """
143 __metaclass__ = DocumentMeta
144
145 get_id = property(lambda self: self['_id'])
146 get_rev = property(lambda self: self['_rev'])
147
148 @classmethod
150 db = getattr(cls, '_db', None)
151 if db is None:
152 app_label = getattr(cls._meta, "app_label")
153 db = get_db(app_label)
154 cls._db = db
155 return db
156
157 DocumentSchema = schema.DocumentSchema
158
159
160 Property = schema.Property
161 StringProperty = schema.StringProperty
162 IntegerProperty = schema.IntegerProperty
163 DecimalProperty = schema.DecimalProperty
164 BooleanProperty = schema.BooleanProperty
165 FloatProperty = schema.FloatProperty
166 DateTimeProperty = schema.DateTimeProperty
167 DateProperty = schema.DateProperty
168 TimeProperty = schema.TimeProperty
169 SchemaProperty = schema.SchemaProperty
170 SchemaListProperty = schema.SchemaListProperty
171 ListProperty = schema.ListProperty
172 DictProperty = schema.DictProperty
173 StringListProperty = schema.StringListProperty
174 SchemaDictProperty = schema.SchemaDictProperty
175
176
177
178
179 dict_to_json = schema.dict_to_json
180 list_to_json = schema.list_to_json
181 value_to_json = schema.value_to_json
182 value_to_python = schema.value_to_python
183 dict_to_python = schema.dict_to_python
184 list_to_python = schema.list_to_python
185 convert_property = schema.convert_property
186