kpsul/gestioncof/management/commands/syncmails.py

90 lines
3.3 KiB
Python
Raw Permalink Normal View History

"""
Import des mails de GestioCOF dans la base de donnée
"""
import json
import os
from custommail.models import CustomMail, Type, Variable
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
2018-01-07 14:30:33 +01:00
DATA_LOCATION = os.path.join(os.path.dirname(__file__), "..", "data", "custommail.json")
def dummy_log(__):
pass
# XXX. this should probably be in the custommail package
2018-09-30 13:08:58 +02:00
def load_from_file(log=dummy_log, verbosity=1):
with open(DATA_LOCATION, "r") as jsonfile:
2018-01-07 14:30:33 +01:00
mail_data = json.load(jsonfile)
# On se souvient à quel objet correspond quel pk du json
assoc = {"types": {}, "mails": {}}
status = {"synced": 0, "unchanged": 0}
2018-01-07 14:30:33 +01:00
for obj in mail_data:
fields = obj["fields"]
2018-01-07 14:30:33 +01:00
# Pour les trois types d'objets :
# - On récupère les objets référencés par les clefs étrangères
# - On crée l'objet si nécessaire
# - On le stocke éventuellement dans les deux dictionnaires définis
# plus haut
# Variable types
if obj["model"] == "custommail.variabletype":
fields["inner1"] = assoc["types"].get(fields["inner1"])
fields["inner2"] = assoc["types"].get(fields["inner2"])
if fields["kind"] == "model":
fields["content_type"] = ContentType.objects.get_by_natural_key(
*fields["content_type"]
2018-01-07 14:30:33 +01:00
)
var_type, _ = Type.objects.get_or_create(**fields)
assoc["types"][obj["pk"]] = var_type
2018-01-07 14:30:33 +01:00
# Custom mails
if obj["model"] == "custommail.custommail":
2018-01-07 14:30:33 +01:00
mail = None
try:
mail = CustomMail.objects.get(shortname=fields["shortname"])
status["unchanged"] += 1
2018-01-07 14:30:33 +01:00
except CustomMail.DoesNotExist:
mail = CustomMail.objects.create(**fields)
status["synced"] += 1
2018-09-30 13:08:58 +02:00
if verbosity:
log("SYNCED {:s}".format(fields["shortname"]))
assoc["mails"][obj["pk"]] = mail
2018-01-07 14:30:33 +01:00
# Variables
if obj["model"] == "custommail.custommailvariable":
fields["custommail"] = assoc["mails"].get(fields["custommail"])
fields["type"] = assoc["types"].get(fields["type"])
2018-01-07 14:30:33 +01:00
try:
Variable.objects.get(
custommail=fields["custommail"], name=fields["name"]
2018-01-07 14:30:33 +01:00
)
except Variable.DoesNotExist:
Variable.objects.create(**fields)
2018-09-30 13:08:58 +02:00
if verbosity:
log("{synced:d} mails synchronized {unchanged:d} unchanged".format(**status))
2018-01-07 14:30:33 +01:00
class Command(BaseCommand):
help = (
"Va chercher les données mails de GestioCOF stocké au format json "
"dans /gestioncof/management/data/custommails.json. Le format des "
"données est celui donné par la commande :"
" `python manage.py dumpdata custommail --natural-foreign` "
"La bonne façon de mettre à jour ce fichier est donc de le "
"charger à l'aide de syncmails, le faire les modifications à "
"l'aide de l'interface administration et/ou du shell puis de le "
"remplacer par le nouveau résultat de la commande précédente."
)
def handle(self, *args, **options):
2018-01-07 14:30:33 +01:00
load_from_file(log=self.stdout.write)