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 smart_str, 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 'SetProperty',]
42
43
44 DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering',
45 'app_label')
48 """ class based on django.db.models.options. We only keep
49 useful bits."""
50
51 - def __init__(self, meta, app_label=None):
52 self.module_name, self.verbose_name = None, None
53 self.verbose_name_plural = None
54 self.object_name, self.app_label = None, app_label
55 self.meta = meta
56 self.admin = None
57
59 cls._meta = self
60 self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
61
62 self.object_name = cls.__name__
63 self.module_name = self.object_name.lower()
64 self.verbose_name = get_verbose_name(self.object_name)
65
66
67 if self.meta:
68 meta_attrs = self.meta.__dict__.copy()
69 for name in self.meta.__dict__:
70
71
72
73 if name.startswith('_'):
74 del meta_attrs[name]
75 for attr_name in DEFAULT_NAMES:
76 if attr_name in meta_attrs:
77 setattr(self, attr_name, meta_attrs.pop(attr_name))
78 elif hasattr(self.meta, attr_name):
79 setattr(self, attr_name, getattr(self.meta, attr_name))
80
81
82
83 setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's')))
84
85
86 if meta_attrs != {}:
87 raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
88 else:
89 self.verbose_name_plural = string_concat(self.verbose_name, 's')
90 del self.meta
91
93 return "%s.%s" % (smart_str(self.app_label), smart_str(self.module_name))
94
96 """
97 There are a few places where the untranslated verbose name is needed
98 (so that we get the same value regardless of currently active
99 locale).
100 """
101 lang = get_language()
102 deactivate_all()
103 raw = force_unicode(self.verbose_name)
104 activate(lang)
105 return raw
106 verbose_name_raw = property(verbose_name_raw)
107
139
141 """ Document object for django extension """
142 __metaclass__ = DocumentMeta
143
144 get_id = property(lambda self: self['_id'])
145 get_rev = property(lambda self: self['_rev'])
146
147 @classmethod
149 db = getattr(cls, '_db', None)
150 if db is None:
151 app_label = getattr(cls._meta, "app_label")
152 db = get_db(app_label)
153 cls._db = db
154 return db
155
156 DocumentSchema = schema.DocumentSchema
157
158
159 Property = schema.Property
160 StringProperty = schema.StringProperty
161 IntegerProperty = schema.IntegerProperty
162 DecimalProperty = schema.DecimalProperty
163 BooleanProperty = schema.BooleanProperty
164 FloatProperty = schema.FloatProperty
165 DateTimeProperty = schema.DateTimeProperty
166 DateProperty = schema.DateProperty
167 TimeProperty = schema.TimeProperty
168 SchemaProperty = schema.SchemaProperty
169 SchemaListProperty = schema.SchemaListProperty
170 ListProperty = schema.ListProperty
171 DictProperty = schema.DictProperty
172 StringListProperty = schema.StringListProperty
173 SchemaDictProperty = schema.SchemaDictProperty
174 SetProperty = schema.SetProperty
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