From bdcd8bba4127c50038179ef6539f425c3d151fff Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Tue, 10 Mar 2015 13:26:57 +0100 Subject: [PATCH] Adding custom fields to csv exrpot --- taiga/projects/issues/api.py | 2 +- taiga/projects/issues/services.py | 15 ++++++++++++--- taiga/projects/tasks/api.py | 2 +- taiga/projects/tasks/services.py | 14 +++++++++++--- taiga/projects/userstories/services.py | 7 +++++++ tests/integration/test_issues.py | 19 ++++++++++++++++++- tests/integration/test_tasks.py | 18 ++++++++++++++++++ tests/integration/test_userstories.py | 18 ++++++++++++++++++ 8 files changed, 86 insertions(+), 9 deletions(-) diff --git a/taiga/projects/issues/api.py b/taiga/projects/issues/api.py index 192bb79b..d7879640 100644 --- a/taiga/projects/issues/api.py +++ b/taiga/projects/issues/api.py @@ -170,7 +170,7 @@ class IssueViewSet(OCCResourceMixin, HistoryResourceMixin, WatchedResourceMixin, project = get_object_or_404(Project, issues_csv_uuid=uuid) queryset = project.issues.all().order_by('ref') - data = services.issues_to_csv(queryset) + data = services.issues_to_csv(project, queryset) csv_response = HttpResponse(data.getvalue(), content_type='application/csv') csv_response['Content-Disposition'] = 'attachment; filename="issues.csv"' return csv_response diff --git a/taiga/projects/issues/services.py b/taiga/projects/issues/services.py index 241c692e..0733bc87 100644 --- a/taiga/projects/issues/services.py +++ b/taiga/projects/issues/services.py @@ -63,16 +63,19 @@ def update_issues_order_in_bulk(bulk_data): db.update_in_bulk_with_ids(issue_ids, new_order_values, model=models.Issue) -def issues_to_csv(queryset): +def issues_to_csv(project, queryset): csv_data = io.StringIO() fieldnames = ["ref", "subject", "description", "milestone", "owner", "owner_full_name", "assigned_to", "assigned_to_full_name", "status", "severity", "priority", "type", "is_closed", "attachments", "external_reference"] + for custom_attr in project.issuecustomattributes.all(): + fieldnames.append(custom_attr.name) + writer = csv.DictWriter(csv_data, fieldnames=fieldnames) writer.writeheader() for issue in queryset: - writer.writerow({ + issue_data = { "ref": issue.ref, "subject": issue.subject, "description": issue.description, @@ -88,6 +91,12 @@ def issues_to_csv(queryset): "is_closed": issue.is_closed, "attachments": issue.attachments.count(), "external_reference": issue.external_reference, - }) + } + + for custom_attr in project.issuecustomattributes.all(): + value = issue.custom_attributes_values.attributes_values.get(str(custom_attr.id), None) + issue_data[custom_attr.name] = value + + writer.writerow(issue_data) return csv_data diff --git a/taiga/projects/tasks/api.py b/taiga/projects/tasks/api.py index b784bff4..01bfa11f 100644 --- a/taiga/projects/tasks/api.py +++ b/taiga/projects/tasks/api.py @@ -80,7 +80,7 @@ class TaskViewSet(OCCResourceMixin, HistoryResourceMixin, WatchedResourceMixin, project = get_object_or_404(Project, tasks_csv_uuid=uuid) queryset = project.tasks.all().order_by('ref') - data = services.tasks_to_csv(queryset) + data = services.tasks_to_csv(project, queryset) csv_response = HttpResponse(data.getvalue(), content_type='application/csv') csv_response['Content-Disposition'] = 'attachment; filename="tasks.csv"' return csv_response diff --git a/taiga/projects/tasks/services.py b/taiga/projects/tasks/services.py index 334b3dfe..d4a00a1e 100644 --- a/taiga/projects/tasks/services.py +++ b/taiga/projects/tasks/services.py @@ -80,16 +80,19 @@ def snapshot_tasks_in_bulk(bulk_data, user): pass -def tasks_to_csv(queryset): +def tasks_to_csv(project, queryset): csv_data = io.StringIO() fieldnames = ["ref", "subject", "description", "user_story", "milestone", "owner", "owner_full_name", "assigned_to", "assigned_to_full_name", "status", "is_iocaine", "is_closed", "us_order", "taskboard_order", "attachments", "external_reference"] + for custom_attr in project.taskcustomattributes.all(): + fieldnames.append(custom_attr.name) + writer = csv.DictWriter(csv_data, fieldnames=fieldnames) writer.writeheader() for task in queryset: - writer.writerow({ + task_data = { "ref": task.ref, "subject": task.subject, "description": task.description, @@ -106,6 +109,11 @@ def tasks_to_csv(queryset): "taskboard_order": task.taskboard_order, "attachments": task.attachments.count(), "external_reference": task.external_reference, - }) + } + for custom_attr in project.taskcustomattributes.all(): + value = task.custom_attributes_values.attributes_values.get(str(custom_attr.id), None) + task_data[custom_attr.name] = value + + writer.writerow(task_data) return csv_data diff --git a/taiga/projects/userstories/services.py b/taiga/projects/userstories/services.py index 6757a9fb..fefc15c3 100644 --- a/taiga/projects/userstories/services.py +++ b/taiga/projects/userstories/services.py @@ -123,6 +123,9 @@ def userstories_to_csv(project,queryset): "client_requirement", "team_requirement", "attachments", "generated_from_issue", "external_reference", "tasks"] + for custom_attr in project.userstorycustomattributes.all(): + fieldnames.append(custom_attr.name) + writer = csv.DictWriter(csv_data, fieldnames=fieldnames) writer.writeheader() for us in queryset: @@ -158,6 +161,10 @@ def userstories_to_csv(project,queryset): row["{}-points".format(role.slug)] = 0 row['total-points'] = us.get_total_points() + for custom_attr in project.userstorycustomattributes.all(): + value = us.custom_attributes_values.attributes_values.get(str(custom_attr.id), None) + row[custom_attr.name] = value + writer.writerow(row) return csv_data diff --git a/tests/integration/test_issues.py b/tests/integration/test_issues.py index dbf74490..9cd3d2af 100644 --- a/tests/integration/test_issues.py +++ b/tests/integration/test_issues.py @@ -1,4 +1,5 @@ import uuid +import csv from unittest import mock @@ -154,7 +155,6 @@ def test_api_filter_by_text_6(client): issue = f.create_issue(subject="test", owner=user) issue.ref = 123 issue.save() - print(issue.ref, issue.subject) url = reverse("issues-list") + "?q=%s" % (issue.ref) client.login(issue.owner) @@ -181,3 +181,20 @@ def test_get_valid_csv(client): response = client.get("{}?uuid={}".format(url, project.issues_csv_uuid)) assert response.status_code == 200 + + +def test_custom_fields_csv_generation(): + project = f.ProjectFactory.create(issues_csv_uuid=uuid.uuid4().hex) + attr = f.IssueCustomAttributeFactory.create(project=project, name="attr1", description="desc") + issue = f.IssueFactory.create(project=project) + attr_values = issue.custom_attributes_values + attr_values.attributes_values = {str(attr.id):"val1"} + attr_values.save() + queryset = project.issues.all() + data = services.issues_to_csv(project, queryset) + data.seek(0) + reader = csv.reader(data) + row = next(reader) + assert row[15] == attr.name + row = next(reader) + assert row[15] == "val1" diff --git a/tests/integration/test_tasks.py b/tests/integration/test_tasks.py index 108fbad4..63fe177e 100644 --- a/tests/integration/test_tasks.py +++ b/tests/integration/test_tasks.py @@ -1,4 +1,5 @@ import uuid +import csv from unittest import mock @@ -130,3 +131,20 @@ def test_get_valid_csv(client): response = client.get("{}?uuid={}".format(url, project.tasks_csv_uuid)) assert response.status_code == 200 + + +def test_custom_fields_csv_generation(): + project = f.ProjectFactory.create(tasks_csv_uuid=uuid.uuid4().hex) + attr = f.TaskCustomAttributeFactory.create(project=project, name="attr1", description="desc") + task = f.TaskFactory.create(project=project) + attr_values = task.custom_attributes_values + attr_values.attributes_values = {str(attr.id):"val1"} + attr_values.save() + queryset = project.tasks.all() + data = services.tasks_to_csv(project, queryset) + data.seek(0) + reader = csv.reader(data) + row = next(reader) + assert row[16] == attr.name + row = next(reader) + assert row[16] == "val1" diff --git a/tests/integration/test_userstories.py b/tests/integration/test_userstories.py index 1094bfef..ceb0cc12 100644 --- a/tests/integration/test_userstories.py +++ b/tests/integration/test_userstories.py @@ -1,5 +1,6 @@ import copy import uuid +import csv from unittest import mock from django.core.urlresolvers import reverse @@ -261,3 +262,20 @@ def test_get_valid_csv(client): response = client.get("{}?uuid={}".format(url, project.userstories_csv_uuid)) assert response.status_code == 200 + + +def test_custom_fields_csv_generation(): + project = f.ProjectFactory.create(userstories_csv_uuid=uuid.uuid4().hex) + attr = f.UserStoryCustomAttributeFactory.create(project=project, name="attr1", description="desc") + us = f.UserStoryFactory.create(project=project) + attr_values = us.custom_attributes_values + attr_values.attributes_values = {str(attr.id):"val1"} + attr_values.save() + queryset = project.user_stories.all() + data = services.userstories_to_csv(project, queryset) + data.seek(0) + reader = csv.reader(data) + row = next(reader) + assert row[23] == attr.name + row = next(reader) + assert row[23] == "val1"