I've been generally thinking about putting together a package of reusable tools to implement common "patterns" for reusable apps. Included in this would be this type of auto-discovery system as well as a registration pattern. Unfortunately I probably won't get much practical work done on this too soon due to other obligations.
That being said I'd be very interested to see what initial stabs other people may take in this direction or what other patterns would make sense for this type of package. Once a toolset of this nature does exist, I think it'll be a great asset to reusable apps and even more so to the domain specific frameworks like imagekit or haystack. ____________________________ Sean O'Connor http://seanoc.com On Tue, Jun 1, 2010 at 3:34 PM, Gabriel Hurley <gab...@gmail.com> wrote: > I like the idea of boxing up the autodiscover code into a publicly > accessible utility class... it's a problem many people solve for > themselves (myself included) but even moreso it would go well with the > new startup.py proposal that's in the works. > > Once it's easy to have things run once at startup, the desire to run > some sort of autodiscovery mechanism from those startup files is > likely to take off. And it's not that it's a hard problem for someone > to solve themselves, but it might be one worth solving once and being > done with it. > > The downside, of course, is the cost of maintaining it and ensuring > backwards compatibility going forward. > > All the best, > > - Gabriel > > On Jun 1, 12:02 pm, Gregor Müllegger <gre...@muellegger.de> wrote: > > I also needed this functionality in one of my apps and just went the > > way of copy and pasting the existing solution that is already > > implemented in django.contrib.admin. But making this code reuseable > > would IMO be a benefit for some app developers. > > > > But I think the approach of using an setting for this purpose is not > > the ideal way. Admin does a great job in that way, because it only > > uses the autodiscover functionality on demand. My need for an > > autodiscover functionality was directly tight to a management command. > > I don't want to load these modules unless I invoke the management > > command, which is possible with the admin like autodiscover() function > > instead of using a setting. > > > > My proposal would go into the direction of pulling the autodiscover > > function out of the django.contrib.admin module and putting it > > somewhere in the core. Since the admin needs some special > > functionality on errors while loading an admin.py module from an app, > > I would suggest a Autodiscover class that provides some hooks to be > > better customizable. > > > > Something like: > > > > class Autodiscover(object): > > def __init__(self, module_name): > > self.module_name = module_name > > > > def __call__(self): > > # existing autodiscover code from django.contrib.admin > > > > def pre_load(self, app_label): > > # gets called directly before trying to load an app's module > > > > def post_load(self, app_label): > > # gets called after trying to load an app's module even if > the > > # import failed > > > > def on_success(self, app_label, module): > > # gets called if the module was imported successfully > > > > def on_error(self, app_label): > > # gets called if the module didn't exist or import failed > > > > In django.contrib.admin or in any other app we can use: > > > > from django.core.autodiscover import Autodiscover > > > > autodiscover = Autodiscover('admin') > > > > This also provides backwards compatibility since the following snippet > still > > works: > > > > from django.contrib import admin > > > > admin.autodiscover() > > > > Yours, > > Gregor Müllegger > > -- > You received this message because you are subscribed to the Google Groups > "Django developers" group. > To post to this group, send email to django-develop...@googlegroups.com. > To unsubscribe from this group, send email to > django-developers+unsubscr...@googlegroups.com<django-developers%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-developers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.