Ejemplo n.º 1
0
    def get_url_patterns(self):
        """Returns the Django URL patterns for this object and its children.

        This is used to automatically build up the URL hierarchy for all
        objects. Projects should call this for top-level resources and
        return them in the ``urls.py`` files.
        """
        urlpatterns = never_cache_patterns(
            '',
            url(r'^$', self, name=self._build_named_url(self.name_plural)),
        )

        for resource in self.list_child_resources:
            resource._parent_resource = self
            child_regex = r'^' + resource.uri_name + r'/'
            urlpatterns += patterns(
                '',
                url(child_regex, include(resource.get_url_patterns())),
            )

        if self.uri_object_key or self.singleton:
            # If the resource has particular items in it...
            if self.uri_object_key:
                base_regex = r'^(?P<%s>%s)/' % (self.uri_object_key,
                                                self.uri_object_key_regex)
            elif self.singleton:
                base_regex = r'^'

            urlpatterns += never_cache_patterns(
                '',
                url(base_regex + r'$', self,
                    name=self._build_named_url(self.name))
            )

            for resource in self.item_child_resources:
                resource._parent_resource = self
                child_regex = base_regex + resource.uri_name + r'/'
                urlpatterns += patterns(
                    '',
                    url(child_regex, include(resource.get_url_patterns())),
                )

        return urlpatterns
Ejemplo n.º 2
0
    def get_url_patterns(self):
        """Returns the Django URL patterns for this object and its children.

        This is used to automatically build up the URL hierarchy for all
        objects. Projects should call this for top-level resources and
        return them in the ``urls.py`` files.
        """
        urlpatterns = never_cache_patterns(
            '',
            url(r'^$', self, name=self._build_named_url(self.name_plural)),
        )

        for resource in self.list_child_resources:
            resource._parent_resource = self
            child_regex = r'^' + resource.uri_name + r'/'
            urlpatterns += patterns(
                '',
                url(child_regex, include(resource.get_url_patterns())),
            )

        if self.uri_object_key or self.singleton:
            # If the resource has particular items in it...
            if self.uri_object_key:
                base_regex = r'^(?P<%s>%s)/' % (self.uri_object_key,
                                                self.uri_object_key_regex)
            elif self.singleton:
                base_regex = r'^'

            urlpatterns += never_cache_patterns(
                '',
                url(base_regex + r'$',
                    self,
                    name=self._build_named_url(self.name)))

            for resource in self.item_child_resources:
                resource._parent_resource = self
                child_regex = base_regex + resource.uri_name + r'/'
                urlpatterns += patterns(
                    '',
                    url(child_regex, include(resource.get_url_patterns())),
                )

        return urlpatterns
Ejemplo n.º 3
0
    def get_url_patterns(self):
        """Return the Django URL patterns for this object and its children.

        This returns the same list as
        :py:meth:`WebAPIResource.get_url_patterns`, but also introduces a
        generic catch-all 404 handler which returns API errors instead of HTML.
        """
        urlpatterns = super(RootResource, self).get_url_patterns()
        urlpatterns += never_cache_patterns(url(r'.*', self.api_404_handler))

        return urlpatterns
Ejemplo n.º 4
0
    def get_url_patterns(self):
        """Return the Django URL patterns for this object and its children.

        This returns the same list as
        :py:meth:`WebAPIResource.get_url_patterns`, but also introduces a
        generic catch-all 404 handler which returns API errors instead of HTML.
        """
        urlpatterns = super(RootResource, self).get_url_patterns()
        urlpatterns += never_cache_patterns(
            '', url(r'.*', self.api_404_handler))

        return urlpatterns
Ejemplo n.º 5
0
    def test_never_cache_patterns_with_prefix(self):
        """Testing never_cache_patterns with view lookup string prefix"""
        msg = (
            'String prefixes for URLs in never_cache_patterns() is '
            'deprecated, and will not work on Django 1.10 or higher.'
        )

        if django.VERSION[:2] >= (1, 10):
            with self.assertRaisesMessage(ValueError, msg):
                never_cache_patterns(
                    'djblets.urls.tests',

                    url('^a/$', dummy_view),
                )
        else:
            self.spy_on(never_cache)

            with warnings.catch_warnings(record=True) as w:
                urlpatterns = never_cache_patterns(
                    'djblets.urls.tests',

                    url('^a/$', 'dummy_view'),
                    url('^b/$', 'dummy_view'),
                )

            self.assertEqual(six.text_type(w[-1].message), msg)
            self.assertEqual(len(never_cache.spy.calls), 2)
            self.assertEqual(len(urlpatterns), 2)

            pattern = urlpatterns[0]
            self.assertEqual(pattern._callback_str,
                             'djblets.urls.tests.dummy_view')
            self.assertTrue(
                never_cache.spy.calls[0].returned(pattern.callback))

            pattern = urlpatterns[1]
            self.assertEqual(pattern._callback_str,
                             'djblets.urls.tests.dummy_view')
            self.assertTrue(
                never_cache.spy.calls[1].returned(pattern.callback))
Ejemplo n.º 6
0
    def test_never_cache_patterns_with_prefix(self):
        """Testing never_cache_patterns with view lookup string prefix"""
        msg = ('String prefixes for URLs in never_cache_patterns() is '
               'deprecated, and will not work on Django 1.10 or higher.')

        if django.VERSION[:2] >= (1, 10):
            with self.assertRaisesMessage(ValueError, msg):
                never_cache_patterns(
                    'djblets.urls.tests',
                    url('^a/$', dummy_view),
                )
        else:
            self.spy_on(never_cache)

            with warnings.catch_warnings(record=True) as w:
                urlpatterns = never_cache_patterns(
                    'djblets.urls.tests',
                    url('^a/$', 'dummy_view'),
                    url('^b/$', 'dummy_view'),
                )

            self.assertEqual(six.text_type(w[-1].message), msg)
            self.assertEqual(len(never_cache.spy.calls), 2)
            self.assertEqual(len(urlpatterns), 2)

            pattern = urlpatterns[0]
            self.assertEqual(pattern._callback_str,
                             'djblets.urls.tests.dummy_view')
            self.assertTrue(never_cache.spy.calls[0].returned(
                pattern.callback))

            pattern = urlpatterns[1]
            self.assertEqual(pattern._callback_str,
                             'djblets.urls.tests.dummy_view')
            self.assertTrue(never_cache.spy.calls[1].returned(
                pattern.callback))
Ejemplo n.º 7
0
    def test_never_cache_patterns(self):
        """Testing never_cache_patterns"""
        self.spy_on(never_cache)

        urlpatterns = never_cache_patterns(
            url('^a/$', dummy_view),
            url('^b/$', dummy_view),
        )

        self.assertEqual(len(never_cache.spy.calls), 2)
        self.assertEqual(len(urlpatterns), 2)

        self.assertTrue(
            never_cache.spy.calls[0].returned(urlpatterns[0].callback))
        self.assertTrue(
            never_cache.spy.calls[1].returned(urlpatterns[1].callback))
Ejemplo n.º 8
0
    def test_never_cache_patterns_without_prefix(self):
        """Testing never_cache_patterns without view lookup string prefix"""
        self.spy_on(never_cache)

        with warnings.catch_warnings(record=True) as w:
            urlpatterns = never_cache_patterns(
                url('^a/$', dummy_view),
                url('^b/$', dummy_view),
            )

        self.assertEqual(len(w), 0)
        self.assertEqual(len(never_cache.spy.calls), 2)
        self.assertEqual(len(urlpatterns), 2)

        self.assertTrue(never_cache.spy.calls[0].returned(
            urlpatterns[0].callback))
        self.assertTrue(never_cache.spy.calls[1].returned(
            urlpatterns[1].callback))
Ejemplo n.º 9
0
    def test_never_cache_patterns_without_prefix(self):
        """Testing never_cache_patterns without view lookup string prefix"""
        self.spy_on(never_cache)

        with warnings.catch_warnings(record=True) as w:
            urlpatterns = never_cache_patterns(
                url('^a/$', dummy_view),
                url('^b/$', dummy_view),
            )

        self.assertEqual(len(w), 0)
        self.assertEqual(len(never_cache.spy.calls), 2)
        self.assertEqual(len(urlpatterns), 2)

        self.assertTrue(
            never_cache.spy.calls[0].returned(urlpatterns[0].callback))
        self.assertTrue(
            never_cache.spy.calls[1].returned(urlpatterns[1].callback))