From 74abd6c853eb5a9f0356e6a04b5d1d46cd147f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 23 Dec 2016 18:51:33 +0100 Subject: [PATCH] Fixe le comportement de syncmail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit syncmail ne devrait pas essayer de changer un email si un email du même shortname existe déjà. Ce n'était pas le cas auparavant --- gestioncof/management/commands/syncmails.py | 30 +++++++++++++-------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/gestioncof/management/commands/syncmails.py b/gestioncof/management/commands/syncmails.py index 6ce1fd6e..62936000 100644 --- a/gestioncof/management/commands/syncmails.py +++ b/gestioncof/management/commands/syncmails.py @@ -33,7 +33,6 @@ class Command(BaseCommand): status = {'synced': 0, 'unchanged': 0} for obj in mail_data: - model = obj['model'] fields = obj['fields'] # Pour les trois types d'objets : @@ -43,7 +42,7 @@ class Command(BaseCommand): # plus haut # Variable types - if model == 'custommail.variabletype': + if obj['model'] == 'custommail.variabletype': fields['inner1'] = assoc['types'].get(fields['inner1']) fields['inner2'] = assoc['types'].get(fields['inner2']) if fields['typ'] == 'model': @@ -55,21 +54,30 @@ class Command(BaseCommand): assoc['types'][obj['pk']] = var_type # Custom mails - if model == 'custommail.custommail': - mail, created = CustomMail.objects.get_or_create(**fields) - assoc['mails'][obj['pk']] = mail - if created: + 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 self.stdout.write( 'SYNCED {:s}'.format(fields['shortname'])) - status['synced'] += 1 - else: - status['unchanged'] += 1 + assoc['mails'][obj['pk']] = mail # Variables - if model == 'custommail.custommailvariable': + if obj['model'] == 'custommail.custommailvariable': fields['custommail'] = assoc['mails'].get(fields['custommail']) fields['typ'] = assoc['types'].get(fields['typ']) - CustomMailVariable.objects.get_or_create(**fields) + try: + CustomMailVariable.objects.get( + custommail=fields['custommail'], + name=fields['name'] + ) + except CustomMailVariable.DoesNotExist: + CustomMailVariable.objects.create(**fields) # C'est agréable d'avoir le résultat affiché self.stdout.write(