Skip to content

Commit

Permalink
Splitted 15th migration to db spec and data migration. Also minor fix…
Browse files Browse the repository at this point in the history
…es for migration 16. (#362)
  • Loading branch information
azhavoro authored and nmanovic committed Mar 28, 2019
1 parent 3c96963 commit b683a19
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 54 deletions.
53 changes: 2 additions & 51 deletions cvat/apps/engine/migrations/0015_rest_api_20190217.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,6 @@ def set_segment_size(apps, schema_editor):
task.segment_size = segment.stop_frame - segment.start_frame + 1
task.save()

def parse_attribute(value):
match = re.match(r'^([~@])(\w+)=(\w+):(.+)?$', value)
if match:
prefix = match.group(1)
input_type = match.group(2)
name = match.group(3)
if match.group(4):
values = list(csv.reader(StringIO(match.group(4)),
quotechar="'"))[0]
else:
values = []

return {'prefix':prefix, 'type':input_type, 'name':name, 'values':values}
else:
return None

def split_text_attribute(apps, schema_editor):
AttributeSpec = apps.get_model('engine', 'AttributeSpec')
for attribute in AttributeSpec.objects.all():
spec = parse_attribute(attribute.text)
if spec:
attribute.mutable = (spec['prefix'] == '~')
attribute.input_type = spec['type']
attribute.name = spec['name']
attribute.default_value = spec['values'][0]
attribute.values = '\n'.join(spec['values'])
attribute.save()

def join_text_attribute(apps, schema_editor):
AttributeSpec = apps.get_model('engine', 'AttributeSpec')
for attribute in AttributeSpec.objects.all():
attribute.text = ""
if attribute.mutable:
attribute.text += "~"
else:
attribute.text += "@"

attribute.text += attribute.input_type
attribute.text += "=" + attribute.name + ":"
attribute.text += ",".join(attribute.values.split('\n'))
attribute.save()

class Migration(migrations.Migration):

dependencies = [
Expand Down Expand Up @@ -196,23 +154,16 @@ class Migration(migrations.Migration):
name='status',
field=models.CharField(choices=[('ANNOTATION', 'annotation'), ('VALIDATION', 'validation'), ('COMPLETED', 'completed')], default=cvat.apps.engine.models.StatusChoice('annotation'), max_length=32),
),
migrations.RunPython(
code=split_text_attribute,
reverse_code=join_text_attribute,
),
migrations.RemoveField(
migrations.AlterField(
model_name='attributespec',
name='text',
field=models.CharField(default='', max_length=1024),
),
migrations.AlterField(
model_name='attributespec',
name='input_type',
field=models.CharField(choices=[('checkbox', 'CHECKBOX'), ('radio', 'RADIO'), ('number', 'NUMBER'), ('text', 'TEXT'), ('select', 'SELECT')], max_length=16),
),
migrations.AlterUniqueTogether(
name='attributespec',
unique_together={('label', 'name')},
),
migrations.AlterField(
model_name='task',
name='segment_size',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import re
import csv
from io import StringIO
from django.conf import settings
from django.db import migrations, models
import django.db.migrations.operations.special
import django.db.models.deletion
import cvat.apps.engine.models

def parse_attribute(value):
match = re.match(r'^([~@])(\w+)=(\w+):(.+)?$', value)
if match:
prefix = match.group(1)
input_type = match.group(2)
name = match.group(3)
if match.group(4):
values = list(csv.reader(StringIO(match.group(4)),
quotechar="'"))[0]
else:
values = []

return {'prefix':prefix, 'type':input_type, 'name':name, 'values':values}
else:
return None

def split_text_attribute(apps, schema_editor):
AttributeSpec = apps.get_model('engine', 'AttributeSpec')
for attribute in AttributeSpec.objects.all():
spec = parse_attribute(attribute.text)
if spec:
attribute.mutable = (spec['prefix'] == '~')
attribute.input_type = spec['type']
attribute.name = spec['name']
attribute.default_value = spec['values'][0] if spec['values'] else ''
attribute.values = '\n'.join(spec['values'])
attribute.save()

def join_text_attribute(apps, schema_editor):
AttributeSpec = apps.get_model('engine', 'AttributeSpec')
for attribute in AttributeSpec.objects.all():
attribute.text = ""
if attribute.mutable:
attribute.text += "~"
else:
attribute.text += "@"

attribute.text += attribute.input_type
attribute.text += "=" + attribute.name + ":"
attribute.text += ",".join(attribute.values.split('\n'))
attribute.save()

class Migration(migrations.Migration):

dependencies = [
('engine', '0015_rest_api_20190217'),
]

operations = [
migrations.RunPython(
code=split_text_attribute,
reverse_code=join_text_attribute,
),
migrations.RemoveField(
model_name='attributespec',
name='text',
),
migrations.AlterUniqueTogether(
name='attributespec',
unique_together={('label', 'name')},
),
]
5 changes: 2 additions & 3 deletions cvat/apps/engine/migrations/0016_auto_20190221_1525.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ def process_paths(db_job, apps, db_labels, db_attributes, db_alias):
for attr in shape.attributes:
db_attrspec = db_attributes[attr.spec_id]
db_attrval = TrackedShapeAttributeVal()
db_attrval.id = len(new_db_shapes)
db_attrval.shape_id = len(new_db_shapes)
db_attrval.spec = db_attrspec
db_attrval.value = attr.value
Expand Down Expand Up @@ -537,7 +536,7 @@ def _get_shape_attr_class(shape_type):
db_shape.outside = shape.outside

for attr in list(shape.trackedshapeattributeval_set.all()):
db_attrspec = db_attributes[attr.id]
db_attrspec = db_attributes[attr.spec_id]
db_attrval = _get_shape_attr_class(shape_type)()
if shape_type == 'polygon_paths':
db_attrval.polygon_id = len(new_db_shapes)
Expand Down Expand Up @@ -601,7 +600,7 @@ def copy_annotations_backward(apps, schema_editor):
class Migration(migrations.Migration):

dependencies = [
('engine', '0015_rest_api_20190217'),
('engine', '0015_rest_api_data_migration_20190217'),
]

operations = [
Expand Down

0 comments on commit b683a19

Please sign in to comment.