diff --git a/sapl/crud/base.py b/sapl/crud/base.py index 9992d7fd9..127312cd0 100644 --- a/sapl/crud/base.py +++ b/sapl/crud/base.py @@ -16,8 +16,8 @@ from django.http.response import Http404 from django.shortcuts import redirect from django.utils.decorators import classonlymethod from django.utils.encoding import force_text -from django.utils.translation import ugettext_lazy as _ from django.utils.translation import string_concat +from django.utils.translation import ugettext_lazy as _ from django.views.generic import (CreateView, DeleteView, DetailView, ListView, UpdateView) from django.views.generic.base import ContextMixin @@ -27,6 +27,7 @@ from sapl.crispy_layout_mixin import CrispyLayoutFormMixin, get_field_display from sapl.settings import BASE_DIR from sapl.utils import normalize + logger = logging.getLogger(BASE_DIR.name) ACTION_LIST, ACTION_CREATE, ACTION_DETAIL, ACTION_UPDATE, ACTION_DELETE = \ @@ -1123,7 +1124,9 @@ class MasterDetailCrud(Crud): self, **kwargs) params = {'pk': self.kwargs['pk']} + if self.container_field: + # FIXME refatorar para parent_field com '__' parent_model = getattr( self.model, obj.parent_field).field.related_model @@ -1132,20 +1135,67 @@ class MasterDetailCrud(Crud): params['__'.join(container[1:])] = self.request.user.pk try: - parent = parent_model.objects.get(**params) + parent_object = parent_model.objects.get(**params) except: raise Http404() else: - parent_field = obj.parent_field.split('__')[0] + parent_model = self.model + parent_object = None + if '__' in obj.parent_field: + fields = obj.parent_field.split('__') + for field in fields: + if parent_model == self.model: + parent_model = getattr( + parent_model, field).field.related_model + parent_object = parent_model.objects.get(**params) + else: + parent_object = getattr(parent_object, field) + + else: + parent_model = getattr( + parent_model, obj.parent_field).field.related_model + parent_object = parent_model.objects.get(**params) + + context['root_pk'] = parent_object.pk - field = self.model._meta.get_field(parent_field) - parent = field.related_model.objects.get(**params) - if parent: + if parent_object: context['title'] = '%s (%s)' % ( - context['title'], parent) + context['title'], parent_object) return context + def cancel_url(self): + if self.list_url: + return self.list_url + obj = self.crud if hasattr(self, 'crud') else self + + params = {'pk': self.kwargs['pk']} + + parent_model = self.model + parent_object = None + if '__' in obj.parent_field: + fields = obj.parent_field.split('__') + for field in fields: + if parent_model == self.model: + parent_model = getattr( + parent_model, field).field.related_model + parent_object = parent_model.objects.get(**params) + else: + parent_object = getattr(parent_object, field) + break + + else: + parent_model = getattr( + parent_model, obj.parent_field).field.related_model + parent_object = parent_model.objects.get(**params) + + return reverse( + '%s:%s' % (parent_model._meta.app_config.name, + '%s_%s' % ( + parent_model._meta.model_name, + ACTION_DETAIL)), + kwargs={'pk': parent_object.pk}) + class UpdateView(Crud.UpdateView): permission_required = RP_CHANGE,