mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 03:17:07 +00:00
@@ -6,6 +6,7 @@ import typing
|
||||
|
||||
from django.db import models
|
||||
from django.urls import reverse
|
||||
from django.utils.text import slugify
|
||||
|
||||
from .person import Person
|
||||
|
||||
@@ -48,6 +49,10 @@ class RelationshipQuestion(models.Model):
|
||||
[choice.pk, str(choice)] for choice in self.answers.all()
|
||||
]
|
||||
|
||||
@property
|
||||
def slug(self) -> str:
|
||||
return slugify(self.text)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.text
|
||||
|
||||
@@ -80,6 +85,10 @@ class RelationshipQuestionChoice(models.Model):
|
||||
order = models.SmallIntegerField(default=0,
|
||||
blank=False, null=False)
|
||||
|
||||
@property
|
||||
def slug(self) -> str:
|
||||
return slugify(self.text)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.text
|
||||
|
||||
|
||||
@@ -46,14 +46,16 @@ class FlattenedModelSerializer(serializers.ModelSerializer):
|
||||
return data_out
|
||||
|
||||
@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.
|
||||
"""
|
||||
return self.flatten_data(self.fields,
|
||||
fields = self.flatten_data(self.fields,
|
||||
sub_type=serializers.BaseSerializer,
|
||||
sub_value_accessor=lambda x: x.fields.items())
|
||||
|
||||
return list(fields)
|
||||
|
||||
def to_representation(self, instance) -> typing.OrderedDict:
|
||||
"""
|
||||
|
||||
@@ -100,14 +102,25 @@ class RelationshipSerializer(FlattenedModelSerializer):
|
||||
'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):
|
||||
rep = super().to_representation(instance)
|
||||
|
||||
# try:
|
||||
# for answer in instance.current_answers.question_answers.all():
|
||||
# rep[answer.question.text] = answer.text
|
||||
#
|
||||
# except AttributeError:
|
||||
# pass
|
||||
try:
|
||||
# Add relationship question answers to data
|
||||
for answer in instance.current_answers.question_answers.all():
|
||||
rep[answer.question.slug] = answer.slug
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
return rep
|
||||
|
||||
@@ -16,7 +16,8 @@ class CsvExportView(LoginRequiredMixin, BaseListView):
|
||||
response = HttpResponse(content_type='text/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.writeheader()
|
||||
|
||||
Reference in New Issue
Block a user