# Imports
from superdjango.exceptions import ViewAlreadyExists
# Exports
__all__ = (
"AJAX",
"ajax",
)
# Classes
[docs]class AJAX(object):
"""Capture AJAX views.
.. code-block:: python
# settings.py
INSTALLED_APPS = [
"superdjango.ajax.apps.AutoConfig",
]
# urls.py
from superdjango.ajax import ajax
urlpatterns = [
# ...
path('ajax/', include(ajax.get_urls())),
# ...
]
"""
[docs] def __init__(self):
"""Initialize an AJAX registry."""
self._registry = dict()
[docs] def get_urls(self):
"""Get the URLs for the site's AJAX views.
:rtype: list
"""
urlpatterns = list()
for name, viewset in list(self._registry.items()):
urlpatterns += viewset().get_urls(prefix=name + "/")
return urlpatterns
[docs] def register(self, app_name, viewset):
"""Register an AJAX viewset.
:param app_name: The name of the app.
:type app_name: str
:param viewset: The viewset class to register.
:type viewset: ViewSet
:raise: ViewAlreadyExists
"""
if app_name in self._registry:
raise ViewAlreadyExists(app_name + " (AJAX)")
self._registry[app_name] = viewset
ajax = AJAX()