Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • arkindex/backend
1 result
Show changes
Commits on Source (9)
0.14.3-beta1
0.14.3-beta2
......@@ -167,7 +167,7 @@ class DataImportsList(CorpusACLMixin, ListAPIView):
if state == State.Unscheduled:
# Handle a workflow with no task as unscheduled
state_query |= Q(workflow__tasks__isnull=True)
qs = qs.filter(state_query).exclude(id__in=excluded_imports.values('id'))
qs = qs.filter(state_query).exclude(id__in=excluded_imports.values('id')).distinct()
return qs.order_by('-last_task')
......@@ -966,4 +966,4 @@ class ListProcessElements(ListAPIView):
if dataimport.mode not in (DataImportMode.Elements, DataImportMode.Workers):
return Element.objects.none()
return self.retrieve_elements(dataimport).order_by('name')
return self.retrieve_elements(dataimport).order_by('id')
#!/usr/bin/env python3
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from arkindex.project.argparse import CorpusArgument
from arkindex.documents.models import ElementType
class Command(BaseCommand):
help = 'Merge an element type with another.'
def add_arguments(self, parser):
super().add_arguments(parser)
parser.add_argument(
'-c',
'--corpus',
help='ID of the corpus to find types on.',
type=CorpusArgument(),
required=True,
)
parser.add_argument(
'-s',
'--source',
help='Slug of the type that will be merged and removed.',
required=True,
)
parser.add_argument(
'-d',
'--destination',
help="Slug of the type that will receive the other type's elements.",
required=True,
)
@transaction.atomic
def handle(self, corpus, source, destination, verbosity=0, **options):
try:
source_type = corpus.types.get(slug=source)
except ElementType.DoesNotExist:
raise CommandError(f'Source type {source} not found in {corpus}')
try:
destination_type = corpus.types.get(slug=destination)
except ElementType.DoesNotExist:
raise CommandError(f'Destination type {destination} not found in {corpus}')
if source_type == destination_type:
raise CommandError('Source and destination types cannot be the same.')
affected = corpus.elements.filter(type=source_type).count()
self.stdout.write(f'Updating {affected} {source} elements to {destination}')
corpus.elements.filter(type=source_type).update(type=destination_type)
self.stdout.write(f'Removing type {source}')
source_type.delete()
self.stdout.write(self.style.SUCCESS(f'Successfully merged type {source} into {destination}.'))
self.stdout.write('Committing transaction…')
from django.core.management import call_command, CommandError
from arkindex.project.tests import FixtureTestCase
from arkindex.documents.models import ElementType
class TestMergeTypesCommand(FixtureTestCase):
def test_merge_types_missing_source(self):
with self.assertRaisesMessage(CommandError, 'Source type 404 not found in Unit Tests'):
call_command('merge_types', corpus=self.corpus, source='404', destination='page')
def test_merge_types_missing_destination(self):
with self.assertRaisesMessage(CommandError, 'Destination type 404 not found in Unit Tests'):
call_command('merge_types', corpus=self.corpus, source='page', destination='404')
def test_merge_types_distinct(self):
with self.assertRaisesMessage(CommandError, 'Source and destination types cannot be the same'):
call_command('merge_types', corpus=self.corpus, source='page', destination='page')
def test_merge_types(self):
page_type = self.corpus.types.get(slug='page')
surface_type = self.corpus.types.get(slug='surface')
self.assertEqual(self.corpus.elements.filter(type=page_type).count(), 6)
self.assertEqual(self.corpus.elements.filter(type=surface_type).count(), 6)
with self.assertNumQueries(11):
call_command('merge_types', corpus=self.corpus, source='surface', destination='page')
self.assertEqual(self.corpus.elements.filter(type=page_type).count(), 12)
self.assertFalse(self.corpus.elements.filter(type=surface_type).exists())
with self.assertRaises(ElementType.DoesNotExist):
surface_type.refresh_from_db()
boto3==1.9
boto3==1.15.16
cryptography>=3.1
Django==3.1.2
elasticsearch==6.8.1
......