apps.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from os.path import basename
  2. import six
  3. from django.apps import apps
  4. from .utils import rstrip_str
  5. class AppURLsMeta(type):
  6. def __init__(cls, name, bases, data):
  7. if len(bases) > 1: # execute only on leaf class
  8. exported_urlpatterns = data.pop('exported_urlpatterns', None)
  9. if exported_urlpatterns:
  10. cls.exported_urlpatterns = exported_urlpatterns
  11. else:
  12. # Default : sets
  13. # exported_urlpatterns = [(<app_name>, <app_url_module>)]
  14. current_path = '.' + rstrip_str(rstrip_str(basename(__file__), '.pyc'), '.py')
  15. url_module = rstrip_str(cls.__module__, current_path) + '.urls'
  16. cls.exported_urlpatterns = [(data['name'], url_module)]
  17. cls.urlprefix = data.pop('urlprefix', None)
  18. class AppURLs(six.with_metaclass(AppURLsMeta)):
  19. """ App Mixxin to allow an application to expose pluggable urls
  20. That's to say, URLs which will be added automatically to the projet
  21. urlpatterns.
  22. You can just make your app inherit from AppURLs, yous app urls.py will be
  23. picked and wired on project urlpatterns, using the app name as prefix.
  24. You can also customize which urlpatterns your app exposes by setting the
  25. `exported_urlpattens` on your AppConfig class as list of `<prefix>,<urlpatterns>`
  26. E.g:
  27. class MyAppConfig(AppConfig, coin.apps.AppURLS):
  28. name = 'my_app'
  29. exported_urlpatterns = [('my_app', 'myapp.cool_urls')]
  30. """
  31. pass