From cb9cd8f9b6f6e8a73ece0445beac0bc52a7c3546 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 26 Oct 2020 14:21:27 +0100 Subject: [PATCH] Add script to export the database into a user-friendly website --- .../commands/export_markdown_site.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 media/management/commands/export_markdown_site.py diff --git a/media/management/commands/export_markdown_site.py b/media/management/commands/export_markdown_site.py new file mode 100644 index 0000000..9806054 --- /dev/null +++ b/media/management/commands/export_markdown_site.py @@ -0,0 +1,43 @@ +from argparse import FileType +from sys import stdin + +from django.core.management import BaseCommand +from media.models import BD, CD, Manga, Revue, Roman, Vinyle + + +class Command(BaseCommand): + """ + Extract the database into a user-friendly website written in Markdown. + """ + def add_arguments(self, parser): + parser.add_argument('--directory', '-d', type=str, default='.', + help="Directory where mkdocs is running.") + + def handle(self, *args, **options): + directory = options["directory"] + + for model_class, file_name in [(BD, "bd.md"), (Manga, "mangas.md"), (Roman, "romans.md"), + (CD, "cd.md"), (Vinyle, "vinyle.md"), (Revue, "revues.md")]: + with open(directory + "/docs/" + file_name, "w") as f: + titles = list(set(obj["title"] for obj in model_class.objects.values("title").distinct().all())) + titles.sort() + + for title in titles: + f.write(f"## {title}\n\n\n") + + for medium in model_class.objects.filter(title=title).order_by("side_identifier").all(): + if hasattr(medium, "subtitle"): + f.write(f"### {medium.subtitle}\n\n\n") + if hasattr(medium, "isbn"): + f.write(f"ISBN : {medium.isbn}\n\n") + f.write(f"Cote : {medium.side_identifier}\n\n") + f.write("Auteurs : " + ", ".join(author.name for author in medium.authors.all()) + "\n\n") + if hasattr(medium, "number_of_pages"): + f.write(f"Nombre de pages : {medium.number_of_pages}\n\n") + if hasattr(medium, "rpm"): + f.write(f"Tours par minute : {medium.rpm}\n\n") + if hasattr(medium, "publish_date"): + f.write(f"PubliƩ le : {medium.publish_date}\n\n") + if hasattr(medium, "external_url"): + f.write(f"Lien : [{medium.external_url}]({medium.external_url})\n\n") + f.write("\n\n\n")