diff --git a/fragdenstaat_de/theme/management/commands/render_sitemap.py b/fragdenstaat_de/theme/management/commands/render_sitemap.py index 4daaae853..f3e9df847 100644 --- a/fragdenstaat_de/theme/management/commands/render_sitemap.py +++ b/fragdenstaat_de/theme/management/commands/render_sitemap.py @@ -1,4 +1,4 @@ -import os +from pathlib import Path from django.contrib.sitemaps import views as sitemaps_views from django.core.management.base import BaseCommand @@ -11,77 +11,78 @@ class Command(BaseCommand): help = "Render sitemap" def add_arguments(self, parser): - parser.add_argument( - "--section", help="Render section", default="", required=False - ) - parser.add_argument( - "--getsections", + subparsers = parser.add_subparsers(dest="subcommand", required=True) + getsections_parser = subparsers.add_parser( + "getsections", help="List available sections", - required=False, - action="store_true", ) - parser.add_argument( - "--outdir", help="Output directory", required=False, default="/tmp/" + 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): - sections = [] - for section in sitemaps: - sections.append(section) - return sections + 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, **options): - sections = [] + def handle(self, *args, func, **kwargs): + func(**kwargs) + + def getsections(self, **kwargs): sections = self.get_sections() - section = options["section"] - outdir = options["outdir"] - if options["getsections"]: - for s in sections: - self.stdout.write(s) - exit() + for s in sections: + self.stdout.write(s) - if section and section not in sections: - self.stderr.write( - "Error: Section {} does not exists. Use '--getsections' for a valid list.".format( - section + 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." ) - ) - exit() + return - if not os.path.isdir(outdir): + if not outdir.exists(): self.stderr.write( - "Error: The directory {} does not exists, please check/create and try again.".format( - outdir - ) + f"Error: The directory {outdir} does not exists, please check/create and try again." ) - exit() + return self.stdout.write("Generating sitemap(s), this might take a while...") if section: - sections.clear() - sections = [section] + sections = section for s in sections: - self.stdout.write("{}".format(s)) + self.stdout.write(s) factory = RequestFactory() - sitemap_name = "/sitemap-{}.xml".format(s) - request = factory.get(sitemap_name) + sitemap_name = f"sitemap-{s}.xml" + request = factory.get("/" + sitemap_name) response = sitemaps_views.sitemap(request, sitemaps, section=s) - sitemap_dest_tmp = "{}/{}".format(outdir, sitemap_name) + 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) + sitemap_name = "sitemap.xml" + request = factory.get("/" + sitemap_name) response = sitemaps_views.index(request, sitemaps, sitemap_url_name="sitemaps") - sitemap_dest_tmp = "{}/{}".format(outdir, sitemap_name) + sitemap_dest_tmp = outdir / sitemap_name self.write_sitemap_tempfile(sitemap_dest_tmp, response.rendered_content) self.stdout.write("Done")