示例#1
0
def check_settings(app_configs, **kwargs):
    errors = []
    r = RoutePattern(settings.PATH) if settings.TYPE_PATTERN == settings.ROUTE else RegexPattern(settings.PATH)
    invalid_keys = [arg for arg in settings.PATH_REQUIRED_ARGS if arg not in r.regex.groupindex.keys()]
    if invalid_keys:
        errors.append(
            Error(
                'PATH in settings does not contains: ' + ', '.join(invalid_keys),
                obj=settings.PATH
            )
        )
    if not getattr(app_settings, 'MEDIA_ROOT', None):
        errors.append(Error('Set MEDIA_ROOT in settings'))
    return errors
示例#2
0
def detype_pattern(pattern):
    """
    return an equivalent pattern that accepts arbitrary values for path parameters.
    de-typing the path will ease determining a matching route without having properly
    formatted dummy values for all path parameters.
    """
    if isinstance(pattern, URLResolver):
        return URLResolver(
            pattern=detype_pattern(pattern.pattern),
            urlconf_name=[detype_pattern(p) for p in pattern.url_patterns],
            default_kwargs=pattern.default_kwargs,
            app_name=pattern.app_name,
            namespace=pattern.namespace,
        )
    elif isinstance(pattern, URLPattern):
        return URLPattern(
            pattern=detype_pattern(pattern.pattern),
            callback=pattern.callback,
            default_args=pattern.default_args,
            name=pattern.name,
        )
    elif isinstance(pattern, RoutePattern):
        return RoutePattern(
            route=re.sub(r'<\w+:(\w+)>', r'<\1>', pattern._route),
            name=pattern.name,
            is_endpoint=pattern._is_endpoint
        )
    elif isinstance(pattern, RegexPattern):
        detyped_regex = pattern._regex
        for name, regex in analyze_named_regex_pattern(pattern._regex).items():
            detyped_regex = detyped_regex.replace(
                f'(?P<{name}>{regex})',
                f'(?P<{name}>[^/]+)',
            )
        return RegexPattern(
            regex=detyped_regex,
            name=pattern.name,
            is_endpoint=pattern._is_endpoint
        )
    else:
        warn(f'unexpected pattern "{pattern}" encountered while simplifying urlpatterns.')
        return pattern
示例#3
0
 def test_str(self):
     self.assertEqual(str(RoutePattern(_('translated/'))), 'translated/')
示例#4
0
from django.conf import settings
from django.urls import path, reverse, URLPattern, URLResolver
from django.urls.resolvers import RoutePattern

os.environ['DJANGO_SETTINGS_MODULE'] = 'urlnamespacetest.settings'


def test_natural():
    expected = '/crudlfapexampleappexamplemodel/list'
    res = reverse('crudlfap:exampleapp:examplemodel:list')
    assert res == expected


crudlfap = URLResolver(
    pattern=RoutePattern('crudlfap/'),
    urlconf_name=[],
    namespace='crudlfap',
    app_name='crudlfap',
)
exampleapp = URLResolver(
    pattern=RoutePattern('exampleapp/'),
    urlconf_name=[],
    namespace='exampleapp',
    app_name='exampleapp',
)
examplemodel = URLResolver(
    pattern=RoutePattern('examplemodel/'),
    urlconf_name=[],
    namespace='examplemodel',
    app_name='examplemodel',
示例#5
0
 def test_str(self):
     self.assertEqual(str(RoutePattern(_("translated/"))), "translated/")
示例#6
0
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.urls.resolvers import RoutePattern
from ws4redis.publisher import RedisPublisher
from ws4redis.redis_store import RedisStore, SELF
from ws4redis.subscriber import RedisSubscriber

events_route = RoutePattern('ws/<int:project_id>/events')


# noinspection PyUnusedLocal
def get_allowed_channels(request, channels):
    try:
        if not request.user.is_authenticated:
            raise PermissionDenied("Please log in")

        path_info = request.environ.get('PATH_INFO', '').lstrip('/')

        events = events_route.match(path_info)
        if events:
            extra_path, _args, kwargs = events
            if extra_path != '':
                # We don't accept trailing garbage
                raise Http404

            authorized = request.session.get('authorized_exercises', [])
            if kwargs['project_id'] in authorized:
                return {'subscribe-broadcast', 'publish-server'}

            raise PermissionDenied("No access to this exercise")