diff --git a/backend/label_types/migrations/0006_auto_20220222_0512.py b/backend/label_types/migrations/0006_auto_20220222_0512.py index bea9ab35..692462d5 100644 --- a/backend/label_types/migrations/0006_auto_20220222_0512.py +++ b/backend/label_types/migrations/0006_auto_20220222_0512.py @@ -10,11 +10,17 @@ def copy_relation_type(apps, schema_editor): RelationType( background_color=relation_type.color, text=relation_type.name, project=relation_type.project ).save() + relation_type.delete() def delete_new_relation_type(apps, schema_editor): - RelationType = apps.get_model("label_types", "RelationType") - RelationType.objects.all().delete() + RelationTypeNew = apps.get_model("label_types", "RelationType") + RelationTypeOld = apps.get_model("label_types", "RelationTypeOld") + for relation_type in RelationTypeNew.objects.all(): + RelationTypeOld.objects.get_or_create( + color=relation_type.background_color, name=relation_type.text, project=relation_type.project + ) + relation_type.delete() class Migration(migrations.Migration): diff --git a/backend/label_types/migrations/0007_delete_relationtypeold.py b/backend/label_types/migrations/0007_delete_relationtypeold.py new file mode 100644 index 00000000..d076c09e --- /dev/null +++ b/backend/label_types/migrations/0007_delete_relationtypeold.py @@ -0,0 +1,17 @@ +# Generated by Django 4.0.2 on 2022-02-22 06:46 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("labels", "0009_delete_relationold"), + ("label_types", "0006_auto_20220222_0512"), + ] + + operations = [ + migrations.DeleteModel( + name="RelationTypeOld", + ), + ] diff --git a/backend/label_types/models.py b/backend/label_types/models.py index 94dc66e4..2c45491c 100644 --- a/backend/label_types/models.py +++ b/backend/label_types/models.py @@ -72,18 +72,6 @@ class SpanType(LabelType): return SpanType.objects.filter(project=self.project) -class RelationTypeOld(models.Model): - color = models.TextField() - name = models.TextField() - project = models.ForeignKey(Project, related_name="relation_types", on_delete=models.CASCADE) - - def __str__(self): - return self.name - - class Meta: - unique_together = ("color", "name") - - class RelationType(LabelType): @property def labels(self): diff --git a/backend/labels/migrations/0008_auto_20220222_0630.py b/backend/labels/migrations/0008_auto_20220222_0630.py index e6e37785..9b8b1288 100644 --- a/backend/labels/migrations/0008_auto_20220222_0630.py +++ b/backend/labels/migrations/0008_auto_20220222_0630.py @@ -19,7 +19,21 @@ def copy_relation(apps, schema_editor): def delete_new_relation(apps, schema_editor): RelationNew = apps.get_model("labels", "RelationNew") - RelationNew.objects.all().delete() + RelationOld = apps.get_model("labels", "RelationOld") + RelationTypeOld = apps.get_model("label_types", "RelationTypeOld") + for relation in RelationNew.objects.all(): + relation_type, created = RelationTypeOld.objects.get_or_create( + project=relation.type.project, name=relation.type.text, color=relation.type.background_color + ) + RelationOld( + annotation_id_1=relation.from_id.id, + annotation_id_2=relation.to_id.id, + timestamp=relation.created_at, + user=relation.user, + project=relation.example.project, + type=relation_type, + ).save() + relation.delete() class Migration(migrations.Migration): diff --git a/backend/labels/migrations/0009_delete_relationold.py b/backend/labels/migrations/0009_delete_relationold.py new file mode 100644 index 00000000..9e5a836b --- /dev/null +++ b/backend/labels/migrations/0009_delete_relationold.py @@ -0,0 +1,16 @@ +# Generated by Django 4.0.2 on 2022-02-22 06:46 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("labels", "0008_auto_20220222_0630"), + ] + + operations = [ + migrations.DeleteModel( + name="RelationOld", + ), + ] diff --git a/backend/labels/models.py b/backend/labels/models.py index 90a4e0b6..ac8d947d 100644 --- a/backend/labels/models.py +++ b/backend/labels/models.py @@ -4,8 +4,7 @@ from django.db import models from .managers import CategoryManager, LabelManager, SpanManager, TextLabelManager from examples.models import Example -from label_types.models import CategoryType, RelationType, RelationTypeOld, SpanType -from projects.models import Project +from label_types.models import CategoryType, RelationType, SpanType class Label(models.Model): @@ -91,21 +90,6 @@ class TextLabel(Label): unique_together = ("example", "user", "text") -class RelationOld(models.Model): - annotation_id_1 = models.IntegerField() - annotation_id_2 = models.IntegerField() - type = models.ForeignKey(RelationTypeOld, related_name="annotation_relations", on_delete=models.CASCADE) - timestamp = models.DateTimeField() - user = models.ForeignKey(User, related_name="annotation_relations", on_delete=models.CASCADE) - project = models.ForeignKey(Project, related_name="annotation_relations", on_delete=models.CASCADE) - - def __str__(self): - return self.__dict__.__str__() - - class Meta: - unique_together = ("annotation_id_1", "annotation_id_2", "type", "project") - - class RelationNew(Label): from_id = models.ForeignKey(Span, on_delete=models.CASCADE, related_name="from_relations") to_id = models.ForeignKey(Span, on_delete=models.CASCADE, related_name="to_relations") diff --git a/backend/labels/serializers.py b/backend/labels/serializers.py index 5c06e9fe..f7209c01 100644 --- a/backend/labels/serializers.py +++ b/backend/labels/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from .models import Category, RelationOld, Span, TextLabel +from .models import Category, RelationNew, Span, TextLabel from examples.models import Example from label_types.models import CategoryType, SpanType @@ -65,5 +65,5 @@ class RelationSerializer(serializers.ModelSerializer): return super().validate(attrs) class Meta: - model = RelationOld - fields = ("id", "annotation_id_1", "annotation_id_2", "type", "user", "timestamp") + model = RelationNew + fields = ("id", "prob", "user", "example", "created_at", "updated_at", "from_id", "to_id", "type") diff --git a/backend/labels/views.py b/backend/labels/views.py index 51f59bc9..e485f22b 100644 --- a/backend/labels/views.py +++ b/backend/labels/views.py @@ -14,7 +14,7 @@ from .serializers import ( SpanSerializer, TextLabelSerializer, ) -from labels.models import Category, Label, RelationOld, Span, TextLabel +from labels.models import Category, Label, RelationNew, Span, TextLabel from projects.models import Project from projects.permissions import IsProjectMember @@ -118,12 +118,12 @@ class RelationList(generics.ListCreateAPIView): def delete(self, request, *args, **kwargs): delete_ids = request.data["ids"] - RelationOld.objects.filter(pk__in=delete_ids).delete() + RelationNew.objects.filter(pk__in=delete_ids).delete() return Response(status=status.HTTP_204_NO_CONTENT) class RelationDetail(generics.RetrieveUpdateDestroyAPIView): - queryset = RelationOld.objects.all() + queryset = RelationNew.objects.all() serializer_class = RelationSerializer lookup_url_kwarg = "annotation_id" permission_classes = [IsAuthenticated & IsProjectMember]