From 8b2cc1c70d1a865a71aa18b5eee633b930660be5 Mon Sep 17 00:00:00 2001 From: J-E Castagnede Date: Tue, 21 May 2024 14:34:14 +0200 Subject: [PATCH] Add a get_columns method to change column list dynamically. --- CHANGES.md | 4 ++++ mapentity/views/base.py | 3 +++ mapentity/views/generic.py | 6 +++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6d3e87cd..4ee3bed2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ CHANGELOG 8.8.1+dev (XXXX-XX-XX) ----------------------- +**Improvements** + +- Add a get_columns method to change column list dynamically. + 8.8.1 (2024-05-07) ------------------- diff --git a/mapentity/views/base.py b/mapentity/views/base.py index 6e5c0ad5..13027249 100644 --- a/mapentity/views/base.py +++ b/mapentity/views/base.py @@ -131,6 +131,9 @@ def __init__(self, *args, **kwargs): self.columns.remove('id') self.columns.insert(0, 'id') + def get_columns(self): + return self.columns + @view_permission_required() def dispatch(self, *args, **kwargs): return super().dispatch(*args, **kwargs) diff --git a/mapentity/views/generic.py b/mapentity/views/generic.py index a0538b79..428e695d 100755 --- a/mapentity/views/generic.py +++ b/mapentity/views/generic.py @@ -76,7 +76,7 @@ def dispatch(self, request, *args, **kwargs): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['filterform'] = self._filterform # From FilterListMixin - context['columns'] = self.columns # From BaseListView + context['columns'] = self.get_columns() # From BaseListView context['unorderable_columns'] = self.unorderable_columns # From BaseListView context['searchable_columns'] = self.searchable_columns # From BaseListView context['create_label'] = self.get_model().get_create_label() @@ -131,14 +131,14 @@ def csv_view(self, request, context, **kwargs): serializer = mapentity_serializers.CSVSerializer() response = HttpResponse(content_type='text/csv') serializer.serialize(queryset=self.get_queryset(), stream=response, - model=self.get_model(), fields=self.columns, ensure_ascii=True) + model=self.get_model(), fields=self.get_columns(), ensure_ascii=True) return response def shape_view(self, request, context, **kwargs): serializer = mapentity_serializers.ZipShapeSerializer() response = HttpResponse(content_type='application/zip') serializer.serialize(queryset=self.get_queryset(), model=self.get_model(), - stream=response, fields=self.columns) + stream=response, fields=self.get_columns()) response['Content-length'] = str(len(response.content)) return response