Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from superdjango.exceptions import ViewAlreadyExists
5# Exports
7__all__ = (
8 "AJAX",
9 "ajax",
10)
12# Classes
15class AJAX(object):
16 """Capture AJAX views.
18 .. code-block:: python
20 # settings.py
21 INSTALLED_APPS = [
22 "superdjango.ajax.apps.AutoConfig",
23 ]
25 # urls.py
26 from superdjango.ajax import ajax
28 urlpatterns = [
29 # ...
30 path('ajax/', include(ajax.get_urls())),
31 # ...
32 ]
34 """
35 def __init__(self):
36 """Initialize an AJAX registry."""
37 self._registry = dict()
39 def get_urls(self):
40 """Get the URLs for the site's AJAX views.
42 :rtype: list
44 """
45 urlpatterns = list()
47 for name, viewset in list(self._registry.items()):
48 urlpatterns += viewset().get_urls(prefix=name + "/")
50 return urlpatterns
52 def register(self, app_name, viewset):
53 """Register an AJAX viewset.
55 :param app_name: The name of the app.
56 :type app_name: str
58 :param viewset: The viewset class to register.
59 :type viewset: ViewSet
61 :raise: ViewAlreadyExists
63 """
64 if app_name in self._registry:
65 raise ViewAlreadyExists(app_name + " (AJAX)")
67 self._registry[app_name] = viewset
70ajax = AJAX()