0002-base-add-mixins-for-child-model-views-31595.patch
passerelle/base/mixins.py | ||
---|---|---|
1 |
# passerelle - uniform access to multiple data sources and services |
|
2 |
# Copyright (C) 2019 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software: you can redistribute it and/or modify it |
|
5 |
# under the terms of the GNU Affero General Public License as published |
|
6 |
# by the Free Software Foundation, either version 3 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU Affero General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU Affero General Public License |
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
from django.utils import functional |
|
18 | ||
19 | ||
20 |
class ResourceChildViewMixin(object): |
|
21 |
'''Mixin to help implementing view for child objects of resource objets.''' |
|
22 | ||
23 |
@property |
|
24 |
def resource_class(self): |
|
25 |
field = self.model._meta.get_field('resource') |
|
26 |
return field.related_model |
|
27 | ||
28 |
@functional.cached_property |
|
29 |
def resource(self): |
|
30 |
return self.resource_class.objects.get(slug=self.kwargs['slug']) |
|
31 | ||
32 |
def get_queryset(self): |
|
33 |
qs = super(ResourceChildViewMixin, self).get_queryset() |
|
34 |
return qs.filter(resource=self.resource) |
|
35 | ||
36 |
def get_success_url(self): |
|
37 |
return self.resource.get_absolute_url() |
|
38 | ||
39 | ||
0 |
- |