Merge branch 'Kerl/syncmails' into 'master'
Utilisation de la command `syncmails` du package custommail See merge request cof-geek/gestioCOF!323
This commit is contained in:
commit
49a74e8e1e
3 changed files with 13 additions and 93 deletions
|
@ -1,9 +1,12 @@
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.core.management import call_command
|
||||||
from django.test import Client, TestCase
|
from django.test import Client, TestCase
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
@ -49,9 +52,10 @@ class BdATestHelpers:
|
||||||
]
|
]
|
||||||
|
|
||||||
def require_custommails(self):
|
def require_custommails(self):
|
||||||
from django.core.management import call_command
|
data_file = os.path.join(
|
||||||
|
settings.BASE_DIR, "gestioncof", "management", "data", "custommail.json"
|
||||||
call_command("syncmails", verbosity=0)
|
)
|
||||||
|
call_command("syncmails", data_file, verbosity=0)
|
||||||
|
|
||||||
def check_restricted_access(
|
def check_restricted_access(
|
||||||
self, url, validate_user=user_is_cof, redirect_url=None
|
self, url, validate_user=user_is_cof, redirect_url=None
|
||||||
|
|
|
@ -1,89 +0,0 @@
|
||||||
"""
|
|
||||||
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
|
|
||||||
|
|
||||||
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
|
|
||||||
def load_from_file(log=dummy_log, verbosity=1):
|
|
||||||
with open(DATA_LOCATION, "r") as jsonfile:
|
|
||||||
mail_data = json.load(jsonfile)
|
|
||||||
|
|
||||||
# On se souvient à quel objet correspond quel pk du json
|
|
||||||
assoc = {"types": {}, "mails": {}}
|
|
||||||
status = {"synced": 0, "unchanged": 0}
|
|
||||||
|
|
||||||
for obj in mail_data:
|
|
||||||
fields = obj["fields"]
|
|
||||||
|
|
||||||
# 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"]
|
|
||||||
)
|
|
||||||
var_type, _ = Type.objects.get_or_create(**fields)
|
|
||||||
assoc["types"][obj["pk"]] = var_type
|
|
||||||
|
|
||||||
# Custom mails
|
|
||||||
if obj["model"] == "custommail.custommail":
|
|
||||||
mail = None
|
|
||||||
try:
|
|
||||||
mail = CustomMail.objects.get(shortname=fields["shortname"])
|
|
||||||
status["unchanged"] += 1
|
|
||||||
except CustomMail.DoesNotExist:
|
|
||||||
mail = CustomMail.objects.create(**fields)
|
|
||||||
status["synced"] += 1
|
|
||||||
if verbosity:
|
|
||||||
log("SYNCED {:s}".format(fields["shortname"]))
|
|
||||||
assoc["mails"][obj["pk"]] = mail
|
|
||||||
|
|
||||||
# Variables
|
|
||||||
if obj["model"] == "custommail.custommailvariable":
|
|
||||||
fields["custommail"] = assoc["mails"].get(fields["custommail"])
|
|
||||||
fields["type"] = assoc["types"].get(fields["type"])
|
|
||||||
try:
|
|
||||||
Variable.objects.get(
|
|
||||||
custommail=fields["custommail"], name=fields["name"]
|
|
||||||
)
|
|
||||||
except Variable.DoesNotExist:
|
|
||||||
Variable.objects.create(**fields)
|
|
||||||
|
|
||||||
if verbosity:
|
|
||||||
log("{synced:d} mails synchronized {unchanged:d} unchanged".format(**status))
|
|
||||||
|
|
||||||
|
|
||||||
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):
|
|
||||||
load_from_file(log=self.stdout.write)
|
|
|
@ -1,8 +1,10 @@
|
||||||
import csv
|
import csv
|
||||||
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from custommail.models import CustomMail
|
from custommail.models import CustomMail
|
||||||
|
from django.conf import settings
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.contrib.messages.api import get_messages
|
from django.contrib.messages.api import get_messages
|
||||||
|
@ -32,7 +34,10 @@ class RegistrationViewTests(ViewTestCaseMixin, TestCase):
|
||||||
auth_forbidden = [None, "user", "member"]
|
auth_forbidden = [None, "user", "member"]
|
||||||
|
|
||||||
def requires_mails(self):
|
def requires_mails(self):
|
||||||
call_command("syncmails", verbosity=0)
|
data_file = os.path.join(
|
||||||
|
settings.BASE_DIR, "gestioncof", "management", "data", "custommail.json"
|
||||||
|
)
|
||||||
|
call_command("syncmails", data_file, verbosity=0)
|
||||||
|
|
||||||
def test_get(self):
|
def test_get(self):
|
||||||
r = self.client.get(self.url)
|
r = self.client.get(self.url)
|
||||||
|
|
Loading…
Reference in a new issue