Esempio n. 1
0
def _serve_symlink_docs(request, project, privacy_level, filename=''):
    """Serve a file by symlink, or a 404 if not found."""
    # Handle indexes
    if filename == '' or filename[-1] == '/':
        filename += 'index.html'

    # This breaks path joining, by ignoring the root when given an "absolute" path
    if filename[0] == '/':
        filename = filename[1:]

    log.info('Serving %s for %s', filename, project)

    files_tried = []

    if (settings.DEBUG or constants.PUBLIC in settings.SERVE_DOCS) and privacy_level != constants.PRIVATE:  # yapf: disable  # noqa
        public_symlink = PublicSymlink(project)
        basepath = public_symlink.project_root
        if os.path.exists(os.path.join(basepath, filename)):
            return _serve_file(request, filename, basepath)

        files_tried.append(os.path.join(basepath, filename))

    if (settings.DEBUG or constants.PRIVATE in settings.SERVE_DOCS) and privacy_level == constants.PRIVATE:  # yapf: disable  # noqa
        # Handle private
        private_symlink = PrivateSymlink(project)
        basepath = private_symlink.project_root

        if os.path.exists(os.path.join(basepath, filename)):
            return _serve_file(request, filename, basepath)

        files_tried.append(os.path.join(basepath, filename))

    raise Http404(
        'File not found. Tried these files: {}'.format(
            ','.join(files_tried)), )
Esempio n. 2
0
 def setUp(self):
     self.project = get(Project, slug='kong', privacy_level='private')
     self.subproject = get(Project, slug='sub', privacy_level='private')
     self.symlink = PrivateSymlink(self.project)
     self.args = {
         'web_root': self.symlink.WEB_ROOT,
         'subproject_root': self.symlink.subproject_root,
     }
     self.commands = []
Esempio n. 3
0
 def setUp(self):
     self.project = get(Project, slug='kong', privacy_level='private')
     self.stable = get(Version,
                       slug='stable',
                       verbose_name='stable',
                       active=True,
                       project=self.project,
                       privacy_level='private')
     self.project.versions.filter(slug='latest').update(
         privacy_level='private')
     self.symlink = PrivateSymlink(self.project)
     self.args = {
         'project_root': self.symlink.project_root,
         'latest_path': self.project.rtd_build_path('latest'),
         'stable_path': self.project.rtd_build_path('stable'),
     }
     self.commands = []
Esempio n. 4
0
    def resolve_404_path(project,
                         version_slug=None,
                         language=None,
                         filename='404.html'):
        """
        Helper to resolve the path of ``404.html`` for project.

        The resolution is based on ``project`` object, version slug and
        language.

        :returns: tuple containing the (basepath, filename)
        :rtype: tuple
        """
        filename = resolve_path(
            project,
            version_slug=version_slug,
            language=language,
            filename=filename,
            subdomain=
            True,  # subdomain will make it a "full" path without a URL prefix
        )

        # This breaks path joining, by ignoring the root when given an "absolute" path
        if filename[0] == '/':
            filename = filename[1:]

        version = None
        if version_slug:
            version_qs = project.versions.filter(slug=version_slug)
            if version_qs.exists():
                version = version_qs.first()

        private = any([
            version and version.privacy_level == PRIVATE,
            not version and project.privacy_level == PRIVATE,
        ])
        if private:
            symlink = PrivateSymlink(project)
        else:
            symlink = PublicSymlink(project)
        basepath = symlink.project_root
        fullpath = os.path.join(basepath, filename)
        return (basepath, filename, fullpath)
Esempio n. 5
0
def _serve_symlink_docs(request, project, privacy_level, filename=''):

    # Handle indexes
    if filename == '' or filename[-1] == '/':
        filename += 'index.html'

    # This breaks path joining, by ignoring the root when given an "absolute" path
    if filename[0] == '/':
        filename = filename[1:]

    log.info('Serving %s for %s' % (filename, project))

    files_tried = []

    serve_docs = getattr(settings, 'SERVE_DOCS', [constants.PRIVATE])

    if (settings.DEBUG or constants.PUBLIC
            in serve_docs) and privacy_level != constants.PRIVATE:
        public_symlink = PublicSymlink(project)
        basepath = public_symlink.project_root
        if os.path.exists(os.path.join(basepath, filename)):
            return _serve_file(request, filename, basepath)
        else:
            files_tried.append(os.path.join(basepath, filename))

    if (settings.DEBUG or constants.PRIVATE
            in serve_docs) and privacy_level == constants.PRIVATE:

        # Handle private
        private_symlink = PrivateSymlink(project)
        basepath = private_symlink.project_root

        if os.path.exists(os.path.join(basepath, filename)):
            if not AdminPermission.is_member(user=request.user, obj=project):
                return _serve_401(request, project)
            return _serve_file(request, filename, basepath)
        else:
            files_tried.append(os.path.join(basepath, filename))

    raise Http404('File not found. Tried these files: %s' %
                  ','.join(files_tried))
Esempio n. 6
0
 def setUp(self):
     self.project = get(Project, slug='kong', privacy_level='private')
     self.translation = get(Project, slug='pip', privacy_level='private')
     self.translation.language = 'de'
     self.translation.main_language_project = self.project
     self.project.translations.add(self.translation)
     self.translation.save()
     self.project.save()
     self.symlink = PrivateSymlink(self.project)
     get(Version, verbose_name='master', active=True, project=self.project)
     get(Version,
         verbose_name='master',
         active=True,
         project=self.translation)
     self.args = {
         'project_root':
         self.symlink.project_root,
         'translation_root':
         os.path.join(self.symlink.WEB_ROOT, self.translation.slug),
     }
     self.assertIn(self.translation, self.project.translations.all())
     self.commands = []