Exemple #1
0
    def define_route(self, path, handler, methods, name=None):
        """
        Define a router as instance of BaseRoute subclass, which passed
        from register method in RestWSRouter.

        :param path: URL, which used to get access to API.
        :param handler: class inherited from MethodBasedView, which used for
                        processing request.
        :param methods: list of available for user methods or string with
                        concrete method name.
        :param name: the base to use for the URL names that are created.
        """
        # it's PlainRoute, when don't have any "dynamic symbols"
        if all(symbol not in path for symbol in ['{', '}']):
            return PlainEndpoint(path, handler, methods, name)

        # Try to processing as a dynamic path
        pattern = ''
        for part in DYNAMIC_PARAMETER.split(path):
            match = VALID_DYNAMIC_PARAMETER.match(part)
            if match:
                pattern += '(?P<{}>{})'.format(match.group('var'), ANY_VALUE)
                continue

            if any(symbol in part for symbol in ['{', '}']):
                raise EndpointValueError("Invalid {} part of {} path".format(part, path))  # NOQA

            pattern += re.escape(part)
        try:
            compiled = re.compile("^{}$".format(pattern))
        except re.error as exc:
            raise EndpointValueError("Bad pattern '{}': {}".format(pattern, exc))  # NOQA
        return DynamicEndpoint(path, handler, methods, name, compiled)
Exemple #2
0
 def setUp(self):
     super(DynamicEndpointTestCase, self).setUp()
     self.endpoint = DynamicEndpoint(
         '/api/{another}/', MethodBasedView, 'GET', None,
         re.compile("^{}$".format(r'/api/(?P<var>[^{}/]+)/')))