January 1, 2009
Mobile Django Admin Patches
The first patch was simply removing an extra slash from a URL in a template. No big deal:
The second patch took a bit more research to figure out what was going on. The symptom I was witnessing was that the mobile interface looked fine in my iPhone until I tried to view a model's list of objects. It kept showing me the default admin view (as you would expect to on the desktop) and it didn't jive with the (screenshots I saw on Flickr)[http://www.flickr.com/photos/jannis/sets/72157607145456697/] that Jannis had posted showing off the app.
After seeing some drastic differences between the output of a
dir command on
admin_site variable and the contents of
quickly became apparent that
__dict__ on the object wasn't showing the whole
picture and thus there was a bug in the
I then found Keith Waclena's excellent
course documentation describing the problem perfectly. It turns out that
only reveals the members of the class or instance and NOT the inherited
members, so one must recurse down through base classes to be complete. So, I
borrowed his recursive lookup function, and rewrote a couple lines to approach
the lookup differently but accomplishing the same intent (at least I hope so
-- it seems to work as intended for me):
commit 5945491fe1216239471d41e590425bfe384d3d99 Author: Patrick Altman <patrick@###.com> Date: Thu Jan 1 00:52:46 2009 -0600 fixed bug in autoregister where attribute searching didn't search deep enough (didn't search base classes) diff --git a/mobileadmin/__init__.py b/mobileadmin/__init__.py index f8658ac..77e0de1 100644 --- a/mobileadmin/__init__.py +++ b/mobileadmin/__init__.py @@ -4,6 +4,21 @@ from django.core.exceptions import ImproperlyConfigured from mobileadmin import decorators, views from mobileadmin.conf import settings +### From http://www2.lib.uchicago.edu/keith/courses/python/class/5/#attrref +def classlookup(C, name): + if C.__dict__.has_key(name): + return (1, C.__dict__[name]) + else: + for b in C.__bases__: + success, value = classlookup(b, name) + if success: + return (1, value) + else: + pass + else: + return (0, None) + + def autoregister(): """ Auto-register all ModelAdmin instances of the default AdminSite with the @@ -14,11 +29,11 @@ def autoregister(): for model, modeladmin in main_site._registry.iteritems(): admin_class = modeladmin.__class__ - - for name, value in admin_class.__dict__.iteritems(): - if name in settings.TEMPLATE_MAPPING: + for name in settings.TEMPLATE_MAPPING: + (found, value) = classlookup(admin_class, name) + if found: setattr(admin_class, name, decorators.mobile_templates(value)) - + if admin_class == UserAdmin: setattr(admin_class, 'add_view', views.auth_add_view)
I have pushed in these changes to my fork of the git project and sent pull requests to Jannis. Hopefully, he'll integrate them into his project soon, but in the meantime, my fork is available.
Being that this occurred shortly after the stroke of midnight, it is officially the first thing that I have done in 2009!
Happy New Year!