def edit_action_out( request: HttpRequest, workflow: Workflow, action: Action, ) -> HttpResponse: """Edit action out. :param request: Request object :param workflow: The workflow with the action :param action: Action :return: HTML response """ # Create the form form = EditActionOutForm(request.POST or None, instance=action) form_filter = FilterForm( request.POST or None, instance=action.get_filter(), action=action ) # Processing the request after receiving the text from the editor if request.method == 'POST' and form.is_valid() and form_filter.is_valid(): # Get content text_content = form.cleaned_data.get('text_content') # Render the content as a template and catch potential problems. if text_renders_correctly(text_content, action, form): # Log the event Log.objects.register( request.user, Log.ACTION_UPDATE, action.workflow, {'id': action.id, 'name': action.name, 'workflow_id': workflow.id, 'workflow_name': workflow.name, 'content': text_content}) # Text is good. Update the content of the action action.set_text_content(text_content) if action.action_type == Action.personalized_json: # Update the target_url field action.target_url = form.cleaned_data['target_url'] action.save() if request.POST['Submit'] == 'Submit': return redirect(request.get_full_path()) return redirect('action:index') # This is a GET request or a faulty POST request # Get the filter or None filter_condition = action.get_filter() # Context to render the form context = { 'filter_condition': filter_condition, 'action': action, 'load_summernote': action.action_type == Action.personalized_text, 'conditions': action.conditions.filter(is_filter=False), 'other_conditions': Condition.objects.filter( action__workflow=workflow, is_filter=False, ).exclude(action=action), 'query_builder_ops': workflow.get_query_builder_ops_as_str(), 'attribute_names': [ attr for attr in list(workflow.attributes.keys()) ], 'columns': workflow.columns.all(), 'stat_columns': workflow.columns.filter(is_key=False), 'selected_rows': filter_condition.n_rows_selected if filter_condition else -1, 'has_data': action.workflow.has_table(), 'all_false_conditions': any( cond.n_rows_selected == 0 for cond in action.conditions.all()), 'rows_all_false': action.get_row_all_false_count(), 'total_rows': workflow.nrows, 'form': form, 'form_filter': form_filter, 'vis_scripts': PlotlyHandler.get_engine_scripts(), } # Return the same form in the same page return render(request, 'action/edit_out.html', context=context)
def save_condition_form( request: HttpRequest, form, template_name: str, action: Action, is_filter: Optional[bool] = False, ) -> JsonResponse: """ Process the AJAX form to create and update conditions and filters. :param request: HTTP request :param form: Form being used to ask for the fields :param template_name: Template being used to render the form :param action: The action to which the condition is attached to :param is_filter: The condition is a filter :return: JSON response """ if request.method == 'POST' and form.is_valid(): if not form.has_changed(): return JsonResponse({'html_redirect': None}) if is_filter and form.instance.id is None and action.get_filter(): # Should not happen. Go back to editing the action return JsonResponse({'html_redirect': ''}) is_new = form.instance.id is None # Update fields and save the condition condition = form.save(commit=False) condition.action = action condition.is_filter = is_filter condition.save() condition.columns.set( action.workflow.columns.filter(name__in=get_variables( condition.formula), )) # If the request has the 'action_content' field, update the action action_content = request.POST.get('action_content') if action_content: action.set_text_content(action_content) propagate_changes(condition, form.changed_data, form.old_name, is_new) # Store the type of event to log if is_new: if is_filter: log_type = Log.FILTER_CREATE else: log_type = Log.CONDITION_CREATE else: if is_filter: log_type = Log.FILTER_UPDATE else: log_type = Log.CONDITION_UPDATE # Log the event Log.objects.register( request.user, log_type, action.workflow, { 'id': condition.id, 'name': condition.name, 'selected_rows': condition.n_rows_selected, 'formula': evaluate_formula(condition.formula, EVAL_TXT), }) return JsonResponse({'html_redirect': ''}) # GET request or invalid form return JsonResponse({ 'html_form': render_to_string(template_name, { 'form': form, 'action_id': action.id, 'condition': form.instance }, request=request), })