From 0320fcfabc12c5dcfe5a663972a1c51f75df4daa Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 6 Apr 2019 14:30:47 +0200 Subject: [PATCH 03/10] base: add mixins for child model views (#31595) --- passerelle/base/mixins.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 passerelle/base/mixins.py diff --git a/passerelle/base/mixins.py b/passerelle/base/mixins.py new file mode 100644 index 00000000..a0b3852a --- /dev/null +++ b/passerelle/base/mixins.py @@ -0,0 +1,39 @@ +# passerelle - uniform access to multiple data sources and services +# Copyright (C) 2019 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.utils import functional + + +class ResourceChildViewMixin(object): + '''Mixin to help implementing view for child objects of resource objets.''' + + @property + def resource_class(self): + field = self.model._meta.get_field('resource') + return field.related_model + + @functional.cached_property + def resource(self): + return self.resource_class.objects.get(slug=self.kwargs['slug']) + + def get_queryset(self): + qs = super(ResourceChildViewMixin, self).get_queryset() + return qs.filter(resource=self.resource) + + def get_success_url(self): + return self.resource.get_absolute_url() + + -- 2.20.1