Load custommails before bda tests

This commit is contained in:
Martin Pépin 2018-01-07 14:30:33 +01:00
parent e9b901337e
commit c80e63415b
2 changed files with 70 additions and 63 deletions

View file

@ -49,6 +49,10 @@ class BdATestHelpers:
(None, Client()) (None, Client())
] ]
def require_custommails(self):
from gestioncof.management.commands import syncmails
syncmails.load_from_file()
def check_restricted_access(self, url, validate_user=user_is_cof, redirect_url=None): def check_restricted_access(self, url, validate_user=user_is_cof, redirect_url=None):
def craft_redirect_url(user): def craft_redirect_url(user):
if redirect_url: if redirect_url:
@ -154,11 +158,11 @@ class TestBdAViews(BdATestHelpers, TestCase):
self.check_restricted_access(url, validate_user=user_is_staff) self.check_restricted_access(url, validate_user=user_is_staff)
def test_send_reminders(self): def test_send_reminders(self):
self.require_custommails()
# Just get the page # Just get the page
url = "/bda/mails-rappel/{}".format(self.tirage.id) url = "/bda/mails-rappel/{}".format(self.tirage.id)
self.check_restricted_access(url, validate_user=user_is_staff) self.check_restricted_access(url, validate_user=user_is_staff)
# Actually send the reminder emails # Actually send the reminder emails
# TODO: first load the emails into the database
_, staff_c = self.client_matrix[0] _, staff_c = self.client_matrix[0]
resp = staff_c.post(url) resp = staff_c.post(url)
self.assertEqual(200, resp.status_code) self.assertEqual(200, resp.status_code)

View file

@ -11,21 +11,16 @@ from django.core.management.base import BaseCommand
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
class Command(BaseCommand): DATA_LOCATION = os.path.join(os.path.dirname(__file__), "..", "data", "custommail.json")
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):
path = os.path.join( def dummy_log(__):
os.path.dirname(os.path.dirname(__file__)), pass
'data', 'custommail.json')
with open(path, 'r') as jsonfile:
# XXX. this should probably be in the custommail package
def load_from_file(log=dummy_log):
with open(DATA_LOCATION, 'r') as jsonfile:
mail_data = json.load(jsonfile) mail_data = json.load(jsonfile)
# On se souvient à quel objet correspond quel pk du json # On se souvient à quel objet correspond quel pk du json
@ -57,14 +52,12 @@ class Command(BaseCommand):
if obj['model'] == 'custommail.custommail': if obj['model'] == 'custommail.custommail':
mail = None mail = None
try: try:
mail = CustomMail.objects.get( mail = CustomMail.objects.get(shortname=fields['shortname'])
shortname=fields['shortname'])
status['unchanged'] += 1 status['unchanged'] += 1
except CustomMail.DoesNotExist: except CustomMail.DoesNotExist:
mail = CustomMail.objects.create(**fields) mail = CustomMail.objects.create(**fields)
status['synced'] += 1 status['synced'] += 1
self.stdout.write( log('SYNCED {:s}'.format(fields['shortname']))
'SYNCED {:s}'.format(fields['shortname']))
assoc['mails'][obj['pk']] = mail assoc['mails'][obj['pk']] = mail
# Variables # Variables
@ -79,8 +72,18 @@ class Command(BaseCommand):
except Variable.DoesNotExist: except Variable.DoesNotExist:
Variable.objects.create(**fields) Variable.objects.create(**fields)
# C'est agréable d'avoir le résultat affiché log('{synced:d} mails synchronized {unchanged:d} unchanged'.format(**status))
self.stdout.write(
'{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)