Browse Source

Remove old models related to relation

pull/1703/head
Hironsan 2 years ago
parent
commit
4eb9ab1b16
8 changed files with 63 additions and 38 deletions
  1. 10
      backend/label_types/migrations/0006_auto_20220222_0512.py
  2. 17
      backend/label_types/migrations/0007_delete_relationtypeold.py
  3. 12
      backend/label_types/models.py
  4. 16
      backend/labels/migrations/0008_auto_20220222_0630.py
  5. 16
      backend/labels/migrations/0009_delete_relationold.py
  6. 18
      backend/labels/models.py
  7. 6
      backend/labels/serializers.py
  8. 6
      backend/labels/views.py

10
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):

17
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",
),
]

12
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):

16
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):

16
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",
),
]

18
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")

6
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")

6
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]
Loading…
Cancel
Save