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(