From d985e93be0176b09f10f9b092bd61fed85065818 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 10 Dec 2014 12:55:25 +0100 Subject: [PATCH] Splitting tags if containing , --- taiga/base/serializers.py | 8 +++-- .../migrations/0003_auto_20141210_1108.py | 36 +++++++++++++++++++ taiga/projects/issues/serializers.py | 4 +-- .../migrations/0013_auto_20141210_1040.py | 36 +++++++++++++++++++ .../migrations/0004_auto_20141210_1107.py | 36 +++++++++++++++++++ taiga/projects/tasks/serializers.py | 4 +-- .../migrations/0008_auto_20141210_1107.py | 36 +++++++++++++++++++ taiga/projects/userstories/serializers.py | 4 +-- 8 files changed, 156 insertions(+), 8 deletions(-) create mode 100644 taiga/projects/issues/migrations/0003_auto_20141210_1108.py create mode 100644 taiga/projects/migrations/0013_auto_20141210_1040.py create mode 100644 taiga/projects/tasks/migrations/0004_auto_20141210_1107.py create mode 100644 taiga/projects/userstories/migrations/0008_auto_20141210_1107.py diff --git a/taiga/base/serializers.py b/taiga/base/serializers.py index 213a7746..8c54677b 100644 --- a/taiga/base/serializers.py +++ b/taiga/base/serializers.py @@ -21,7 +21,7 @@ from rest_framework import serializers from .neighbors import get_neighbors -class PickleField(serializers.WritableField): +class TagsField(serializers.WritableField): """ Pickle objects serializer. """ @@ -29,7 +29,11 @@ class PickleField(serializers.WritableField): return obj def from_native(self, data): - return data + if not data: + return data + + ret = sum([tag.split(",") for tag in data], []) + return ret class JsonField(serializers.WritableField): diff --git a/taiga/projects/issues/migrations/0003_auto_20141210_1108.py b/taiga/projects/issues/migrations/0003_auto_20141210_1108.py new file mode 100644 index 00000000..b8ee567c --- /dev/null +++ b/taiga/projects/issues/migrations/0003_auto_20141210_1108.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.db import connection +from taiga.projects.userstories.models import * +from taiga.projects.tasks.models import * +from taiga.projects.issues.models import * +from taiga.projects.models import * + +def _fix_tags_model(tags_model): + table_name = tags_model._meta.db_table + query = "select id from (select id, unnest(tags) tag from %s) x where tag LIKE '%%,%%'"%(table_name) + cursor = connection.cursor() + cursor.execute(query) + for row in cursor.fetchall(): + id = row[0] + instance = tags_model.objects.get(id=id) + instance.tags = [tag.replace(",", "") for tag in instance.tags] + instance.save() + + +def fix_tags(apps, schema_editor): + print("Fixing user issue tags") + _fix_tags_model(Issue) + + +class Migration(migrations.Migration): + + dependencies = [ + ('issues', '0002_issue_external_reference'), + ] + + operations = [ + migrations.RunPython(fix_tags), + ] diff --git a/taiga/projects/issues/serializers.py b/taiga/projects/issues/serializers.py index cc025185..91034a68 100644 --- a/taiga/projects/issues/serializers.py +++ b/taiga/projects/issues/serializers.py @@ -16,7 +16,7 @@ from rest_framework import serializers -from taiga.base.serializers import Serializer, PickleField, NeighborsSerializerMixin, PgArrayField +from taiga.base.serializers import Serializer, TagsField, NeighborsSerializerMixin, PgArrayField from taiga.mdrender.service import render as mdrender from taiga.projects.validators import ProjectExistsValidator from taiga.projects.notifications.validators import WatchersValidator @@ -25,7 +25,7 @@ from . import models class IssueSerializer(WatchersValidator, serializers.ModelSerializer): - tags = PickleField(required=False) + tags = TagsField(required=False) external_reference = PgArrayField(required=False) is_closed = serializers.Field(source="is_closed") comment = serializers.SerializerMethodField("get_comment") diff --git a/taiga/projects/migrations/0013_auto_20141210_1040.py b/taiga/projects/migrations/0013_auto_20141210_1040.py new file mode 100644 index 00000000..93c093bc --- /dev/null +++ b/taiga/projects/migrations/0013_auto_20141210_1040.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.db import connection +from taiga.projects.userstories.models import * +from taiga.projects.tasks.models import * +from taiga.projects.issues.models import * +from taiga.projects.models import * + +def _fix_tags_model(tags_model): + table_name = tags_model._meta.db_table + query = "select id from (select id, unnest(tags) tag from %s) x where tag LIKE '%%,%%'"%(table_name) + cursor = connection.cursor() + cursor.execute(query) + for row in cursor.fetchall(): + id = row[0] + instance = tags_model.objects.get(id=id) + instance.tags = [tag.replace(",", "") for tag in instance.tags] + instance.save() + + +def fix_tags(apps, schema_editor): + print("Fixing project tags") + _fix_tags_model(Project) + + +class Migration(migrations.Migration): + + dependencies = [ + ('projects', '0012_auto_20141210_1009'), + ] + + operations = [ + migrations.RunPython(fix_tags), + ] diff --git a/taiga/projects/tasks/migrations/0004_auto_20141210_1107.py b/taiga/projects/tasks/migrations/0004_auto_20141210_1107.py new file mode 100644 index 00000000..b4c093f3 --- /dev/null +++ b/taiga/projects/tasks/migrations/0004_auto_20141210_1107.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.db import connection +from taiga.projects.userstories.models import * +from taiga.projects.tasks.models import * +from taiga.projects.issues.models import * +from taiga.projects.models import * + +def _fix_tags_model(tags_model): + table_name = tags_model._meta.db_table + query = "select id from (select id, unnest(tags) tag from %s) x where tag LIKE '%%,%%'"%(table_name) + cursor = connection.cursor() + cursor.execute(query) + for row in cursor.fetchall(): + id = row[0] + instance = tags_model.objects.get(id=id) + instance.tags = [tag.replace(",", "") for tag in instance.tags] + instance.save() + + +def fix_tags(apps, schema_editor): + print("Fixing user task tags") + _fix_tags_model(Task) + + +class Migration(migrations.Migration): + + dependencies = [ + ('tasks', '0003_task_external_reference'), + ] + + operations = [ + migrations.RunPython(fix_tags), + ] diff --git a/taiga/projects/tasks/serializers.py b/taiga/projects/tasks/serializers.py index fe71186b..8c132c5b 100644 --- a/taiga/projects/tasks/serializers.py +++ b/taiga/projects/tasks/serializers.py @@ -16,7 +16,7 @@ from rest_framework import serializers -from taiga.base.serializers import Serializer, PickleField, NeighborsSerializerMixin, PgArrayField +from taiga.base.serializers import Serializer, TagsField, NeighborsSerializerMixin, PgArrayField from taiga.mdrender.service import render as mdrender from taiga.projects.validators import ProjectExistsValidator from taiga.projects.milestones.validators import SprintExistsValidator @@ -27,7 +27,7 @@ from . import models class TaskSerializer(WatchersValidator, serializers.ModelSerializer): - tags = PickleField(required=False, default=[]) + tags = TagsField(required=False, default=[]) external_reference = PgArrayField(required=False) comment = serializers.SerializerMethodField("get_comment") milestone_slug = serializers.SerializerMethodField("get_milestone_slug") diff --git a/taiga/projects/userstories/migrations/0008_auto_20141210_1107.py b/taiga/projects/userstories/migrations/0008_auto_20141210_1107.py new file mode 100644 index 00000000..8c4b6c8d --- /dev/null +++ b/taiga/projects/userstories/migrations/0008_auto_20141210_1107.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.db import connection +from taiga.projects.userstories.models import * +from taiga.projects.tasks.models import * +from taiga.projects.issues.models import * +from taiga.projects.models import * + +def _fix_tags_model(tags_model): + table_name = tags_model._meta.db_table + query = "select id from (select id, unnest(tags) tag from %s) x where tag LIKE '%%,%%'"%(table_name) + cursor = connection.cursor() + cursor.execute(query) + for row in cursor.fetchall(): + id = row[0] + instance = tags_model.objects.get(id=id) + instance.tags = [tag.replace(",", "") for tag in instance.tags] + instance.save() + + +def fix_tags(apps, schema_editor): + print("Fixing user story tags") + _fix_tags_model(UserStory) + + +class Migration(migrations.Migration): + + dependencies = [ + ('userstories', '0007_userstory_external_reference'), + ] + + operations = [ + migrations.RunPython(fix_tags), + ] diff --git a/taiga/projects/userstories/serializers.py b/taiga/projects/userstories/serializers.py index c768f909..7d3017f9 100644 --- a/taiga/projects/userstories/serializers.py +++ b/taiga/projects/userstories/serializers.py @@ -18,7 +18,7 @@ import json from django.apps import apps from rest_framework import serializers -from taiga.base.serializers import Serializer, PickleField, NeighborsSerializerMixin, PgArrayField +from taiga.base.serializers import Serializer, TagsField, NeighborsSerializerMixin, PgArrayField from taiga.mdrender.service import render as mdrender from taiga.projects.validators import ProjectExistsValidator, UserStoryStatusExistsValidator from taiga.projects.userstories.validators import UserStoryExistsValidator @@ -38,7 +38,7 @@ class RolePointsField(serializers.WritableField): class UserStorySerializer(WatchersValidator, serializers.ModelSerializer): - tags = PickleField(default=[], required=False) + tags = TagsField(default=[], required=False) external_reference = PgArrayField(required=False) points = RolePointsField(source="role_points", required=False) total_points = serializers.SerializerMethodField("get_total_points")