You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

  1. from django.db import migrations
  2. def copy_label_to_subclass(apps, schema_editor):
  3. Label = apps.get_model('api', 'Label')
  4. for label in Label.objects.all():
  5. project_type = label.project.project_type
  6. if project_type.endswith('Classification'):
  7. model = apps.get_model('api', 'CategoryType')
  8. else:
  9. model = apps.get_model('api', 'SpanType')
  10. model.objects.create(
  11. id=label.id,
  12. text=label.text,
  13. prefix_key=label.prefix_key,
  14. suffix_key=label.suffix_key,
  15. project=label.project,
  16. background_color=label.background_color,
  17. text_color=label.text_color,
  18. created_at=label.created_at,
  19. updated_at=label.updated_at
  20. )
  21. def delete_subclass_object(apps, schema_editor):
  22. CategoryType = apps.get_model('api', 'CategoryType')
  23. SpanType = apps.get_model('api', 'SpanType')
  24. Label = apps.get_model('api', 'Label')
  25. for model in [CategoryType, SpanType]:
  26. for label in model.objects.all():
  27. old_label = Label(
  28. id=label.id,
  29. text=label.text,
  30. prefix_key=label.prefix_key,
  31. suffix_key=label.suffix_key,
  32. project=label.project,
  33. background_color=label.background_color,
  34. text_color=label.text_color,
  35. created_at=label.created_at,
  36. updated_at=label.updated_at
  37. )
  38. label.delete()
  39. old_label.save()
  40. class Migration(migrations.Migration):
  41. dependencies = [
  42. ('api', '0020_auto_20211221_1415'),
  43. ]
  44. operations = [
  45. migrations.RunPython(
  46. code=copy_label_to_subclass,
  47. reverse_code=delete_subclass_object
  48. ),
  49. ]