Package couchdbkit :: Package ext :: Package django :: Module schema
[hide private]
[frames] | no frames]

Source Code for Module couchdbkit.ext.django.schema

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (c) 2008-2009 Benoit Chesneau <benoitc@e-engura.com> 
  4  # 
  5  # Permission to use, copy, modify, and distribute this software for any 
  6  # purpose with or without fee is hereby granted, provided that the above 
  7  # copyright notice and this permission notice appear in all copies. 
  8  # 
  9  # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
 10  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
 11  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
 12  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
 13  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 14  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
 15  # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 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') 
46 47 -class Options(object):
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
58 - def contribute_to_class(self, cls, name):
59 cls._meta = self 60 self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS 61 # First, construct the default values for these options. 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 # Next, apply any overridden values from 'class Meta'. 67 if self.meta: 68 meta_attrs = self.meta.__dict__.copy() 69 for name in self.meta.__dict__: 70 # Ignore any private attributes that Django doesn't care about. 71 # NOTE: We can't modify a dictionary's contents while looping 72 # over it, so we loop over the *original* dictionary instead. 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 # verbose_name_plural is a special case because it uses a 's' 82 # by default. 83 setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's'))) 84 85 # Any leftover attributes must be invalid. 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
92 - def __str__(self):
93 return "%s.%s" % (smart_str(self.app_label), smart_str(self.module_name))
94
95 - def verbose_name_raw(self):
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
108 -class DocumentMeta(schema.SchemaProperties):
109 - def __new__(cls, name, bases, attrs):
110 super_new = super(DocumentMeta, cls).__new__ 111 parents = [b for b in bases if isinstance(b, DocumentMeta)] 112 if not parents: 113 return super_new(cls, name, bases, attrs) 114 115 new_class = super_new(cls, name, bases, attrs) 116 attr_meta = attrs.pop('Meta', None) 117 if not attr_meta: 118 meta = getattr(new_class, 'Meta', None) 119 else: 120 meta = attr_meta 121 122 if getattr(meta, 'app_label', None) is None: 123 document_module = sys.modules[new_class.__module__] 124 app_label = document_module.__name__.split('.')[-2] 125 else: 126 app_label = getattr(meta, 'app_label') 127 128 new_class.add_to_class('_meta', Options(meta, app_label=app_label)) 129 130 register_schema(app_label, new_class) 131 132 return get_schema(app_label, name)
133
134 - def add_to_class(cls, name, value):
135 if hasattr(value, 'contribute_to_class'): 136 value.contribute_to_class(cls, name) 137 else: 138 setattr(cls, name, value)
139
140 -class Document(schema.Document):
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
148 - def get_db(cls):
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 # properties 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 # some utilities 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