Example #1
0
    def test_save_url_as_static_file_200(self):
        output_dir = "/output"
        url = "/licenses/metadata.yaml"
        file_content = b"xxxxx"
        relpath = "licenses/metadata.yaml"

        class MockResponse:
            content = file_content
            status_code = 200

        class MockResolverMatch:
            def __init__(self, func):
                self.func = func
                self.args = []
                self.kwargs = {}

        mock_view_metadata = MagicMock()
        mock_view_metadata.return_value = MockResponse()

        with mock.patch("legal_tools.utils.save_bytes_to_file") as mock_save:
            with mock.patch.object(URLResolver, "resolve") as mock_resolve:
                mock_resolve.return_value = MockResolverMatch(
                    func=mock_view_metadata
                )
                utils.save_url_as_static_file(output_dir, url, relpath)

        mock_resolve.assert_called_with(url)
        mock_view_metadata.assert_called()
        mock_save.assert_called_with(
            file_content, "/output/licenses/metadata.yaml"
        )
Example #2
0
    def test_save_url_as_static_file_500(self):
        output_dir = "/output"
        url = "/licenses/metadata.yaml"
        file_content = b"xxxxx"

        class MockResponse:
            content = file_content
            status_code = 500

        class MockResolverMatch:
            def __init__(self, func):
                self.func = func
                self.args = []
                self.kwargs = {}

        mock_view_metadata = MagicMock()
        mock_view_metadata.return_value = MockResponse()

        with mock.patch("legal_tools.utils.save_bytes_to_file"):
            with mock.patch.object(URLResolver, "resolve") as mock_resolve:
                mock_resolve.return_value = MockResolverMatch(
                    func=mock_view_metadata
                )
                with self.assertRaisesMessage(
                    ValueError,
                    "ERROR: Status 500 for url /licenses/metadata.yaml",
                ):
                    with mock.patch("sys.stdout", new=StringIO()):
                        utils.save_url_as_static_file(
                            output_dir,
                            url,
                            relpath="/output/licenses/metadata.yaml",
                        )
Example #3
0
 def test_save_url_as_static_file_not_200(self):
     output_dir = "/output"
     url = "/some/url/"
     with self.assertRaises(Resolver404):
         utils.save_url_as_static_file(
             output_dir,
             url,
             relpath="",
         )
Example #4
0
    def write_dev_index(self):
        hostname = socket.gethostname()
        output_dir = self.output_dir

        LOG.debug(f"{hostname}:{output_dir}")
        LOG.info("Writing dev index")
        save_url_as_static_file(
            output_dir,
            url=reverse("dev_index"),
            relpath="index.html",
        )
Example #5
0
    def write_metadata_yaml(self):
        hostname = socket.gethostname()
        output_dir = self.output_dir

        LOG.debug(f"{hostname}:{output_dir}")
        LOG.info("Writing metadata.yaml")

        save_url_as_static_file(
            output_dir,
            url=reverse("metadata"),
            relpath="licenses/metadata.yaml",
        )
Example #6
0
    def write_translation_branch_statuses(self):
        hostname = socket.gethostname()
        output_dir = self.output_dir

        LOG.debug(f"{hostname}:{output_dir}")

        tbranches = TranslationBranch.objects.filter(complete=False)
        for tbranch_id in tbranches.values_list("id", flat=True):
            LOG.info(f"Writing Translation branch status: {tbranch_id}")
            relpath = f"dev/{tbranch_id}.html"
            LOG.debug(f"    {relpath}")
            save_url_as_static_file(
                output_dir,
                url=f"/dev/{tbranch_id}/",
                relpath=relpath,
            )
Example #7
0
    def write_lists(self):
        hostname = socket.gethostname()
        output_dir = self.output_dir

        LOG.debug(f"{hostname}:{output_dir}")
        LOG.info("Writing lists")

        for category in ["licenses", "publicdomain"]:
            for language_code in settings.LANGUAGES_MOSTLY_TRANSLATED:
                relpath = f"{category}/list.{language_code}.html"
                save_url_as_static_file(
                    output_dir,
                    url=reverse(
                        "view_list_language_specified",
                        kwargs={
                            "category": category,
                            "language_code": language_code,
                        },
                    ),
                    relpath=relpath,
                )
            relpath = f"{category}/list.{settings.LANGUAGE_CODE}.html"
            symlink = "list.html"
            relative_symlink(output_dir, relpath, symlink)
Example #8
0
    def write_legal_tools(self):
        hostname = socket.gethostname()
        output_dir = self.output_dir

        legal_codes = LegalCode.objects.validgroups()
        redirect_pairs = []
        for group in legal_codes.keys():
            LOG.debug(f"{hostname}:{output_dir}")
            LOG.info(f"Writing {group}")
            for legal_code in legal_codes[group]:
                # deed
                try:
                    (
                        relpath,
                        symlinks,
                        redirects_data,
                    ) = legal_code.get_publish_files("deed")
                    save_url_as_static_file(
                        output_dir,
                        url=legal_code.deed_url,
                        relpath=relpath,
                    )
                    for symlink in symlinks:
                        relative_symlink(output_dir, relpath, symlink)
                    for redirect_data in redirects_data:
                        save_redirect(output_dir, redirect_data)
                    redirect_pairs += legal_code.get_redirect_pairs("deed")
                except Http404 as e:
                    if "invalid language" not in str(e):
                        raise
                # legalcode
                (
                    relpath,
                    symlinks,
                    redirects_data,
                ) = legal_code.get_publish_files("legalcode")
                if relpath:
                    # Deed-only tools will not return a legal code relpath
                    save_url_as_static_file(
                        output_dir,
                        url=legal_code.legal_code_url,
                        relpath=relpath,
                    )
                for symlink in symlinks:
                    relative_symlink(output_dir, relpath, symlink)
                for redirect_data in redirects_data:
                    save_redirect(output_dir, redirect_data)
                redirect_pairs += legal_code.get_redirect_pairs("legalcode")

        redirect_pairs.sort(key=lambda x: x[0], reverse=True)
        for i, pair in enumerate(redirect_pairs):
            redirect_pairs[i][0] = re.escape(pair[0])
        widths = [max(map(len, map(str, col))) for col in zip(*redirect_pairs)]
        redirects_include = [
            "# DO NOT EDIT MANUALLY",
            "# This file was generated by the publish command.",
            "# https://github.com/creativecommons/cc-legal-tools-app",
        ]
        for regex, replacement in redirect_pairs:
            regex = f"^/{regex.ljust(widths[0])}"
            replacement = replacement.ljust(widths[1])
            redirects_include.append(
                f"rewrite {regex} {replacement} permanent;")
        redirects_include.append("# vim: set ft=nginx")
        redirects_include.append("")
        redirects_include = "\n".join(redirects_include).encode("utf-8")
        redirects_filename = os.path.join(self.config_dir,
                                          "nginx_language_redirects")
        save_bytes_to_file(redirects_include, redirects_filename)