Created
May 7, 2013 20:57
-
-
Save airtonix/5536061 to your computer and use it in GitHub Desktop.
And example of how I deal with automatically registering tastypie resources for the urlconf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PROJECT/base.lib.discovery | |
# | |
from functools import wraps | |
import traceback | |
import sys | |
registry = {} | |
def autodiscover(submodule_name=None, variables=None, callback=None): | |
""" | |
Auto-discover INSTALLED_APPS {submodule_name}.py modules and fail silently when | |
not present. | |
""" | |
import copy | |
from django.conf import settings | |
from django.utils.importlib import import_module | |
from django.utils.module_loading import module_has_submodule | |
registry = {} | |
before_import_registry = None | |
pointless_error_message = "No module named {}".format(submodule_name) | |
for app in settings.INSTALLED_APPS: | |
mod = import_module(app) | |
# Attempt to import the app's submodule. | |
try: | |
before_import_registry = copy.copy(registry) | |
path = '{app}.{submodule}'.format( | |
app=app, | |
submodule=submodule_name) | |
module = import_module(path) | |
if not variables == None: | |
if isinstance(variables, str): | |
if "," in variables: | |
variables = variables.split(",") | |
else: | |
variables = (variables, ) | |
available_variables = ( getattr(module, variable, None) for variable in variables ) | |
callback(*available_variables) | |
except Exception as error : | |
if not pointless_error_message in error.message: | |
print path | |
print traceback.format_exc() | |
# Reset the model registry to the state before the last import as | |
# this import will have to reoccur on the next request and this | |
# could raise NotRegistered and AlreadyRegistered exceptions | |
# (see #8245). | |
registry = before_import_registry | |
# Decide whether to bubble up this error. If the app just | |
# doesn't have an admin module, we can ignore the error | |
# attempting to import it, otherwise we want it to bubble up. | |
if module_has_submodule(mod, submodule_name): | |
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# myapp.api.resources | |
# | |
from tastypie.resources import ModelResource | |
from an_imaginary_app_that_has_things import models | |
class ThingResource(ModelResource): | |
class Meta: | |
queryset = models.Thing.objects.all() | |
enabled_resources = [ | |
ThingResource, | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PROJECT/base.settings | |
# | |
#... | |
INSTALLED_APPS = ( | |
#... | |
'base', | |
'myapp', | |
#... | |
) | |
#... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PROJECT/base.urls | |
# | |
from base.lib.discovery import autodiscover | |
v1_api = Api(api_name='v1') | |
def register_resources(resources=None): | |
if not resources is None: | |
for resource in resources: | |
v1_api.register(resource()) | |
autodiscover('api.resources', 'enabled_resources', register_resources) | |
urlpatterns = patterns('', | |
surl(r'^api/', include(v1_api.urls)), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment