mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	feat: add action to remove all pending tasks (#9600)
This commit is contained in:
		@@ -422,10 +422,11 @@ class BulkOperationMixin:
 | 
			
		||||
 | 
			
		||||
        items = request.data.pop('items', None)
 | 
			
		||||
        filters = request.data.pop('filters', None)
 | 
			
		||||
        all_filter = request.GET.get('all', None)
 | 
			
		||||
 | 
			
		||||
        queryset = model.objects.all()
 | 
			
		||||
 | 
			
		||||
        if not items and not filters:
 | 
			
		||||
        if not items and not filters and all_filter is None:
 | 
			
		||||
            raise ValidationError({
 | 
			
		||||
                'non_field_errors': _(
 | 
			
		||||
                    'List of items or filters must be provided for bulk operation'
 | 
			
		||||
@@ -459,6 +460,11 @@ class BulkOperationMixin:
 | 
			
		||||
                    'non_field_errors': _('Invalid filters provided')
 | 
			
		||||
                })
 | 
			
		||||
 | 
			
		||||
        if all_filter and not helpers.str2bool(all_filter):
 | 
			
		||||
            raise ValidationError({
 | 
			
		||||
                'non_field_errors': _('All filter must only be used with true')
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
        if queryset.count() == 0:
 | 
			
		||||
            raise ValidationError({
 | 
			
		||||
                'non_field_errors': _('No items match the provided criteria')
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,11 @@ import { useMemo } from 'react';
 | 
			
		||||
 | 
			
		||||
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
 | 
			
		||||
import { apiUrl } from '@lib/functions/Api';
 | 
			
		||||
import { UserRoles } from '@lib/index';
 | 
			
		||||
import { notifications, showNotification } from '@mantine/notifications';
 | 
			
		||||
import { IconTrashXFilled, IconX } from '@tabler/icons-react';
 | 
			
		||||
import { api } from '../../App';
 | 
			
		||||
import { ActionButton } from '../../components/buttons/ActionButton';
 | 
			
		||||
import { useTable } from '../../hooks/UseTable';
 | 
			
		||||
import { useUserState } from '../../states/UserState';
 | 
			
		||||
import type { TableColumn } from '../Column';
 | 
			
		||||
@@ -48,6 +53,41 @@ export default function PendingTasksTable({
 | 
			
		||||
    ];
 | 
			
		||||
  }, []);
 | 
			
		||||
 | 
			
		||||
  const tableActions = useMemo(() => {
 | 
			
		||||
    return [
 | 
			
		||||
      <ActionButton
 | 
			
		||||
        key='remove-all'
 | 
			
		||||
        icon={<IconTrashXFilled />}
 | 
			
		||||
        tooltip={t`Remove all pending tasks`}
 | 
			
		||||
        onClick={() => {
 | 
			
		||||
          api
 | 
			
		||||
            .delete(`${apiUrl(ApiEndpoints.task_pending_list)}?all=true`)
 | 
			
		||||
            .then(() => {
 | 
			
		||||
              notifications.show({
 | 
			
		||||
                id: 'notes',
 | 
			
		||||
                title: t`Success`,
 | 
			
		||||
                message: t`All pending tasks deleted`,
 | 
			
		||||
                color: 'green'
 | 
			
		||||
              });
 | 
			
		||||
              table.refreshTable();
 | 
			
		||||
            })
 | 
			
		||||
            .catch((err) => {
 | 
			
		||||
              showNotification({
 | 
			
		||||
                title: t`Error while deleting all pending tasks`,
 | 
			
		||||
                message:
 | 
			
		||||
                  err.response.data?.non_field_errors ??
 | 
			
		||||
                  err.message ??
 | 
			
		||||
                  t`Unknown error`,
 | 
			
		||||
                color: 'red',
 | 
			
		||||
                icon: <IconX />
 | 
			
		||||
              });
 | 
			
		||||
            });
 | 
			
		||||
        }}
 | 
			
		||||
        hidden={!user.hasAddRole(UserRoles.admin)}
 | 
			
		||||
      />
 | 
			
		||||
    ];
 | 
			
		||||
  }, [user]);
 | 
			
		||||
 | 
			
		||||
  return (
 | 
			
		||||
    <InvenTreeTable
 | 
			
		||||
      url={apiUrl(ApiEndpoints.task_pending_list)}
 | 
			
		||||
@@ -56,7 +96,8 @@ export default function PendingTasksTable({
 | 
			
		||||
      props={{
 | 
			
		||||
        afterBulkDelete: onRecordsUpdated,
 | 
			
		||||
        enableBulkDelete: user.isStaff(),
 | 
			
		||||
        enableSelection: true
 | 
			
		||||
        enableSelection: true,
 | 
			
		||||
        tableActions: tableActions
 | 
			
		||||
      }}
 | 
			
		||||
    />
 | 
			
		||||
  );
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user