示例#1
0
def consume(app, request):
    """Consume path segments from request to find model obj.

    Removes the successfully consumed path segments from
    :attr:`morepath.Request.unconsumed`.

    Uses :meth:`morepath.traject.Traject.consume` to consume path
    segments according to path configuration.

    Extracts URL parameters from the path.

    Gets a factory function and uses matched path variables and URL parameters
    to construct the model instance (or :class:`morepath.App` instance).

    :param app: the :class:`morepath.App` instance that contains the
      path registry to use.
    :param request: :class:`morepath.Request` instance that contains the
      path segments to consume.
    :return: The new model object, or a mounted :class:`morepath.App`
      instance, or ``None`` if no new instance could be found.
    """
    value, stack, traject_variables = app.config.path_registry.consume(
        request.unconsumed)
    if value is None:
        return None
    get_obj, get_parameters = value
    variables = get_parameters(request.GET)
    variables['request'] = request
    variables['app'] = app
    variables.update(traject_variables)
    next_obj = mapply(get_obj, **variables)
    if next_obj is None:
        return None
    request.unconsumed = stack
    return next_obj
示例#2
0
def consume(app, request):
    """Consume path segments from request to find model obj.

    Removes the successfully consumed path segments from
    :attr:`morepath.Request.unconsumed`.

    Uses :meth:`morepath.traject.Traject.consume` to consume path
    segments according to path configuration.

    Extracts URL parameters from the path.

    Gets a factory function and uses matched path variables and URL parameters
    to construct the model instance (or :class:`morepath.App` instance).

    :param app: the :class:`morepath.App` instance that contains the
      path registry to use.
    :param request: :class:`morepath.Request` instance that contains the
      path segments to consume.
    :return: The new model object, or a mounted :class:`morepath.App`
      instance, or ``None`` if no new instance could be found.
    """
    value, stack, traject_variables = app.config.path_registry.consume(
        request.unconsumed)
    if value is None:
        return None
    get_obj, get_parameters = value
    variables = get_parameters(request.GET)
    variables['request'] = request
    variables['app'] = app
    variables.update(traject_variables)
    next_obj = mapply(get_obj, **variables)
    if next_obj is None:
        return None
    request.unconsumed = stack
    return next_obj
示例#3
0
def traject_consume(request, app, lookup):
    traject = app.traject
    value, stack, traject_variables = traject.consume(request.unconsumed)
    if value is None:
        return None
    get_obj, get_parameters = value
    variables = get_parameters(request.GET)
    variables['request'] = request
    variables['app'] = app
    variables.update(traject_variables)
    next_obj = mapply(get_obj, **variables)
    if next_obj is None:
        return None
    request.unconsumed = stack
    return next_obj
示例#4
0
文件: core.py 项目: webmaven/morepath
def traject_consume(request, model, lookup):
    traject = generic.traject(model, lookup=lookup, default=None)
    if traject is None:
        return None
    get_model, stack, traject_variables = traject(request.unconsumed)
    if get_model is None:
        return None
    variables = generic.context(model, default={}, lookup=lookup)
    variables['base'] = model
    variables['request'] = request
    variables.update(traject_variables)
    next_model = mapply(get_model, **variables)
    if next_model is None:
        return None
    request.unconsumed = stack
    return next_model
示例#5
0
def traject_consume(request, model, lookup):
    traject = generic.traject(model, lookup=lookup, default=None)
    if traject is None:
        return None
    value, stack, traject_variables = traject.consume(request.unconsumed)
    if value is None:
        return None
    get_model, get_parameters = value
    variables = get_parameters(request.args)
    variables.update(generic.context(model, default={}, lookup=lookup))
    variables['parent'] = model
    variables['request'] = request
    variables.update(traject_variables)
    next_model = mapply(get_model, **variables)
    if next_model is None:
        return None
    request.unconsumed = stack
    return next_model
示例#6
0
def consume(request, app):
    """Consume request.unconsumed to new obj, starting with app.

    Returns the new model instance, or None if no new instance could be found.
    The model instance may be an app instance.

    Adjusts request.unconsumed with the remaining unconsumed stack.
    """
    value, stack, traject_variables = app.traject.consume(request.unconsumed)
    if value is None:
        return None
    get_obj, get_parameters = value
    variables = get_parameters(request.GET)
    variables['request'] = request
    variables['app'] = app
    variables.update(traject_variables)
    next_obj = mapply(get_obj, **variables)
    if next_obj is None:
        return None
    request.unconsumed = stack
    return next_obj
示例#7
0
    def register_identity_policy_function(self, factory, dispatch, name):
        """Register a method from the identity policy as a function.

        The identity policy is registered as a class, but their methods
        are really registered with dispatch functions that are then
        exposed to the public API and are used in the framework.

        :param factory: factory to create identity policy instance,
          typically the identity policy class object.
        :param dispatch: the dispatch function we want to register
          a method on.
        :param name: the name of the method to register.
        """
        # make sure we only have a single identity policy
        identity_policy = self.identity_policy
        if identity_policy is None:
            self.identity_policy = identity_policy = mapply(
                factory,
                settings=self.setting_registry)
        func = getattr(identity_policy, name)
        message = (
            "DEPRECATED. morepath.{0}_identity is deprecated. "
            "Use the morepath.App.{0}_identity method instead.".format(name))
        if name == 'remember':

            @wraps(func)
            def wrapper(response, request, identity):
                warnings.warn(message, DeprecationWarning)
                return func(response, request, identity)

        elif name == 'forget':

            @wraps(func)
            def wrapper(response, request):
                warnings.warn(message, DeprecationWarning)
                return func(response, request)

        else:
            wrapper = func
        self.reg_registry.register_function(dispatch, wrapper)
示例#8
0
    def register_identity_policy_function(self, factory, dispatch, name):
        """Register a method from the identity policy as a function.

        The identity policy is registered as a class, but their methods
        are really registered with dispatch functions that are then
        exposed to the public API and are used in the framework.

        :param factory: factory to create identity policy instance,
          typically the identity policy class object.
        :param dispatch: the dispatch function we want to register
          a method on.
        :param name: the name of the method to register.
        """
        # make sure we only have a single identity policy
        identity_policy = self.identity_policy
        if identity_policy is None:
            self.identity_policy = identity_policy = mapply(
                factory,
                settings=self.setting_registry)
        self.reg_registry.register_function(
            dispatch,
            getattr(identity_policy, name))
示例#9
0
    def register_identity_policy_function(self, factory, dispatch, name):
        """Register a method from the identity policy as a function.

        The identity policy is registered as a class, but their methods
        are really registered with dispatch functions that are then
        exposed to the public API and are used in the framework.

        :param factory: factory to create identity policy instance,
          typically the identity policy class object.
        :param dispatch: the dispatch function we want to register
          a method on.
        :param name: the name of the method to register.
        """
        # make sure we only have a single identity policy
        identity_policy = self.identity_policy
        if identity_policy is None:
            self.identity_policy = identity_policy = mapply(
                factory, settings=self.setting_registry)
        func = getattr(identity_policy, name)
        message = (
            "DEPRECATED. morepath.{0}_identity is deprecated. "
            "Use the morepath.App.{0}_identity method instead.".format(name))
        if name == 'remember':

            @wraps(func)
            def wrapper(response, request, identity):
                warnings.warn(message, DeprecationWarning)
                return func(response, request, identity)

        elif name == 'forget':

            @wraps(func)
            def wrapper(response, request):
                warnings.warn(message, DeprecationWarning)
                return func(response, request)

        else:
            wrapper = func
        self.reg_registry.register_function(dispatch, wrapper)
示例#10
0
 def create_context(self):
     return mapply(self.context_factory, **self.variables)
示例#11
0
 def perform(self, registry, obj):
     app = self.app
     policy = mapply(obj, settings=app.registry.settings)
     registry.register_function(generic.identify, policy.identify)
     registry.register_function(generic.remember_identity, policy.remember)
     registry.register_function(generic.forget_identity, policy.forget)