From 8b504918aa93e0fd0ca887d2efa2472a226e36ed Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 22 Feb 2022 14:22:41 +0900 Subject: [PATCH] Add RelationType model --- ...004_rename_relationtype_relationtypeold.py | 19 ++++ .../migrations/0005_relationtype_and_more.py | 94 +++++++++++++++++++ .../migrations/0006_auto_20220222_0512.py | 28 ++++++ backend/label_types/models.py | 6 ++ 4 files changed, 147 insertions(+) create mode 100644 backend/label_types/migrations/0004_rename_relationtype_relationtypeold.py create mode 100644 backend/label_types/migrations/0005_relationtype_and_more.py create mode 100644 backend/label_types/migrations/0006_auto_20220222_0512.py diff --git a/backend/label_types/migrations/0004_rename_relationtype_relationtypeold.py b/backend/label_types/migrations/0004_rename_relationtype_relationtypeold.py new file mode 100644 index 00000000..c021738a --- /dev/null +++ b/backend/label_types/migrations/0004_rename_relationtype_relationtypeold.py @@ -0,0 +1,19 @@ +# Generated by Django 4.0.2 on 2022-02-22 05:05 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("projects", "0003_alter_project_polymorphic_ctype"), + ("labels", "0005_alter_relation_project"), + ("label_types", "0003_auto_20220204_0201"), + ] + + operations = [ + migrations.RenameModel( + old_name="RelationType", + new_name="RelationTypeOld", + ), + ] diff --git a/backend/label_types/migrations/0005_relationtype_and_more.py b/backend/label_types/migrations/0005_relationtype_and_more.py new file mode 100644 index 00000000..c6a4ce37 --- /dev/null +++ b/backend/label_types/migrations/0005_relationtype_and_more.py @@ -0,0 +1,94 @@ +# Generated by Django 4.0.2 on 2022-02-22 05:11 + +from django.db import migrations, models +import django.db.models.deletion +import label_types.models + + +class Migration(migrations.Migration): + + dependencies = [ + ("projects", "0003_alter_project_polymorphic_ctype"), + ("label_types", "0004_rename_relationtype_relationtypeold"), + ] + + operations = [ + migrations.CreateModel( + name="RelationType", + fields=[ + ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), + ("text", models.CharField(db_index=True, max_length=100)), + ( + "prefix_key", + models.CharField( + blank=True, + choices=[("ctrl", "ctrl"), ("shift", "shift"), ("ctrl shift", "ctrl shift")], + max_length=10, + null=True, + ), + ), + ( + "suffix_key", + models.CharField( + blank=True, + choices=[ + ("0", "0"), + ("1", "1"), + ("2", "2"), + ("3", "3"), + ("4", "4"), + ("5", "5"), + ("6", "6"), + ("7", "7"), + ("8", "8"), + ("9", "9"), + ("a", "a"), + ("b", "b"), + ("c", "c"), + ("d", "d"), + ("e", "e"), + ("f", "f"), + ("g", "g"), + ("h", "h"), + ("i", "i"), + ("j", "j"), + ("k", "k"), + ("l", "l"), + ("m", "m"), + ("n", "n"), + ("o", "o"), + ("p", "p"), + ("q", "q"), + ("r", "r"), + ("s", "s"), + ("t", "t"), + ("u", "u"), + ("v", "v"), + ("w", "w"), + ("x", "x"), + ("y", "y"), + ("z", "z"), + ], + max_length=1, + null=True, + ), + ), + ( + "background_color", + models.CharField(default=label_types.models.generate_random_hex_color, max_length=7), + ), + ("text_color", models.CharField(default="#ffffff", max_length=7)), + ("created_at", models.DateTimeField(auto_now_add=True, db_index=True)), + ("updated_at", models.DateTimeField(auto_now=True)), + ("project", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="projects.project")), + ], + options={ + "ordering": ["created_at"], + "abstract": False, + }, + ), + migrations.AddConstraint( + model_name="relationtype", + constraint=models.UniqueConstraint(fields=("project", "text"), name="label_types_relationtype_is_unique"), + ), + ] diff --git a/backend/label_types/migrations/0006_auto_20220222_0512.py b/backend/label_types/migrations/0006_auto_20220222_0512.py new file mode 100644 index 00000000..bea9ab35 --- /dev/null +++ b/backend/label_types/migrations/0006_auto_20220222_0512.py @@ -0,0 +1,28 @@ +# Generated by Django 4.0.2 on 2022-02-22 05:12 + +from django.db import migrations + + +def copy_relation_type(apps, schema_editor): + RelationTypeOld = apps.get_model("label_types", "RelationTypeOld") + RelationType = apps.get_model("label_types", "RelationType") + for relation_type in RelationTypeOld.objects.all(): + RelationType( + background_color=relation_type.color, text=relation_type.name, project=relation_type.project + ).save() + + +def delete_new_relation_type(apps, schema_editor): + RelationType = apps.get_model("label_types", "RelationType") + RelationType.objects.all().delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ("label_types", "0005_relationtype_and_more"), + ] + + operations = [ + migrations.RunPython(code=copy_relation_type, reverse_code=delete_new_relation_type), + ] diff --git a/backend/label_types/models.py b/backend/label_types/models.py index 1f61453c..94dc66e4 100644 --- a/backend/label_types/models.py +++ b/backend/label_types/models.py @@ -82,3 +82,9 @@ class RelationTypeOld(models.Model): class Meta: unique_together = ("color", "name") + + +class RelationType(LabelType): + @property + def labels(self): + return RelationType.objects.filter(project=self.project)