feat: Export relationships with answers

See #27
See #29
Resolves #25
This commit is contained in:
James Graham
2020-04-01 13:15:48 +01:00
parent 76ae447cc6
commit 76270c4572
3 changed files with 35 additions and 12 deletions

View File

@@ -6,6 +6,7 @@ import typing
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils.text import slugify
from .person import Person from .person import Person
@@ -48,6 +49,10 @@ class RelationshipQuestion(models.Model):
[choice.pk, str(choice)] for choice in self.answers.all() [choice.pk, str(choice)] for choice in self.answers.all()
] ]
@property
def slug(self) -> str:
return slugify(self.text)
def __str__(self) -> str: def __str__(self) -> str:
return self.text return self.text
@@ -80,6 +85,10 @@ class RelationshipQuestionChoice(models.Model):
order = models.SmallIntegerField(default=0, order = models.SmallIntegerField(default=0,
blank=False, null=False) blank=False, null=False)
@property
def slug(self) -> str:
return slugify(self.text)
def __str__(self) -> str: def __str__(self) -> str:
return self.text return self.text

View File

@@ -46,13 +46,15 @@ class FlattenedModelSerializer(serializers.ModelSerializer):
return data_out return data_out
@property @property
def column_headers(self) -> typing.Collection: def column_headers(self) -> typing.List[str]:
""" """
Get all column headers that will be output by this serializer. Get all column headers that will be output by this serializer.
""" """
return self.flatten_data(self.fields, fields = self.flatten_data(self.fields,
sub_type=serializers.BaseSerializer, sub_type=serializers.BaseSerializer,
sub_value_accessor=lambda x: x.fields.items()) sub_value_accessor=lambda x: x.fields.items())
return list(fields)
def to_representation(self, instance) -> typing.OrderedDict: def to_representation(self, instance) -> typing.OrderedDict:
""" """
@@ -100,14 +102,25 @@ class RelationshipSerializer(FlattenedModelSerializer):
'target', 'target',
] ]
@property
def column_headers(self) -> typing.List[str]:
headers = super().column_headers
# Add relationship questions to columns
for question in models.RelationshipQuestion.objects.all():
headers.append(question.slug)
return headers
def to_representation(self, instance): def to_representation(self, instance):
rep = super().to_representation(instance) rep = super().to_representation(instance)
# try: try:
# for answer in instance.current_answers.question_answers.all(): # Add relationship question answers to data
# rep[answer.question.text] = answer.text for answer in instance.current_answers.question_answers.all():
# rep[answer.question.slug] = answer.slug
# except AttributeError:
# pass except AttributeError:
pass
return rep return rep

View File

@@ -16,7 +16,8 @@ class CsvExportView(LoginRequiredMixin, BaseListView):
response = HttpResponse(content_type='text/csv') response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename="{self.get_context_object_name(self.object_list)}.csv"' response['Content-Disposition'] = f'attachment; filename="{self.get_context_object_name(self.object_list)}.csv"'
serializer = self.serializer_class(self.get_queryset(), many=True) # Force ordering by PK - though this should be default anyway
serializer = self.serializer_class(self.get_queryset().order_by('pk'), many=True)
writer = csv.DictWriter(response, fieldnames=serializer.child.column_headers) writer = csv.DictWriter(response, fieldnames=serializer.child.column_headers)
writer.writeheader() writer.writeheader()