Esempio n. 1
0
def can_url(user, view):
    """
    Tests whether the given user would have access to the view. The view can
    be a callable, importable text path or a view name, possibly with the
    namespace prefix ('namespace:view_name'). The view function must be
    decorated with the can_url_func (that's what UrlPatterns class does).
    """
    if ':' not in view:
        view = get_callable(view)

    if not callable(view):
        callbacks = get_all_callbacks(get_urlconf())
        if view not in callbacks:
            raise NoReverseMatch("Reverse for '%s' not found." % view)

        view = callbacks[view]

    if not hasattr(view, 'can_url_func'):
        raise NoReverseMatch(
            "Reverse for '%s' is not decorated with permissions." % view)

    try:
        return view.can_url_func(user)
    except PermissionDenied:
        return False
    def setUp(self):
        self.owner = create_user(username='******', password='******')
        self.domain = 'pip.dev.readthedocs.io'
        self.pip = get(
            Project,
            name='pip',
            slug='pip',
            users=[self.owner],
            privacy_level='public',
            urlconf='subpath/$subproject/$version/$language/$filename'  # Flipped
        )
        self.subproject = get(
            Project,
            name='subproject',
            slug='subproject',
            users=[self.owner],
            privacy_level='public',
            main_language_project=None,
        )
        self.relationship = get(
            ProjectRelationship,
            parent=self.pip,
            child=self.subproject,
        )

        self.old_urlconf = get_urlconf()
        sys.modules['fake_urlconf'] = self.pip.proxito_urlconf
        set_urlconf('fake_urlconf')
Esempio n. 3
0
    def test_restore_urlconf_after_request(self):
        """
        The urlconf attribute for the current thread
        should remain intact after each request,
        When is set to None it means 'use default from settings'.
        """
        set_urlconf(None)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)
    def test_restore_urlconf_after_request(self):
        """
        The urlconf attribute for the current thread
        should remain intact after each request,
        When is set to None it means 'use default from settings'.
        """
        set_urlconf(None)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)
    def setUp(self):
        self.owner = create_user(username='******', password='******')
        self.domain = 'pip.dev.readthedocs.io'
        self.pip = get(
            Project,
            slug='pip',
            users=[self.owner],
            privacy_level='public',
            urlconf='subpath/to/$version/$language/$filename'  # Flipped
        )

        self.old_urlconf = get_urlconf()
        sys.modules['fake_urlconf'] = self.pip.proxito_urlconf
        set_urlconf('fake_urlconf')
Esempio n. 6
0
    def should_skip_cache(self, request):
        req_info = resolve(request.path, urlconf=get_urlconf())

        disable = getattr(req_info.func, DISABLE_CACHE_ATTR, False)
        if disable:
            return True

        if request.user.is_authenticated:
            return True

        # Django Rest Framework authentication happens at view-level, not middleware
        # So, any specific authentication, such as TokenAuthentication will run after that
        # This cache ignores API views.
        view_class = getattr(req_info.func, "cls", None)
        drf_views = (rest_framework.views.APIView,
                     rest_framework.viewsets.ViewSet)
        if view_class and issubclass(view_class, drf_views):
            return True

        return False