Updated object deletion to match django 1.3's handling and thus multiple dbs.
[philo.git] / contrib / gilbert / plugins / models.py
index 0ba310e..ee13989 100644 (file)
@@ -1,15 +1,17 @@
 from django.conf import settings
 from django.contrib.admin.util import lookup_field, label_for_field, display_for_field, NestedObjects
 from django.core.exceptions import PermissionDenied
+from django.db import router
 from django.db.models import Q
 from django.db.models.fields.related import ManyToOneRel
 from django.db.models.fields.files import FieldFile, ImageFieldFile, FileField
 from django.forms.models import ModelForm, modelform_factory
+from django.template.defaultfilters import capfirst
 from django.utils import simplejson as json
 from django.utils.encoding import smart_unicode
 from .base import Plugin
 from ..extdirect import ext_action, ext_method
-import operator, staticmedia
+import operator
 
 
 @ext_action(name='models')
@@ -23,8 +25,8 @@ class Models(Plugin):
        @property
        def index_js_urls(self):
                return super(Models, self).index_js_urls + [
-                       staticmedia.url('gilbert/extjs/examples/ux/SearchField.js'),
-                       staticmedia.url('gilbert/plugins/models.js'),
+                       settings.STATIC_URL + 'gilbert/extjs/examples/ux/SearchField.js',
+                       settings.STATIC_URL + 'gilbert/plugins/models.js',
                ]
        
        @property
@@ -64,7 +66,7 @@ class ModelAdmin(Plugin):
        def data_serialize_model_instance(cls, obj):
                return {
                        'app_label': obj._meta.app_label,
-                       'name': obj._meta.object_name,
+                       'name': obj._meta.module_name,
                        'pk': obj.pk,
                        '__unicode__': unicode(obj),
                }
@@ -119,6 +121,7 @@ class ModelAdmin(Plugin):
        @property
        def data_columns_spec(self):
                spec = []
+               
                for field_name in self.data_columns:
                        column = {
                                'dataIndex': field_name,
@@ -126,11 +129,13 @@ class ModelAdmin(Plugin):
                                'editable': False,
                        }
                        header, attr = label_for_field(field_name, self.model, model_admin=self, return_attr=True)
-                       column['header'] = header
+                       column['header'] = capfirst(header)
                        if (field_name in self.sortable_fields) or (getattr(attr, 'admin_order_field', None) in self.sortable_fields):
                                column['sortable'] = True
                        if field_name in self.data_editable_columns:
                                column['editable'] = True
+                       if field_name in [field.name for field in self.model_meta.fields if isinstance(field.rel, ManyToOneRel)]:
+                               column['xtype'] = 'foreignkeycolumn'
                        spec.append(column)
                return spec
        
@@ -170,17 +175,20 @@ class ModelAdmin(Plugin):
                        raise PermissionDenied
                return self.model._default_manager.all()
        
+       def queryset(self, request):
+               return self.model._default_manager.get_query_set()
+       
        @ext_method
        def filter(self, request, **kwargs):
                if not self.has_read_permission(request):
                        raise PermissionDenied
-               return self.model._default_manager.all().filter(**kwargs)
+               return self.queryset(request).filter(**kwargs)
        
        @ext_method
        def get(self, request, **kwargs):
                if not self.has_read_permission(request):
                        raise PermissionDenied
-               return self.model._default_manager.all().values().get(**kwargs)
+               return self.queryset(request).values().get(**kwargs)
        
        @property
        def form_class(self):
@@ -371,9 +379,8 @@ class ModelAdmin(Plugin):
                        pks = [pks]
                objs = [self.model._default_manager.all().get(pk=pk) for pk in pks]
                
-               collector = NestedObjects()
-               
-               for obj in objs:
-                       obj._collect_sub_objects(collector)
+               using = router.db_for_write(self.model)
+               collector = NestedObjects(using=using)
+               collector.collect(objs)
                
                return collector.nested(self.data_serialize_model_instance)
\ No newline at end of file