refactor: Move export into separate app

This commit is contained in:
James Graham
2020-04-01 16:00:22 +01:00
parent 76270c4572
commit d02f865952
17 changed files with 188 additions and 128 deletions

View File

@@ -3,7 +3,6 @@ Views for displaying or manipulating models within the `people` app.
"""
from . import (
export,
network,
person,
relationship

View File

@@ -1,36 +0,0 @@
import csv
import typing
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from django.views.generic.list import BaseListView
from .. import models, serializers
class CsvExportView(LoginRequiredMixin, BaseListView):
model = None
serializer_class = None
def render_to_response(self, context: typing.Dict) -> HttpResponse:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename="{self.get_context_object_name(self.object_list)}.csv"'
# 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()
writer.writerows(serializer.data)
return response
class PersonExportView(CsvExportView):
model = models.Person
serializer_class = serializers.PersonExportSerializer
class RelationshipExportView(CsvExportView):
model = models.Relationship
serializer_class = serializers.RelationshipSerializer