示例#1
0
    def get_footer_links(self, lesson_slug, page, **kwargs):
        """Return links to previous page, current session and the next page.

        Each link is either a dict with 'url' and 'title' keys or ``None``.

        If :meth:`render_page` fails and a canonical version is in the
        base repo, it's used instead with a warning.
        This method provides the correct footer links for the page,
        since ``sessions`` is not included in the info provided by forks.
        """
        naucse.utils.views.forks_raise_if_disabled()

        task = Task("naucse.utils.forks:get_footer_links",
                    args=[self.slug, lesson_slug, page],
                    kwargs=kwargs)

        result = arca.run(self.repo,
                          self.branch,
                          task,
                          reference=Path("."),
                          depth=None)

        to_return = []

        from naucse.views import logger
        logger.debug(result.output)

        if not isinstance(result.output, dict):
            return None, None, None

        def validate_link(link, key):
            return key in link and isinstance(link[key], str)

        for link_type in "prev_link", "session_link", "next_link":
            link = result.output.get(link_type)

            if isinstance(link, dict) and validate_link(
                    link, "url") and validate_link(link, "title"):
                if link["url"].startswith(f"/{self.slug}/"):
                    absolute_urls_to_freeze.append(link["url"])
                to_return.append(link)
            else:
                to_return.append(None)

        logger.debug(to_return)

        return to_return
示例#2
0
    def slug(self):
        """Return the slug of the repository based on the current branch.

        Returns the default if not on a branch, the branch doesn't
        have a remote, or the remote url can't be parsed.
        """
        from naucse.views import logger

        # Travis CI checks out specific commit, so there isn't an active branch
        if os.environ.get("TRAVIS") and os.environ.get("TRAVIS_REPO_SLUG"):
            return os.environ.get("TRAVIS_REPO_SLUG")

        repo = Repo(".")

        try:
            active_branch = repo.active_branch
        except TypeError:  # thrown if not in a branch
            logger.warning("MetaInfo.slug: There is not active branch")
            return self._default_slug

        try:
            remote_name = active_branch.remote_name
        except ValueError:
            tracking_branch = active_branch.tracking_branch()

            if tracking_branch is None:  # a branch without a remote
                logger.warning(
                    "MetaInfo.slug: The branch doesn't have a remote")
                return self._default_slug

            remote_name = tracking_branch.remote_name

        remote_url = repo.remotes[remote_name].url

        parsed = giturlparse.parse(remote_url)

        if hasattr(parsed, "owner") and hasattr(parsed, "repo"):
            return f"{parsed.owner}/{parsed.repo}"

        logger.warning("MetaInfo.slug: The url could not be parsed.")
        logger.debug("MetaInfo.slug: Parsed %s", parsed.__dict__)

        return self._default_slug