Exemplo 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 redirect in project.redirects.all():
            if redirect.redirect_type == 'prefix':
                if full_path.startswith(redirect.from_url):
                    log.debug('Redirecting %s' % redirect)
                    cut_path = re.sub('^%s' % redirect.from_url, '', full_path)
                    to = redirect_filename(project=project, filename=cut_path)
                    return HttpResponseRedirect(to)
            elif redirect.redirect_type == 'page':
                if full_path == redirect.from_url:
                    log.debug('Redirecting %s' % redirect)
                    to = redirect_filename(
                        project=project, filename=redirect.to_url.lstrip('/'))
                    return HttpResponseRedirect(to)
            elif redirect.redirect_type == 'exact':
                if full_path == redirect.from_url:
                    log.debug('Redirecting %s' % redirect)
                    return HttpResponseRedirect(redirect.to_url)
                # Handle full sub-level redirects
                if '$rest' in redirect.from_url:
                    match = redirect.from_url.split('$rest')[0]
                    if full_path.startswith(match):
                        cut_path = re.sub('^%s' % match, redirect.to_url,
                                          full_path)
                        return HttpResponseRedirect(cut_path)
            elif redirect.redirect_type == 'sphinx_html':
                if full_path.endswith('/'):
                    log.debug('Redirecting %s' % redirect)
                    to = re.sub('/$', '.html', full_path)
                    return HttpResponseRedirect(to)
            elif redirect.redirect_type == 'sphinx_htmldir':
                if full_path.endswith('.html'):
                    log.debug('Redirecting %s' % redirect)
                    to = re.sub('.html$', '/', full_path)
                    return HttpResponseRedirect(to)
    return None
Exemplo 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 redirect in project.redirects.all():
            if redirect.redirect_type == 'prefix':
                if full_path.startswith(redirect.from_url):
                    log.debug('Redirecting %s' % redirect)
                    cut_path = re.sub('^%s' % redirect.from_url, '', full_path)
                    to = redirect_filename(project=project, filename=cut_path)
                    return HttpResponseRedirect(to)
            elif redirect.redirect_type == 'page':
                if full_path == redirect.from_url:
                    log.debug('Redirecting %s' % redirect)
                    to = redirect_filename(project=project, filename=redirect.to_url.lstrip('/'))
                    return HttpResponseRedirect(to)
            elif redirect.redirect_type == 'exact':
                if full_path == redirect.from_url:
                    log.debug('Redirecting %s' % redirect)
                    return HttpResponseRedirect(redirect.to_url)
                # Handle full sub-level redirects
                if '$rest' in redirect.from_url:
                    match = redirect.from_url.split('$rest')[0]
                    if full_path.startswith(match):
                        cut_path = re.sub('^%s' % match, redirect.to_url, full_path)
                        return HttpResponseRedirect(cut_path)
            elif redirect.redirect_type == 'sphinx_html':
                if full_path.endswith('/'):
                    log.debug('Redirecting %s' % redirect)
                    to = re.sub('/$', '.html', full_path)
                    return HttpResponseRedirect(to)
            elif redirect.redirect_type == 'sphinx_htmldir':
                if full_path.endswith('.html'):
                    log.debug('Redirecting %s' % redirect)
                    to = re.sub('.html$', '/', full_path)
                    return HttpResponseRedirect(to)
    return None
Exemplo 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',
                 }))
Exemplo 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',
             }
         )
     )
Exemplo n.º 5
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 redirect in project.redirects.all():
            if redirect.redirect_type == 'prefix':
                if full_path.startswith(redirect.from_url):
                    log.debug('Redirecting %s' % redirect)
                    cut_path = re.sub('^%s' % redirect.from_url, '', full_path)
                    to = redirect_filename(project=project, filename=cut_path)
                    return HttpResponseRedirect(to)
            if redirect.redirect_type == 'sphinx_html':
                if full_path.endswith('/'):
                    log.debug('Redirecting %s' % redirect)
                    to = re.sub('/$', '.html', full_path)
                    return HttpResponseRedirect(to)
            if redirect.redirect_type == 'sphinx_htmldir':
                if full_path.endswith('.html'):
                    log.debug('Redirecting %s' % redirect)
                    to = re.sub('.html$', '/', full_path)
                    return HttpResponseRedirect(to)
    return None
Exemplo n.º 6
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 redirect in project.redirects.all():
            if redirect.redirect_type == 'prefix':
                if full_path.startswith(redirect.from_url):
                    log.debug('Redirecting %s' % redirect)
                    cut_path = re.sub('^%s' % redirect.from_url, '', full_path)
                    to = redirect_filename(project=project, filename=cut_path)
                    return HttpResponseRedirect(to)
            if redirect.redirect_type == 'sphinx_html':
                if full_path.endswith('/'):
                    log.debug('Redirecting %s' % redirect)
                    to = re.sub('/$', '.html', full_path)
                    return HttpResponseRedirect(to)
            if redirect.redirect_type == 'sphinx_htmldir':
                if full_path.endswith('.html'):
                    log.debug('Redirecting %s' % redirect)
                    to = re.sub('.html$', '/', full_path)
                    return HttpResponseRedirect(to)
    return None
Exemplo n.º 7
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')
Exemplo n.º 8
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')
Exemplo n.º 9
0
 def test_redirects_no_subdomain(self):
     self.assertEqual(redirect_filename(self.proj, 'index.html'),
                      '/docs/read-the-docs/en/latest/index.html')
Exemplo n.º 10
0
 def test_http_filenames_return_themselves(self):
     self.assertEqual(redirect_filename(None, 'http'), 'http')
Exemplo n.º 11
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'
     )
Exemplo n.º 12
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'
     )
Exemplo n.º 13
0
 def test_redirects_no_subdomain(self):
     self.assertEqual(
         redirect_filename(self.proj, 'index.html'),
         '/docs/read-the-docs/en/latest/index.html'
     )
Exemplo n.º 14
0
 def test_http_filenames_return_themselves(self):
     self.assertEqual(
         redirect_filename(None, 'http'),
         'http'
     )