def _render_table_display_page( request: HttpRequest, workflow: Workflow, view: Optional[View], columns, ajax_url: str, ): """Render content of the display page. Function to render the content of the display page taking into account the columns to show and the AJAX url to use to render the table content. :param request: HTTP request :param workflow: Workflow object used to access the data frame :param view: View to use to render the table (or None) :param columns: Columns to display in the page :param ajax_url: URL to invoke to populate the table :return: HTTP Response """ # Create the initial context context = { 'workflow': workflow, 'query_builder_ops': workflow.get_query_builder_ops_as_str(), 'ajax_url': ajax_url, 'views': workflow.views.all(), 'no_actions': workflow.actions.count() == 0, 'vis_scripts': PlotlyHandler.get_engine_scripts(), } # If there is a DF, add the columns if workflow.has_table(): context['columns'] = columns context['column_types'] = str([''] + [ col.data_type for col in columns]) context['columns_datatables'] = [{'data': 'Operations'}] + [ {'data': col.name.replace('.', '\\.')} for col in columns] context['stat_columns'] = workflow.columns.filter(is_key=False) else: context['columns'] = None context['columns_datatables'] = [] # If using a view, add it to the context if view: context['view'] = view return render(request, 'table/display.html', context)
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 edit_action_out(request, pk): """ View to handle the AJAX form to edit an action (editor, conditions, filters, etc). :param request: Request object :param pk: Action PK :return: JSON response """ # Try to get the workflow first workflow = get_workflow(request) if not workflow: return redirect('workflow:index') # Get the action and create the form try: action = Action.objects.filter( Q(workflow__user=request.user) | Q(workflow__shared=request.user)).distinct().get(pk=pk) except ObjectDoesNotExist: return redirect('action:index') # Create the form form = EditActionOutForm(request.POST or None, instance=action) # See if the action has a filter or not try: filter_condition = Condition.objects.get( action=action, is_filter=True ) except Condition.DoesNotExist: filter_condition = None except Condition.MultipleObjectsReturned: return render(request, 'error.html', {'message': 'Malfunction detected when retrieving filter ' '(action: {0})'.format(action.id)}) # Conditions to show in the page as well. conditions = Condition.objects.filter( action=action, is_filter=False ).order_by('created').values('id', 'name') # Boolean to find out if there is a table attached to this workflow has_data = ops.workflow_has_table(action.workflow) # Get the total number of rows in DF and those selected by filter. total_rows = workflow.nrows if filter_condition: action.n_selected_rows = \ pandas_db.num_rows(action.workflow.id, filter_condition.formula) # Context to render the form context = {'filter_condition': filter_condition, 'action': action, 'conditions': conditions, 'query_builder_ops': workflow.get_query_builder_ops_as_str(), 'attribute_names': [x for x in workflow.attributes.keys()], 'column_names': workflow.get_column_names(), 'selected_rows': action.n_selected_rows, 'has_data': has_data, 'total_rows': total_rows, 'form': form, 'vis_scripts': PlotlyHandler.get_engine_scripts() } # Processing the request after receiving the text from the editor if request.method == 'POST': # Get the next step next_step = request.POST['Submit'] if form.is_valid(): content = form.cleaned_data.get('content', None) # TODO: Can we detect unused vars only for this invocation? # Render the content as a template and catch potential problems. # This seems to be only possible if dealing directly with Jinja2 # instead of Django. try: render_template(content, {}, action) except Exception as e: # Pass the django exception to the form (fingers crossed) form.add_error(None, e.message) return render(request, 'action/edit_out.html', context) # Log the event logs.ops.put(request.user, 'action_update', action.workflow, {'id': action.id, 'name': action.name, 'workflow_id': workflow.id, 'workflow_name': workflow.name, 'content': content}) # Text is good. Update the content of the action action.content = content action.save() # Closing if next_step == 'Save-and-close': return redirect('action:index') # Return the same form in the same page return render(request, 'action/edit_out.html', context=context)
def edit_action_out(request, workflow, action): """ View to handle the form to edit an action OUT (editor, conditions, filters, etc). :param request: Request object :param action: Action :return: HTML response """ # Create the form form = EditActionOutForm(request.POST or None, instance=action) # Get the filter or None filter_condition = action.get_filter() # Conditions to show in the page. conditions = Condition.objects.filter(action=action, is_filter=False).order_by('created') # Context to render the form context = { 'filter_condition': filter_condition, 'action': action, 'conditions': conditions, 'query_builder_ops': workflow.get_query_builder_ops_as_str(), 'attribute_names': [x for x in workflow.attributes.keys()], 'column_names': workflow.get_column_names(), 'selected_rows': filter_condition.n_rows_selected if filter_condition else -1, 'has_data': ops.workflow_has_table(action.workflow), 'total_rows': workflow.nrows, 'form': form, 'vis_scripts': PlotlyHandler.get_engine_scripts() } # Template to use template = 'action/md_edit_personalized_text.html' if action.action_type == Action.PERSONALIZED_JSON: template = 'action/edit_personalized_json.html' # Processing the request after receiving the text from the editor if request.method == 'GET' or not form.is_valid(): # Return the same form in the same page return render(request, template, context=context) # Get content content = form.cleaned_data.get('content', None) # Render the content as a template and catch potential problems. # This seems to be only possible if dealing directly with Jinja2 # instead of Django. try: render_template(content, {}, action) except Exception as e: # Pass the django exception to the form (fingers crossed) form.add_error(None, e.message) return render(request, template, context) # 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': content }) # Text is good. Update the content of the action action.set_content(content) # Update additional fields if action.action_type == Action.PERSONALIZED_JSON: action.target_url = form.cleaned_data['target_url'] action.save() return redirect('action:index')
def edit_action_in( request: HttpRequest, workflow: Workflow, action: Action, ) -> HttpResponse: """Edit an action in. :param request: Request object :param workflow: workflow :param action: Action :return: HTTP response """ # All tuples (action, column, condition) to consider tuples = action.column_condition_pair.all() # Columns all_columns = workflow.columns # Conditions filter_condition = action.get_filter() all_conditions = action.conditions.filter(is_filter=False) # Create the context info. context = { 'action': action, # Workflow elements 'total_rows': workflow.nrows, 'query_builder_ops': workflow.get_query_builder_ops_as_str(), 'has_data': workflow.has_table(), 'selected_rows': filter_condition.n_rows_selected if filter_condition else -1, 'all_false_conditions': any( cond.n_rows_selected == 0 for cond in all_conditions ), # Column elements 'key_columns': all_columns.filter(is_key=True), 'stat_columns': all_columns.filter(is_key=False), 'key_selected': tuples.filter(column__is_key=True).first(), 'has_no_key': tuples.filter(column__is_key=False).exists(), 'any_empty_description': tuples.filter( column__description_text='', column__is_key=False, ).exists(), 'columns_to_insert': all_columns.exclude( column_condition_pair__action=action, ).exclude( is_key=True, ).distinct().order_by('position'), 'column_selected_table': ColumnSelectedTable( tuples.filter(column__is_key=False).values( 'id', 'column__id', 'column__name', 'column__description_text', 'condition__name', ), orderable=False, extra_columns=[( 'operations', OperationsColumn( verbose_name='', template_file=ColumnSelectedTable.ops_template, template_context=lambda record: { 'id': record['column__id'], 'aid': action.id}), )], condition_list=all_conditions, ), # Conditions 'filter_condition': filter_condition, 'conditions': all_conditions, 'vis_scripts': PlotlyHandler.get_engine_scripts(), 'other_conditions': Condition.objects.filter( action__workflow=workflow, is_filter=False, ).exclude(action=action), } return render(request, 'action/edit_in.html', context)