Skip to content
Snippets Groups Projects
Commit 6df7aad8 authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'reorder-dedupe' into 'master'

Deduplicate zones in reorder_polygons

See merge request !606
parents 51934246 4d77485c
No related branches found
No related tags found
1 merge request!606Deduplicate zones in reorder_polygons
......@@ -38,6 +38,7 @@ class Command(BaseCommand):
'--update-batch-size',
help='Batch size used to update zones. Updates are run with each retrieval batch.',
type=int,
default=1000,
)
parser.add_argument(
'--dry-run',
......@@ -51,7 +52,15 @@ class Command(BaseCommand):
default=True,
)
def handle(self, *args, offset=0, limit=0, batch_size=1000, update_batch_size=None, remove=True, **kwargs):
def handle(self,
*args,
offset=0,
limit=0,
batch_size=1000,
update_batch_size=1000,
dry_run=False,
remove=True,
**kwargs):
assert batch_size > 0, 'Batch size must be positive.'
assert update_batch_size > 0, 'Update batch size must be positive.'
......@@ -77,10 +86,14 @@ class Command(BaseCommand):
zone.polygon.reorder()
except Exception as e:
if old_polygon.width < 1 or old_polygon.height < 1 and remove:
logger.info('Removing incoherent zone {}…'.format(zone.id))
zone.transcriptions.all().delete()
zone.elements.all().delete()
zone.delete()
if dry_run:
logger.info('Would remove incoherent zone {}'.format(zone.id))
else:
logger.info('Removing incoherent zone {}…'.format(zone.id))
zone.transcriptions.all().delete()
zone.elements.all().delete()
zone.regions.all().delete()
zone.delete()
removed += 1
total -= 1
else:
......@@ -90,9 +103,29 @@ class Command(BaseCommand):
continue
if zone.polygon != old_polygon:
reordered_zones.append(zone)
# Check if a zone already exists and either reorder or deduplicate
existing_zone = Zone.objects \
.filter(image_id=zone.image_id, polygon=zone.polygon) \
.exclude(id=zone.id) \
.first()
if not existing_zone:
reordered_zones.append(zone)
continue
if dry_run:
logger.info('Would remove zone {} and reassign to zone {}'.format(zone.id, existing_zone.id))
continue
logger.info('Removing zone {} and reassigning to zone {}…'.format(zone.id, existing_zone.id))
zone.transcriptions.all().update(zone_id=existing_zone.id)
zone.elements.all().update(zone_id=existing_zone.id)
zone.regions.all().update(zone_id=existing_zone.id)
zone.delete()
removed += 1
total -= 1
Zone.objects.bulk_update(reordered_zones, ['polygon'], batch_size=update_batch_size)
if not dry_run:
Zone.objects.bulk_update(reordered_zones, ['polygon'], batch_size=update_batch_size)
reordered += len(reordered_zones)
completed += batch.count()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment