コード例 #1
0
def delete(
    request: http.HttpRequest,
    pk: int,
    workflow: Optional[models.Workflow] = None,
    column: Optional[models.Column] = None,
) -> http.JsonResponse:
    """Delete a column in the table attached to a workflow.

    :param request: HTTP request
    :param pk: ID of the column to delete.
    :param workflow: Workflow being processed
    :param column: Column to delete (set by the decorator)
    :return: Render the delete column form
    """
    # Get the workflow element
    # If the columns is unique and it is the only one, we cannot allow
    # the operation
    unique_column = workflow.get_column_unique()
    if column.is_key and len([col for col in unique_column if col]) == 1:
        # This is the only key column
        messages.error(request, _('You cannot delete the only key column'))
        return http.JsonResponse({'html_redirect': reverse('column:index')})

    # Get the name of the column to delete
    context = {'pk': pk, 'cname': column.name}

    # Get the conditions/actions attached to this workflow
    cond_to_delete = [
        col
        for col in models.Condition.objects.filter(action__workflow=workflow)
        if evaluation.has_variable(col.formula, column.name)
    ]
    # Put it in the context because it is shown to the user before confirming
    # the deletion
    context['cond_to_delete'] = cond_to_delete

    if request.method == 'POST':
        services.delete_column(request.user, workflow, column, cond_to_delete)

        # There are various points of return
        from_url = request.META['HTTP_REFERER']
        if from_url.endswith(reverse('table:display')):
            return http.JsonResponse(
                {'html_redirect': reverse('table:display')})

        return http.JsonResponse({'html_redirect': reverse('column:index')})

    return http.JsonResponse({
        'html_form':
        render_to_string('column/includes/partial_delete.html',
                         context,
                         request=request),
    })
コード例 #2
0
    def test_create_random_column_datetime_form(self):
        """Create a random number column with no values"""
        # Get the workflow first
        self.workflow = models.Workflow.objects.all().first()

        # Column name and current number of them
        cname = 'random_column'
        ncols = self.workflow.ncols

        # JSON POST request for column creation with incorrect string value
        resp = self.get_response('column:random_column_add',
                                 method='POST',
                                 req_params={
                                     'name': cname,
                                     'data_type': 'datetime',
                                     'raw_categories': 'bogus',
                                     'position': 0
                                 },
                                 is_ajax=True)

        resp_content = json.loads(resp.content)
        self.assertTrue('html_form' in resp_content)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.ncols, ncols)
        new_column = self.workflow.columns.filter(name=cname).first()
        self.assertIsNone(new_column)

        # JSON POST request for column creation with a single integer
        resp = self.get_response('column:random_column_add',
                                 method='POST',
                                 req_params={
                                     'name': cname,
                                     'data_type': 'datetime',
                                     'raw_categories':
                                     '2020-09-11 12:04:43+0930',
                                     'position': 0
                                 },
                                 is_ajax=True)

        resp_content = json.loads(resp.content)
        self.assertTrue('html_form' in resp_content)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.ncols, ncols)
        new_column = self.workflow.columns.filter(name=cname).first()
        self.assertIsNone(new_column)

        # JSON POST request for column creation with a multiple strings
        dtimes = [
            parse_datetime('2020-09-11 12:04:43+0930'),
            parse_datetime('2020-09-12 12:04:43+0930')
        ]
        resp = self.get_response(
            'column:random_column_add',
            method='POST',
            req_params={
                'name': cname,
                'data_type': 'datetime',
                'raw_categories':
                '2020-09-11 12:04:43+0930, 2020-09-12 12:04:43+0930',
                'position': 0
            },
            is_ajax=True)

        resp_content = json.loads(resp.content)
        self.assertTrue('html_form' not in resp_content)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.ncols, ncols + 1)
        new_column = self.workflow.columns.filter(name=cname).first()
        self.assertIsNotNone(new_column)
        self.assertEqual(new_column.name, cname)
        self.assertEqual(new_column.data_type, 'datetime')
        data_frame = pandas.load_table(
            self.workflow.get_data_frame_table_name())
        self.assertTrue(all(element in dtimes
                            for element in data_frame[cname]))
        # Delete the column
        services.delete_column(self.workflow.user, self.workflow, new_column)
コード例 #3
0
    def test_create_random_column_number_form(self):
        """Create a random number column with no values"""
        # Get the workflow first
        self.workflow = models.Workflow.objects.all().first()

        # Column name and current number of them
        cname = 'random_column'
        ncols = self.workflow.ncols

        # JSON POST request for column creation with string value
        resp = self.get_response('column:random_column_add',
                                 method='POST',
                                 req_params={
                                     'name': cname,
                                     'data_type': 'double',
                                     'raw_categories': 'bogus',
                                     'position': 0
                                 },
                                 is_ajax=True)

        resp_content = json.loads(resp.content)
        self.assertTrue('html_form' in resp_content)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.ncols, ncols)
        new_column = self.workflow.columns.filter(name=cname).first()
        self.assertIsNone(new_column)

        # JSON POST request for column creation with a single integer
        resp = self.get_response('column:random_column_add',
                                 method='POST',
                                 req_params={
                                     'name': cname,
                                     'data_type': 'double',
                                     'raw_categories': '13.0',
                                     'position': 0
                                 },
                                 is_ajax=True)

        resp_content = json.loads(resp.content)
        self.assertTrue('html_form' in resp_content)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.ncols, ncols)
        new_column = self.workflow.columns.filter(name=cname).first()
        self.assertIsNone(new_column)

        # JSON POST request for column creation with a multiple strings
        resp = self.get_response('column:random_column_add',
                                 method='POST',
                                 req_params={
                                     'name': cname,
                                     'data_type': 'double',
                                     'raw_categories': 'one, two, three',
                                     'position': 0
                                 },
                                 is_ajax=True)

        resp_content = json.loads(resp.content)
        self.assertTrue('html_form' in resp_content)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.ncols, ncols)
        new_column = self.workflow.columns.filter(name=cname).first()
        self.assertIsNone(new_column)

        # JSON POST request for column creation with a interval integer
        resp = self.get_response('column:random_column_add',
                                 method='POST',
                                 req_params={
                                     'name': cname,
                                     'data_type': 'double',
                                     'raw_categories': '-3.0 - -5.0',
                                     'position': 0
                                 },
                                 is_ajax=True)

        resp_content = json.loads(resp.content)
        self.assertTrue('html_form' not in resp_content)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.ncols, ncols + 1)
        new_column = self.workflow.columns.filter(name=cname).first()
        self.assertIsNotNone(new_column)
        self.assertEqual(new_column.name, cname)
        self.assertEqual(new_column.data_type, 'double')
        data_frame = pandas.load_table(
            self.workflow.get_data_frame_table_name())
        self.assertTrue(
            all(element in [-3, -4, -5] for element in data_frame[cname]))
        # Delete the column
        services.delete_column(self.workflow.user, self.workflow, new_column)

        # JSON POST request for column creation with an integer list
        resp = self.get_response('column:random_column_add',
                                 method='POST',
                                 req_params={
                                     'name': cname,
                                     'data_type': 'double',
                                     'raw_categories': '17, 18, 19',
                                     'position': 0
                                 },
                                 is_ajax=True)

        resp_content = json.loads(resp.content)
        self.assertTrue('html_form' not in resp_content)
        self.assertTrue(status.is_success(resp.status_code))
        self.workflow.refresh_from_db()
        self.assertEqual(self.workflow.ncols, ncols + 1)
        new_column = self.workflow.columns.filter(name=cname).first()
        self.assertIsNotNone(new_column)
        self.assertEqual(new_column.name, cname)
        self.assertEqual(new_column.data_type, 'double')
        data_frame = pandas.load_table(
            self.workflow.get_data_frame_table_name())
        self.assertTrue(
            all(element in [17, 18, 19] for element in data_frame[cname]))