示例#1
0
    def _get_permission_model_ids_from_request_data(self, request, model, view):
        if not is_hal_content_type(request.content_type):
            return None

        permission_model_attr = self.get_permission_model_attr(model)
        if LINKS_FIELD_NAME in request.data and permission_model_attr in request.data[LINKS_FIELD_NAME]:
            return [get_id_from_detail_uri(request.data[LINKS_FIELD_NAME][permission_model_attr])]

        direct_parent = None
        direct_parent_id = None
        other_parent = None
        other_parent_id = None

        # there has to be a parent that holds the permission model in the parent lookups
        for kwarg_key in view.kwargs:
            if not kwarg_key.startswith(drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX):
                continue

            kwarg_key_without_prefix = kwarg_key.replace(drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX, '')
            if kwarg_key.endswith('__' + permission_model_attr):
                return [view.kwargs[kwarg_key]]
            elif '__' in kwarg_key:
                other_parent = kwarg_key_without_prefix.split('__')[0]
                if LINKS_FIELD_NAME in request.data and other_parent in request.data[LINKS_FIELD_NAME]:
                    other_parent_id = get_id_from_detail_uri(request.data[LINKS_FIELD_NAME][other_parent])
            else:
                direct_parent = kwarg_key_without_prefix
                direct_parent_id = view.kwargs[kwarg_key]

        permission_model_ids = self._get_permission_model_ids_from_parent(view.queryset.model, direct_parent, direct_parent_id)
        return permission_model_ids or self._get_permission_model_ids_from_parent(view.queryset.model, other_parent,
                                                                                  other_parent_id)
示例#2
0
def _add_parent_to_hal_request_data(request, parentKey):
    if not drf_hal_json.is_hal_content_type(request.content_type):
        return
    links = request.data.get('_links')
    if links and parentKey in links:
        return

    if not links:
        links = {}
        request.data['_links'] = links

    uriSplit = request.build_absolute_uri().split('/')
    if request.method == 'PUT':
        uriSplit = uriSplit[:-3]  # in case of PUT the id must be removed as well
    else:
        uriSplit = uriSplit[:-2]

    links[parentKey] = '/'.join(uriSplit) + '/'
示例#3
0
def _add_parent_to_hal_request_data(request, parentKey):
    if not drf_hal_json.is_hal_content_type(request.content_type):
        return
    links = request.data.get('_links')
    if links and parentKey in links:
        return

    if not links:
        links = {}
        request.data['_links'] = links

    uriSplit = request.build_absolute_uri().split('/')
    if request.method == 'PUT':
        uriSplit = uriSplit[:
                            -3]  # in case of PUT the id must be removed as well
    else:
        uriSplit = uriSplit[:-2]

    links[parentKey] = '/'.join(uriSplit) + '/'
示例#4
0
    def _get_permission_model_ids_from_request_data(self, request, model,
                                                    view):
        if not is_hal_content_type(request.content_type):
            return None

        permission_model_attr = self.get_permission_model_attr(model)
        if LINKS_FIELD_NAME in request.data and permission_model_attr in request.data[
                LINKS_FIELD_NAME]:
            return [
                get_id_from_detail_uri(
                    request.data[LINKS_FIELD_NAME][permission_model_attr])
            ]

        direct_parent = None
        direct_parent_id = None
        other_parent = None
        other_parent_id = None

        # there has to be a parent that holds the permission model in the parent lookups
        for kwarg_key in view.kwargs:
            if not kwarg_key.startswith(
                    drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX):
                continue

            kwarg_key_without_prefix = kwarg_key.replace(
                drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX, '')
            if kwarg_key.endswith('__' + permission_model_attr):
                return [view.kwargs[kwarg_key]]
            elif '__' in kwarg_key:
                other_parent = kwarg_key_without_prefix.split('__')[0]
                if LINKS_FIELD_NAME in request.data and other_parent in request.data[
                        LINKS_FIELD_NAME]:
                    other_parent_id = get_id_from_detail_uri(
                        request.data[LINKS_FIELD_NAME][other_parent])
            else:
                direct_parent = kwarg_key_without_prefix
                direct_parent_id = view.kwargs[kwarg_key]

        permission_model_ids = self._get_permission_model_ids_from_parent(
            view.queryset.model, direct_parent, direct_parent_id)
        return permission_model_ids or self._get_permission_model_ids_from_parent(
            view.queryset.model, other_parent, other_parent_id)