Пример #1
0
def get_row_values(
    action: models.Action,
    row_idx: Union[int, Tuple[str, str]],
) -> Dict[str, Union[str, int, float, datetime]]:
    """Get the values in a row either by index or by key.

    Given an action and a row index, obtain the appropriate row of values
    from the data frame.

    :param action: Action object
    :param row_idx: Row index to use for evaluation
    :return Dictionary with the data row
    """
    # Step 1: Get the row of data from the DB
    filter_formula = action.get_filter_formula()

    # If row_idx is an integer, get the data by index, otherwise, by key
    if isinstance(row_idx, int):
        row = pandas.get_table_row_by_index(
            action.workflow,
            filter_formula,
            row_idx,
        )
    else:

        row = sql.get_row(
            action.workflow.get_data_frame_table_name(),
            row_idx[0],
            row_idx[1],
            column_names=action.workflow.get_column_names(),
            filter_formula=filter_formula,
        )
    return row
Пример #2
0
    def test_stats(self):
        """Test the use of forms in to schedule actions."""
        # Remove is_key from column 'age'
        col = self.workflow.columns.get(name='age')
        col.is_key = False
        col.save()

        # Get the visualization for the whole table
        resp = self.get_response('table:stat_table')
        self.assertTrue(status.is_success(resp.status_code))

        # GEt the visualization of the view
        view = self.workflow.views.get(name='simple view')
        resp = self.get_response('table:stat_table_view', {'pk': view.id})
        self.assertTrue(status.is_success(resp.status_code))

        # Get one of the rows
        r_val = get_table_row_by_index(self.workflow, None, 1)
        resp = self.get_response('table:stat_table',
                                 req_params={
                                     'key': 'email',
                                     'val': r_val['email']
                                 })
        self.assertTrue(status.is_success(resp.status_code))

        # Get one of the rows from one of the views
        resp = self.get_response('table:stat_table_view', {'pk': view.id},
                                 req_params={
                                     'key': 'email',
                                     'val': r_val['email']
                                 })
        self.assertTrue(status.is_success(resp.status_code))

        # Get one of the columns
        col = self.workflow.columns.get(name='age')
        # Get the column visualization
        resp = self.get_response('table:stat_column', {'pk': col.id})
        self.assertTrue(status.is_success(resp.status_code))

        # Get the JSON column visualization for a modal
        col = self.workflow.columns.get(name='one')
        resp = self.get_response('table:stat_column_JSON', {'pk': col.id},
                                 is_ajax=True)
        self.assertTrue(status.is_success(resp.status_code))
    def test_display(self):
        """Test the use of forms in to schedule actions."""
        # Remove is_key from column 'age'
        col = self.workflow.columns.get(name='age')
        col.is_key = False
        col.save()

        # Store the number of rows
        nrows = self.workflow.nrows

        # Get the visualization for the whole table
        resp = self.get_response('table:display')
        self.assertTrue(status.is_success(resp.status_code))

        # Get the JSON subset of the table display
        resp = self.get_response(
            'table:display_ss',
            method='POST',
            req_params={
                'draw': '1',
                'start': '0',
                'length': '10',
                'order[0][column]': '1',
                'order[0][dir]': 'asc',
                'search[value]': ''},
            is_ajax=True)
        self.assertTrue(status.is_success(resp.status_code))

        # GEt the display of the view
        view = self.workflow.views.get(name='simple view')
        resp = self.get_response('table:display_view', {'pk': view.id})
        self.assertTrue(status.is_success(resp.status_code))

        # Get the JSON subset of the view display
        resp = self.get_response(
            'table:display_view_ss',
            {'pk': view.id},
            method='POST',
            req_params={
                'draw': '1',
                'start': '0',
                'length': '10',
                'order[0][column]': '1',
                'order[0][dir]': 'asc',
                'search[value]': ''}, is_ajax=True)
        self.assertTrue(status.is_success(resp.status_code))

        # Delete one row of the table
        r_val = get_table_row_by_index(self.workflow, None, 1)
        resp = self.get_response(
            'table:row_delete',
            req_params={
                'key': 'email',
                'val': r_val['email']},
            is_ajax=True)
        self.assertTrue(status.is_success(resp.status_code))

        req = self.factory.get(
            reverse('table:row_delete'),
            {'key': 'email', 'value': r_val['email']})

        # The POST request uses the params in the GET URL
        get_url = req.get_full_path()
        req = self.factory.post(
            get_url,
            {},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        req = self.add_middleware(req)
        resp = row_delete(req)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.nrows, nrows - 1)

        # Incorrect post
        resp = self.get_response(
            'table:row_delete',
            method='POST',
            req_params={
                'key': 'email',
                'val': r_val['email']},
            is_ajax=True)
        self.assertTrue(status.is_success(resp.status_code))