Author: floguy
Date: Sun Sep 7 14:46:07 2008
New Revision: 7
Modified:
trunk/things/__init__.py
trunk/things/options.py
trunk/things/sites.py
trunk/thingsproject/twitter/thing.py
trunk/thingsproject/urls.py
Log:
More refactoring and fixups.
Modified: trunk/things/__init__.py
==============================================================================
--- trunk/things/__init__.py (original)
+++ trunk/things/__init__.py Sun Sep 7 14:46:07 2008
@@ -1,16 +1,40 @@
from things.options import ModelThing
-from things.sites import ThingSite, thing_site
-from things import fields
+from things.sites import ThingSite, site
+from things.fields import OrderField
def autodiscover():
"""
- Auto-discover INSTALLED_APPS thing.py modules and fail silently when
- not present. This forces an import on them to register any admin bits
they
+ Auto-discover INSTALLED_APPS thing.py modules and fail silently when
+ not present. This forces an import on them to register any thing bits
they
may want.
"""
+ import imp
from django.conf import settings
+
for app in settings.INSTALLED_APPS:
+ # For each app, we need to look for an thing.py inside that app's
+ # package. We can't use os.path here -- recall that modules may be
+ # imported different ways (think zip files) -- so we need to get
+ # the app's __path__ and look for thing.py on that path.
+
+ # Step 1: find out the app's __path__ Import errors here will (and
+ # should) bubble up, but a missing __path__ (which is legal, but
weird)
+ # fails silently -- apps that do weird things with __path__ might
+ # need to roll their own thing registration.
try:
- __import__("%s.thing" % app)
+ app_path = __import__(app, {}, {},
[app.split('.')[-1]]).__path__
+ except AttributeError:
+ continue
+
+ # Step 2: use imp.find_module to find the app's thing.py. For some
+ # reason imp.find_module raises ImportError if the app can't be
found
+ # but doesn't actually try to import the module. So skip this app
if
+ # its thing.py doesn't exist
+ try:
+ imp.find_module('thing', app_path)
except ImportError:
- pass
\ No newline at end of file
+ continue
+
+ # Step 3: import the app's thing file. If this has errors we want
them
+ # to bubble up.
+ __import__("%s.thing" % app)
\ No newline at end of file
Modified: trunk/things/options.py
==============================================================================
--- trunk/things/options.py (original)
+++ trunk/things/options.py Sun Sep 7 14:46:07 2008
@@ -37,18 +37,22 @@
def urls(self, prefix, name_prefix):
self.name_prefix = name_prefix # Is this robust enough? Needs
testing.
tmp_urls = [
- url('%sorder/%s/%s/' % (prefix,
- field.field_url, field.url_asc),
+ url('%sorder/%s/%s/' % (prefix, field.field_url,
field.url_asc),
self.list_view,
- {'descending': False, 'field':
self.fields.get(field.field_name, None)},
+ {
+ 'descending': False,
+ 'field': self.fields.get(field.field_name, None),
+ },
name='%s_%s_list_asc' % (name_prefix, field.field_url))
for (name, field) in self.fields.iteritems()
]
tmp_urls.extend([
- url('%sorder/%s/%s/' % (prefix,
- field.field_url, field.url_desc),
+ url('%sorder/%s/%s/' % (prefix, field.field_url,
field.url_desc),
self.list_view,
- {'descending': True, 'field':
self.fields.get(field.field_name, None)},
+ {
+ 'descending': True,
+ 'field': self.fields.get(field.field_name, None)
+ },
name='%s_%s_list_desc' % (name_prefix, field.field_url))
for (name, field) in self.fields.iteritems()
])
@@ -63,10 +67,14 @@
if descending == True:
pre = '-'
items = items.order_by('%s%s' % (pre, field.field_name))
- context = {'items': items, 'fields': self.fields.values(),
- 'field': field, 'name_prefix': self.name_prefix}
+ context = {
+ 'items': items,
+ 'fields': self.fields.values(),
+ 'field': field,
+ 'name_prefix': self.name_prefix,
+ }
return render_to_response('things/thing_list.html', context,
- context_instance=RequestContext(request))
+ context_instance=RequestContext(request))
def detail_view(self, request, pk):
# TODO: Flesh this out for realz
Modified: trunk/things/sites.py
==============================================================================
--- trunk/things/sites.py (original)
+++ trunk/things/sites.py Sun Sep 7 14:46:07 2008
@@ -1,7 +1,7 @@
import re
+import things
from django.conf.urls.defaults import *
from django.db.models.base import ModelBase
-from things import ModelThing
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
@@ -29,13 +29,23 @@
If a model is already registered, this will raise
AlreadyRegistered.
"""
- thing_class = thing_class or ThingAdmin
+ #thing_class = thing_class or ThingAdmin
if isinstance(model_or_iterable, ModelBase):
model_or_iterable = (model_or_iterable,)
for model in model_or_iterable:
if model in self._registry:
raise AlreadyRegistered('The model %s is already
registered' % model.__name__)
- self._registry[model] = thing_class(model, self)
+ if not thing_class:
+ thing = things.ModelThing(model, self)
+ thing.fields = {}
+ for field in model._meta.fields:
+ of = things.OrderField()
+ of.parent = thing
+ of.field_name = field.attname
+ thing.fields[field.attname] = of
+ else:
+ thing = thing_class(model, self)
+ self._registry[model] = thing
def unregister(self, model_or_iterable):
"""
@@ -94,4 +104,4 @@
}
return render_to_response('things/index.html', context,
context_instance=RequestContext(request))
-thing_site = ThingSite()
\ No newline at end of file
+site = ThingSite()
\ No newline at end of file
Modified: trunk/thingsproject/twitter/thing.py
==============================================================================
--- trunk/thingsproject/twitter/thing.py (original)
+++ trunk/thingsproject/twitter/thing.py Sun Sep 7 14:46:07 2008
@@ -1,10 +1,10 @@
-from things import ModelThing, thing_site, fields
+import things
from models import Tweet
-class TweetThing(ModelThing):
- date_posted = fields.OrderField(verbose_name_asc='Newest',
- verbose_name_desc='Oldest', url_asc='newest',
- url_desc='oldest', field_url='date')
- message = fields.OrderField()
+class TweetThing(things.ModelThing):
+ date_posted = things.OrderField(verbose_name_asc='Newest',
+ verbose_name_desc='Oldest', url_asc='newest', url_desc='oldest',
+ field_url='date')
+ message = things.OrderField()
-thing_site.register(Tweet, TweetThing)
+things.site.register(Tweet, TweetThing)
Modified: trunk/thingsproject/urls.py
==============================================================================
--- trunk/thingsproject/urls.py (original)
+++ trunk/thingsproject/urls.py Sun Sep 7 14:46:07 2008
@@ -1,6 +1,6 @@
from django.conf.urls.defaults import *
from django.contrib import admin
-from things import thing_site, autodiscover
+import things
admin.autodiscover()
@@ -8,6 +8,6 @@
(r'^admin/(.*)$', admin.site.root),
)
-autodiscover()
+things.autodiscover()
-urlpatterns += thing_site.urls()
\ No newline at end of file
+urlpatterns += things.site.urls()
\ No newline at end of file
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pinax-updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pinax-updates?hl=en
-~----------~----~----~----~------~----~------~--~---