mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 03:17:07 +00:00
refactor: Move export into separate app
This commit is contained in:
@@ -3,7 +3,6 @@ Views for displaying or manipulating models within the `people` app.
|
||||
"""
|
||||
|
||||
from . import (
|
||||
export,
|
||||
network,
|
||||
person,
|
||||
relationship
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user