1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-11-27 20:13:04 +00:00
med/media/management/commands/export_markdown_site.py

100 lines
4.4 KiB
Python
Raw Normal View History

from django.core.management import BaseCommand
2021-10-23 16:31:03 +00:00
from media.models import Comic, CD, Manga, Review, Novel, Vinyl, Game
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"]
2020-10-26 13:44:25 +00:00
with open(directory + "/docs/index.md", "w") as f:
f.write("# Media de la Mediatek\n\n\n")
2020-12-28 22:12:27 +00:00
f.write("Ce site répertorie l'intégralité des media présents "
"à la Mediatek de l'ENS Paris-Saclay.\n")
2020-10-26 13:44:25 +00:00
2021-10-23 16:31:03 +00:00
for model_class, file_name in [(Comic, "bd.md"), (Manga, "mangas.md"),
(Novel, "romans.md"),
(CD, "cd.md"), (Vinyl, "vinyles.md")]:
2020-12-28 22:12:27 +00:00
self.process_model_class(model_class, file_name, f, directory)
2020-10-26 13:27:43 +00:00
# Traitement différent pour les revues
with open(directory + "/docs/revues.md", "w") as f:
2020-10-26 13:33:52 +00:00
f.write("# Revues\n\n\n")
2020-10-26 13:27:43 +00:00
2020-12-28 22:12:27 +00:00
titles = list(set(obj["title"] for obj in
2021-10-23 16:31:03 +00:00
Review.objects.values("title").distinct().all()))
2020-10-26 13:27:43 +00:00
titles.sort()
for title in titles:
f.write(f"## {title}\n\n\n")
2021-10-23 16:31:03 +00:00
for medium in Review.objects.filter(title=title)\
2020-12-28 22:12:27 +00:00
.order_by("number").all():
2020-10-26 13:27:43 +00:00
f.write(f"### Numéro {medium.number}\n\n\n")
if medium.double:
f.write("Double revue\n\n")
if medium.year:
f.write(f"Année : {medium.year}\n\n")
if medium.month:
f.write(f"Mois : {medium.month}\n\n")
if medium.day:
f.write(f"Jour : {medium.day}\n\n")
f.write("\n\n\n")
2020-10-26 14:13:19 +00:00
# Traitement différent pour les jeux
with open(directory + "/docs/jeux.md", "w") as f:
f.write("# Jeux\n\n\n")
2021-10-23 16:31:03 +00:00
for game in Game.objects.order_by("name").all():
2020-10-26 14:13:19 +00:00
f.write(f"## {game.name}\n\n\n")
2021-10-23 16:39:51 +00:00
f.write(f"Durée : {game.duration}\n\n")
f.write(f"Nombre de joueurs : {game.players_min} "
f"- {game.players_max}\n\n")
if game.owner.username != "Med":
f.write(f"Propriétaire : {game.owner.username}\n\n")
2020-10-26 14:13:19 +00:00
if game.comment:
f.write(f"Commentaire : {game.comment}\n\n")
f.write("\n\n\n")
2020-12-28 22:12:27 +00:00
def process_model_class(self, model_class, file_name, f, directory):
with open(directory + "/docs/" + file_name, "w") as f:
f.write("# " + str(model_class._meta.verbose_name_plural)
.capitalize() + "\n\n\n")
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 : "
f"{medium.number_of_pages}\n\n")
if hasattr(medium, "rpm"):
f.write(f"Tours par minute : "
f"{medium.rpm}\n\n")
if hasattr(medium, "publish_date"):
f.write(f"Publié le : "
f"{medium.publish_date}\n\n")
if hasattr(medium, "external_url"):
f.write(f"Lien : [{medium.external_url}]"
f"({medium.external_url})\n\n")
f.write("\n\n\n")