@@ -29,9 +29,30 @@ def register_form_view(cls, slug=None):
2929
3030
3131class AjaxView (View ):
32- # @method_decorator(csrf_exempt)
32+ """
33+ A Django view to handle AJAX requests for GET and POST methods for django CMS Form Builder forms.
34+ this view allows django CMS plugins to receive ajax requests if they implement the `ajax_get` and
35+ `ajax_post` methods. The form plugin implements the `ajax_post` method to handle form submissions.
36+
37+ Methods
38+ -------
39+
40+ dispatch(request, \*args, \*\*kwargs)
41+ Overrides the default dispatch method to handle AJAX requests.
42+
43+ decode_path(path)
44+ Decodes a URL path into a dictionary of parameters.
45+
46+ plugin_instance(pk)
47+ Retrieves the plugin instance and its associated model instance by primary key.
48+
49+ ajax_post(request, \*args, \*\*kwargs)
50+ Handles AJAX POST requests. Calls the `ajax_post` method of the plugin or form instance if available.
51+
52+ ajax_get(request, \*args, \*\*kwargs)
53+ Handles AJAX GET requests. Calls the `ajax_get` method of the plugin or form instance if available.
54+ """
3355 def dispatch (self , request , * args , ** kwargs ):
34- # is_ajax = request.headers.get('X-Requested-With') == "XMLHttpRequest"
3556 if request .accepts ("application/json" ):
3657 if request .method == "GET" and "get" in self .http_method_names :
3758 return self .ajax_get (request , * args , ** kwargs )
@@ -63,6 +84,32 @@ def plugin_instance(pk):
6384 return plugin , instance
6485
6586 def ajax_post (self , request , * args , ** kwargs ):
87+ """
88+ Handles AJAX POST requests for the form builder.
89+
90+ This method processes AJAX POST requests by determining the appropriate
91+ plugin or form instance to handle the request based on the provided
92+ keyword arguments.
93+
94+ Args:
95+ request (HttpRequest): The HTTP request object.
96+ \*args: Additional positional arguments.
97+ \*\*kwargs: Additional keyword arguments, which may include:
98+ - instance_id (int): The ID of the plugin instance.
99+ - parameter (str): Optional parameter for decoding.
100+ - form_id (str): The ID of the form instance.
101+
102+ Returns:
103+ JsonResponse: A JSON response with the result of the AJAX POST request.
104+ Http404: If the plugin or form instance cannot be found or does not
105+ support AJAX POST requests.
106+
107+ Raises:
108+ Http404: If the plugin or form instance cannot be found or does not
109+ support AJAX POST requests.
110+ ValidationError: If there is a validation error during the request
111+ processing.
112+ """
66113 if "instance_id" in kwargs :
67114 plugin , instance = self .plugin_instance (kwargs ["instance_id" ])
68115 if hasattr (plugin , "ajax_post" ):
@@ -90,6 +137,30 @@ def ajax_post(self, request, *args, **kwargs):
90137 raise Http404 ()
91138
92139 def ajax_get (self , request , * args , ** kwargs ):
140+ """
141+ Handles AJAX GET requests.
142+
143+ This method processes AJAX GET requests by checking for specific keys in the
144+ `kwargs` and delegating the request to the appropriate handler.
145+
146+ Args:
147+ request (HttpRequest): The HTTP request object.
148+ \*args: Additional positional arguments.
149+ \*\*kwargs: Additional keyword arguments.
150+
151+ Returns:
152+ JsonResponse: A JSON response with the result of the AJAX request.
153+ Http404: If the required keys are not found in `kwargs` or the handler is not available.
154+
155+ Raises:
156+ ValidationError: If there is an error during the processing of the AJAX request.
157+
158+ Notes:
159+ - If "instance_id" is present in `kwargs`, it attempts to retrieve the plugin instance
160+ and calls its `ajax_get` method if available.
161+ - If "form_id" is present in `kwargs`, it attempts to retrieve the form instance from
162+ the `_formview_pool` and calls its `ajax_get` or `get` method if available.
163+ """
93164 if "instance_id" in kwargs :
94165 plugin , instance = self .plugin_instance (kwargs ["instance_id" ])
95166 if hasattr (plugin , "ajax_get" ):
0 commit comments