Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render sitemap #447

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions fragdenstaat_de/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-10-12 09:08-0500\n"
"POT-Creation-Date: 2023-12-15 04:20-0600\n"
"PO-Revision-Date: 2023-10-12 16:09+0200\n"
"Last-Translator: Karl Engelhardt <karl.engelhardt@okfn.de>\n"
"Language-Team: German <https://weblate.frag-den-staat.de/projects/froide/"
Expand Down Expand Up @@ -772,7 +772,6 @@ msgstr "Organisationen"
msgid "No organizations found."
msgstr "Keine Organisation gefunden."

#. Translators: meta keywords
#: fragdenstaat_de/templates/snippets/meta.html
msgid ""
"Freedom of Information Requests, Freedom of Information Law, Public Body "
Expand All @@ -782,8 +781,7 @@ msgstr ""
"Behördeninformationen"

#~ msgid "You like our work? Support us with a donation!"
#~ msgstr ""
#~ "Ihnen gefällt unsere Arbeit? Unterstützen Sie uns mit einer Spende!"
#~ msgstr "Ihnen gefällt unsere Arbeit? Unterstützen Sie uns mit einer Spende!"

#~ msgid "Donate now"
#~ msgstr "Jetzt spenden"
Expand Down
88 changes: 88 additions & 0 deletions fragdenstaat_de/theme/management/commands/render_sitemap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from pathlib import Path

from django.contrib.sitemaps import views as sitemaps_views
from django.core.management.base import BaseCommand
from django.test import RequestFactory

from fragdenstaat_de.theme.urls import sitemaps


class Command(BaseCommand):
help = "Render sitemap"

def add_arguments(self, parser):
subparsers = parser.add_subparsers(dest="subcommand", required=True)
getsections_parser = subparsers.add_parser(
"getsections",
help="List available sections",
)
getsections_parser.set_defaults(func=self.getsections)

generate_parser = subparsers.add_parser("generate")
generate_parser.add_argument(
"--section",
help="Render section (can be specified multiple times)",
action="append",
default=[],
)
generate_parser.add_argument(
"--outdir", help="Output directory", type=Path, default=Path("/tmp/")
)
generate_parser.set_defaults(func=self.generate)

def get_sections(self):
return sitemaps.keys()

def write_sitemap_tempfile(self, sitemap_file, sitemap_content):
with open(sitemap_file, "w") as sitemap_out:
sitemap_out.write(sitemap_content)
return True

def handle(self, *args, func, **kwargs):
func(**kwargs)

def getsections(self, **kwargs):
sections = self.get_sections()

for s in sections:
self.stdout.write(s)

def generate(self, section, outdir, **kwargs):
sections = self.get_sections()

unknown_sections = set(section) - set(sections)
if unknown_sections:
for unknown_section in unknown_sections:
self.stderr.write(
f"Error: Section {unknown_section} does not exists. Use 'getsections' for a valid list."
)
return

if not outdir.exists():
self.stderr.write(
f"Error: The directory {outdir} does not exists, please check/create and try again."
)
return

self.stdout.write("Generating sitemap(s), this might take a while...")

if section:
sections = section

for s in sections:
self.stdout.write(s)
factory = RequestFactory()
sitemap_name = f"sitemap-{s}.xml"
request = factory.get("/" + sitemap_name)
response = sitemaps_views.sitemap(request, sitemaps, section=s)

sitemap_dest_tmp = outdir / sitemap_name
self.write_sitemap_tempfile(sitemap_dest_tmp, response.rendered_content)

sitemap_name = "sitemap.xml"
request = factory.get("/" + sitemap_name)
response = sitemaps_views.index(request, sitemaps, sitemap_url_name="sitemaps")
sitemap_dest_tmp = outdir / sitemap_name
self.write_sitemap_tempfile(sitemap_dest_tmp, response.rendered_content)

self.stdout.write("Done")