def log_table_server_side( request: http.HttpRequest, workflow: models.Workflow ) -> Dict: """Create the server side response for the table of logs. :param request: Http Request :param workflow: Workflow being considered. :return: """ dt_page = DataTablesServerSidePaging(request) if not dt_page.is_valid: return {'error': _('Incorrect request. Unable to process')} # Get the logs qs = workflow.logs records_total = qs.count() if dt_page.search_value: # Refine the log qs = qs.filter( Q(id__icontains=dt_page.search_value) | Q(user__email__icontains=dt_page.search_value) | Q(name__icontains=dt_page.search_value) | Q(payload__icontains=dt_page.search_value), workflow__id=workflow.id, ).distinct() # Order and select values qs = qs.order_by(F('created').desc()).values_list( 'id', 'name', 'created', 'user__email') records_filtered = qs.count() final_qs = [] for log_item in qs[dt_page.start:dt_page.start + dt_page.length]: row = [ '<button type="button" data-url="{0}"'.format( reverse('logs:modal_view', kwargs={'pk': log_item[0]}), ) + ' class="btn btn-sm btn-light js-log-view"' + ' data-toggle="tooltip" title="{0}">{1}</button>'.format( ugettext('View log content'), log_item[0], ), models.Log.LOG_TYPES[log_item[1]], simplify_datetime_str(log_item[2]), log_item[3], ] # Add the row to the final query_set final_qs.append(row) return { 'draw': dt_page.draw, 'recordsTotal': records_total, 'recordsFiltered': records_filtered, 'data': final_qs}
def display_ss( request: HttpRequest, workflow: Optional[Workflow] = None, ) -> JsonResponse: """Return the subset of logs to include in a table page.""" # Try to get workflow and if not present, go to home page # Check that the GET parameter are correctly given dt_page = DataTablesServerSidePaging(request) if not dt_page.is_valid: return JsonResponse( {'error': _('Incorrect request. Unable to process')}, ) # Get the logs qs = workflow.logs records_total = qs.count() if dt_page.search_value: # Refine the log qs = qs.filter( Q(id__icontains=dt_page.search_value) | Q(user__email__icontains=dt_page.search_value) | Q(name__icontains=dt_page.search_value) | Q(payload__icontains=dt_page.search_value), workflow__id=workflow.id, ).distinct() # Order and select values qs = qs.order_by(F('created').desc()).values_list( 'id', 'created', 'user__email', 'name', ) records_filtered = qs.count() final_qs = [] for log_item in qs[dt_page.start:dt_page.start + dt_page.length]: row = [ '<a href="{0}" class="spin"'.format( reverse('logs:view', kwargs={'pk': log_item[0]}), ) + ' data-toggle="tooltip" title="{0}">{1}</a>'.format( ugettext('View log content'), log_item[0], ), simplify_datetime_str(log_item[1]), log_item[2], log_item[3], ] # Add the row to the final query_set final_qs.append(row) # Render the page with the table return JsonResponse({ 'draw': dt_page.draw, 'recordsTotal': records_total, 'recordsFiltered': records_filtered, 'data': final_qs, })
def run_survey_ss( request: HttpRequest, pk: int, workflow: Optional[Workflow] = None, action: Optional[Action] = None, ) -> JsonResponse: """Show elements in table that satisfy filter request. Serve the AJAX requests to show the elements in the table that satisfy the filter and between the given limits. :param request: :param pk: action id being run :return: """ # Check that the GET parameter are correctly given dt_page = DataTablesServerSidePaging(request) if not dt_page.is_valid: return JsonResponse( {'error': _('Incorrect request. Unable to process')}, ) # Get columns and the position of the first key columns = [ccpair.column for ccpair in action.column_condition_pair.all()] key_idx = next(idx for idx, col in enumerate(columns) if col.is_key) query_set = _create_initial_qs( workflow.get_data_frame_table_name(), action.get_filter_formula(), columns, dt_page, ) filtered = len(query_set) # Get the subset of the qs to show in the table query_set = _create_table_qsdata( action.id, query_set, dt_page, columns, key_idx, ) return JsonResponse({ 'draw': dt_page.draw, 'recordsTotal': workflow.nrows, 'recordsFiltered': filtered, 'data': query_set, })
def column_ss( request: HttpRequest, workflow: Optional[models.Workflow] = None, ) -> JsonResponse: """Render the server side page for the table of columns. Given the workflow id and the request, return to DataTable the proper list of columns to be rendered. :param request: Http request received from DataTable :param workflow: Workflow being manipulated. :return: Data to visualize in the table """ # Check that the GET parameter are correctly given dt_page = DataTablesServerSidePaging(request) if not dt_page.is_valid: return JsonResponse( {'error': _('Incorrect request. Unable to process')}, ) return JsonResponse(services.column_table_server_side(dt_page, workflow))
def show_survey_table_ss( request: http.HttpRequest, pk: int, workflow: Optional[models.Workflow] = None, action: Optional[models.Action] = None, ) -> http.JsonResponse: """Show elements in table that satisfy filter request. Serve the AJAX requests to show the elements in the table that satisfy the filter and between the given limits. :param request: :param pk: action id being run :param workflow: Workflow being manipulated (set by the decorator) :param action: Action being manipulated (set by the decorator) :return: """ del pk # Check that the GET parameters are correctly given dt_page = DataTablesServerSidePaging(request) if not dt_page.is_valid: return http.JsonResponse( {'error': _('Incorrect request. Unable to process')}, ) return services.create_survey_table(workflow, action, dt_page)
def column_ss( request: HttpRequest, workflow: Optional[Workflow] = None, ) -> JsonResponse: """Render the server side page for the table of columns. Given the workflow id and the request, return to DataTable the proper list of columns to be rendered. :param request: Http request received from DataTable :return: Data to visualize in the table """ # Check that the GET parameter are correctly given dt_page = DataTablesServerSidePaging(request) if not dt_page.is_valid: return JsonResponse( {'error': _('Incorrect request. Unable to process')}, ) # Get the initial set qs = workflow.columns.all() records_total = qs.count() records_filtered = records_total # Reorder if required if dt_page.order_col: col_name = [ 'position', 'name', 'description_text', 'data_type', 'is_key' ][dt_page.order_col] if dt_page.order_dir == 'desc': col_name = '-' + col_name qs = qs.order_by(col_name) if dt_page.search_value: qs = qs.filter( Q(name__icontains=dt_page.search_value) | Q(data_type__icontains=dt_page.search_value)) records_filtered = qs.count() # Creating the result final_qs = [] for col in qs[dt_page.start:dt_page.start + dt_page.length]: ops_string = render_to_string( 'workflow/includes/workflow_column_operations.html', { 'id': col.id, 'is_key': col.is_key }, ) final_qs.append({ 'number': col.position, 'name': format_html( '<a href="#" class="js-workflow-column-edit"' + 'data-toggle="tooltip" data-url="{0}"' + 'title="{1}">{2}</a>', reverse('workflow:column_edit', kwargs={'pk': col.id}), _('Edit the parameters of this column'), col.name, ), 'description': col.description_text, 'type': col.get_simplified_data_type(), 'key': '<span class="true">✔</span>' if col.is_key else '', 'operations': ops_string, }) if len(final_qs) == dt_page.length: break return JsonResponse({ 'draw': dt_page.draw, 'recordsTotal': records_total, 'recordsFiltered': records_filtered, 'data': final_qs, })
def render_table_display_server_side( request, workflow, columns, formula, view_id=None, ) -> http.JsonResponse: """Render the appropriate subset of the data table. Use the search string provided in the UI + the filter (if applicable) taken from a view. :param request: AJAX request :param workflow: workflow object :param columns: Subset of columns to consider :param formula: Expression to filter rows :param view_id: ID of the view restricting the display (if any) :return: JSON response """ # Check that the GET parameter are correctly given dt_page = DataTablesServerSidePaging(request) if not dt_page.is_valid: return http.JsonResponse( {'error': _('Incorrect request. Unable to process')}, ) # Get columns and names column_names = [col.name for col in columns] # See if an order has been given. order_col_name = None if dt_page.order_col: # The first column is ops order_col_name = column_names[dt_page.order_col - 1] qs = sql.search_table( workflow.get_data_frame_table_name(), dt_page.search_value, columns_to_search=column_names, filter_formula=formula, order_col_name=order_col_name, order_asc=dt_page.order_dir == 'asc', ) # Find the first key column key_name, key_idx = next( ((col.name, idx) for idx, col in enumerate(columns) if col.is_key), None) key_name = escape(key_name) # Post processing + adding operation columns and performing the search final_qs = [] nitems = 0 # For counting the number of elements in the result for row in qs[dt_page.start:dt_page.start + dt_page.length]: nitems += 1 new_element = {} if view_id: stat_url = reverse('table:stat_table_view', kwargs={'pk': view_id}) else: stat_url = reverse('table:stat_table') # Transform key name and key value into escaped strings key_value = escape(row[key_idx]) ops_string = render_to_string( 'table/includes/partial_row_ops.html', { 'stat_url': stat_url + '?{0}'.format(urlencode({ 'key': key_name, 'val': key_value }, )), 'edit_url': reverse('dataops:rowupdate') + '?{0}'.format(urlencode({ 'k': key_name, 'v': key_value }, )), 'delete_key': '?{0}'.format( urlencode({ 'key': key_name, 'value': key_value }, )), 'view_id': view_id, }, ) # Element to add to the final queryset new_element['Operations'] = ops_string column_values = [ rval.astimezone(timezone( settings.TIME_ZONE, )).strftime('%Y-%m-%d %H:%M:%S %z') if isinstance(rval, datetime) else rval for rval in list(row) ] new_element.update(zip(column_names, column_values)) # Create the list of elements to display and add it ot the final QS final_qs.append(new_element) if nitems == dt_page.length: # We reached the number or requested elements, abandon. break return http.JsonResponse({ 'draw': dt_page.draw, 'recordsTotal': workflow.nrows, 'recordsFiltered': len(qs), 'data': final_qs, })