Ejemplo n.º 1
0
def _try_redirect(request, full_path=None):
    project = project_slug = None
    if hasattr(request, 'slug'):
        project_slug = request.slug
    elif full_path.startswith('/docs/'):
        split = full_path.split('/')
        if len(split) > 2:
            project_slug = split[2]
    else:
        return None

    if project_slug:
        try:
            project = Project.objects.get(slug=project_slug)
        except Project.DoesNotExist:
            return None

    if project:
        for project_redirect in project.redirects.all():
            if project_redirect.redirect_type == 'prefix':
                if full_path.startswith(project_redirect.from_url):
                    log.debug('Redirecting %s' % project_redirect)
                    cut_path = re.sub('^%s' % project_redirect.from_url, '',
                                      full_path)
                    to = redirect_filename(project=project, filename=cut_path)
                    return HttpResponseRedirect(to)
            elif project_redirect.redirect_type == 'page':
                if full_path == project_redirect.from_url:
                    log.debug('Redirecting %s' % project_redirect)
                    to = redirect_filename(
                        project=project,
                        filename=project_redirect.to_url.lstrip('/'))
                    return HttpResponseRedirect(to)
            elif project_redirect.redirect_type == 'exact':
                if full_path == project_redirect.from_url:
                    log.debug('Redirecting %s' % project_redirect)
                    return HttpResponseRedirect(project_redirect.to_url)
                # Handle full sub-level redirects
                if '$rest' in project_redirect.from_url:
                    match = project_redirect.from_url.split('$rest')[0]
                    if full_path.startswith(match):
                        cut_path = re.sub('^%s' % match,
                                          project_redirect.to_url, full_path)
                        return HttpResponseRedirect(cut_path)
            elif project_redirect.redirect_type == 'sphinx_html':
                if full_path.endswith('/'):
                    log.debug('Redirecting %s' % project_redirect)
                    to = re.sub('/$', '.html', full_path)
                    return HttpResponseRedirect(to)
            elif project_redirect.redirect_type == 'sphinx_htmldir':
                if full_path.endswith('.html'):
                    log.debug('Redirecting %s' % project_redirect)
                    to = re.sub('.html$', '/', full_path)
                    return HttpResponseRedirect(to)
    return None
Ejemplo n.º 2
0
def _try_redirect(request, full_path=None):
    project = project_slug = None
    if hasattr(request, 'slug'):
        project_slug = request.slug
    elif full_path.startswith('/docs/'):
        split = full_path.split('/')
        if len(split) > 2:
            project_slug = split[2]
    else:
        return None

    if project_slug:
        try:
            project = Project.objects.get(slug=project_slug)
        except Project.DoesNotExist:
            return None

    if project:
        for project_redirect in project.redirects.all():
            if project_redirect.redirect_type == 'prefix':
                if full_path.startswith(project_redirect.from_url):
                    log.debug('Redirecting %s' % project_redirect)
                    cut_path = re.sub('^%s' % project_redirect.from_url, '', full_path)
                    to = redirect_filename(project=project, filename=cut_path)
                    return HttpResponseRedirect(to)
            elif project_redirect.redirect_type == 'page':
                if full_path == project_redirect.from_url:
                    log.debug('Redirecting %s' % project_redirect)
                    to = redirect_filename(
                        project=project,
                        filename=project_redirect.to_url.lstrip('/'))
                    return HttpResponseRedirect(to)
            elif project_redirect.redirect_type == 'exact':
                if full_path == project_redirect.from_url:
                    log.debug('Redirecting %s' % project_redirect)
                    return HttpResponseRedirect(project_redirect.to_url)
                # Handle full sub-level redirects
                if '$rest' in project_redirect.from_url:
                    match = project_redirect.from_url.split('$rest')[0]
                    if full_path.startswith(match):
                        cut_path = re.sub('^%s' % match, project_redirect.to_url, full_path)
                        return HttpResponseRedirect(cut_path)
            elif project_redirect.redirect_type == 'sphinx_html':
                if full_path.endswith('/'):
                    log.debug('Redirecting %s' % project_redirect)
                    to = re.sub('/$', '.html', full_path)
                    return HttpResponseRedirect(to)
            elif project_redirect.redirect_type == 'sphinx_htmldir':
                if full_path.endswith('.html'):
                    log.debug('Redirecting %s' % project_redirect)
                    to = re.sub('.html$', '/', full_path)
                    return HttpResponseRedirect(to)
    return None
Ejemplo n.º 3
0
 def test_single_version_no_subdomain(self):
     self.proj.single_version = True
     self.assertEqual(
         redirect_filename(self.proj, 'faq.html'),
         reverse(
             'docs_detail',
             kwargs={
                 'project_slug': self.proj.slug,
                 'filename': 'faq.html',
             }
         )
     )
Ejemplo n.º 4
0
 def test_single_version_no_subdomain(self):
     self.proj.single_version = True
     self.assertEqual(
         redirect_filename(self.proj, 'faq.html'),
         reverse(
             'docs_detail',
             kwargs={
                 'project_slug': self.proj.slug,
                 'filename': 'faq.html',
             }
         )
     )
Ejemplo n.º 5
0
 def test_single_version_with_subdomain(self):
     self.proj.single_version = True
     self.assertEqual(
         redirect_filename(self.proj, 'faq.html'),
         '/faq.html'
     )
Ejemplo n.º 6
0
 def test_redirects_with_subdomain(self):
     self.assertEqual(
         redirect_filename(self.proj, 'faq.html'),
         '/en/latest/faq.html'
     )
Ejemplo n.º 7
0
 def test_redirects_no_subdomain(self):
     self.assertEqual(
         redirect_filename(self.proj, 'index.html'),
         '/docs/read-the-docs/en/latest/index.html'
     )
Ejemplo n.º 8
0
 def test_http_filenames_return_themselves(self):
     self.assertEqual(
         redirect_filename(None, 'http'),
         'http'
     )
Ejemplo n.º 9
0
 def test_single_version_with_subdomain(self):
     self.proj.single_version = True
     self.assertEqual(
         redirect_filename(self.proj, 'faq.html'),
         'http://read-the-docs.rtfd.org/faq.html'
     )
Ejemplo n.º 10
0
 def test_redirects_with_subdomain(self):
     self.assertEqual(
         redirect_filename(self.proj, 'faq.html'),
         'http://read-the-docs.rtfd.org/en/latest/faq.html'
     )
Ejemplo n.º 11
0
 def test_redirects_no_subdomain(self):
     self.assertEqual(
         redirect_filename(self.proj, 'index.html'),
         '/docs/read-the-docs/en/latest/index.html'
     )
Ejemplo n.º 12
0
 def test_http_filenames_return_themselves(self):
     self.assertEqual(
         redirect_filename(None, 'http'),
         'http'
     )