Commits

Anonymous committed 9c67d36

finished gae settings API

Comments (0)

Files changed (5)

+from .db_settings import get_indexes
+
 import datetime
 import sys
 
 class SQLInsertCompiler(NonrelInsertCompiler, SQLCompiler):
     @safe_call
     def insert(self, data, return_id=False):
-        kwds = {}
         gae_data = {}
+        opts = self.query.get_meta()
+        indexes = get_indexes().get(self.query.model, {})
+        unindexed_fields = indexes.get('unindexed', ())
+        unindexed_cols = [opts.get_field(name).column
+                          for name in unindexed_fields]
+        kwds = {'unindexed_properties': unindexed_cols}
         for column, value in data.items():
-            if column == self.query.get_meta().pk.column:
+            if column == opts.pk.column:
                 if isinstance(value, basestring):
                     kwds['name'] = value
                 else:
-from .db_settings import FIELD_INDEXES
+from .db_settings import get_indexes
 from djangotoolbox.db.creation import NonrelDatabaseCreation
 
-# TODO: add support for specifying index for non-string fields
 class StringType(object):
     def __init__(self, internal_type):
         self.internal_type = internal_type
 
     def __mod__(self, field):
-        app_label = field['model']._meta.app_label
-        name = field['model']._meta.object_name
-        path = '%s.%s.%s' % (app_label, name, field['name'])
-        index = FIELD_INDEXES.get(path)
-        if index is True:
+        indexes = get_indexes().get(field['model'], {})
+        if field['name'] in indexes.get('indexed', ()):
             return 'text'
-        elif index is False:
+        elif field['name'] in indexes.get('unindexed', ()):
             return 'longtext'
         return self.internal_type
 

db/db_settings.py

 from django.conf import settings
 from django.utils.importlib import import_module
 
-FIELD_INDEXES = {}
+_MODULE_NAMES = getattr(settings, 'GAE_SETTINGS_MODULES', ())
+
+FIELD_INDEXES = None
 
 # TODO: add support for eventual consistency setting on specific models
 
-def load_indexes():
-    index_modules = [app + '.db_settings_gae'
-                     for app in settings.INSTALLED_APPS]
-    index_modules.append('db_settings_gae')
-
-    for name in index_modules:
-        try:
-            FIELD_INDEXES.update(import_module(name).FIELD_INDEXES)
-        except (ImportError, AttributeError):
-            pass
-
-load_indexes()
+def get_indexes():
+    global FIELD_INDEXES
+    if FIELD_INDEXES is None:
+        field_indexes = {}
+        for name in _MODULE_NAMES:
+            try:
+                field_indexes.update(import_module(name).FIELD_INDEXES)
+            except (ImportError, AttributeError):
+                pass
+        FIELD_INDEXES = field_indexes
+    return FIELD_INDEXES

db_settings_gae.py

-FIELD_INDEXES = {
-    'tests.FieldsWithoutOptionsModel.indexed_text': True,
-}

tests/testmodels.py

 from django.db import models
+from ..db.db_settings import get_indexes
 
 class EmailModel(models.Model):
     email = models.EmailField()
 #    decimal = models.DecimalField() # can be None
 #    image = models.ImageField()
 
+get_indexes()[FieldsWithoutOptionsModel] = {'indexed': ('indexed_text',)}
+
 class FieldsWithOptionsModel(models.Model):
     # any type of unique (unique_data, ...) is not supported on GAE, instead you
     # can use primary_key=True for some special cases. But be carefull: changing
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.