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
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
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', }))
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', } ) )
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
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
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')
def test_redirects_with_subdomain(self): self.assertEqual(redirect_filename(self.proj, 'faq.html'), 'http://read-the-docs.rtfd.org/en/latest/faq.html')
def test_redirects_no_subdomain(self): self.assertEqual(redirect_filename(self.proj, 'index.html'), '/docs/read-the-docs/en/latest/index.html')
def test_http_filenames_return_themselves(self): self.assertEqual(redirect_filename(None, 'http'), 'http')
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' )
def test_redirects_with_subdomain(self): self.assertEqual( redirect_filename(self.proj, 'faq.html'), 'http://read-the-docs.rtfd.org/en/latest/faq.html' )
def test_redirects_no_subdomain(self): self.assertEqual( redirect_filename(self.proj, 'index.html'), '/docs/read-the-docs/en/latest/index.html' )
def test_http_filenames_return_themselves(self): self.assertEqual( redirect_filename(None, 'http'), 'http' )