From 1657ad9e9f401393f6d52720450f50e21a7a6696 Mon Sep 17 00:00:00 2001 From: julio Date: Fri, 31 May 2024 11:26:25 +0200 Subject: [PATCH] HA-148 - Improved create_preregister_users command to choose no intercoop, only intercoop or all users HA-166 - Improved admin email sending to do it only if there is an admin email already set up --- authentication/models/preregister.py | 19 ++++++++------- .../commands/create_preregister_users.py | 24 ++++++++++++------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/authentication/models/preregister.py b/authentication/models/preregister.py index 6514e48..8da85ba 100644 --- a/authentication/models/preregister.py +++ b/authentication/models/preregister.py @@ -48,15 +48,16 @@ def send_email(self): self.email_sent = True self.save() - # Admin confirmation email - send_template_email( - title=_('Nueva usuaria creada en la app'), - destination=self.account.node.admin_email, - template_name='admin_preregister_confirm_msg', - template_params={ - 'user': self.user, - 'account': self.account } - ) + if self.account.node.admin_email: + send_template_email( + title=_('Nueva usuaria creada en la app'), + destination=self.account.node.admin_email, + template_name='admin_preregister_confirm_msg', + template_params={ + 'user': self.user, + 'account': self.account } + ) + except Exception as e: print(e) diff --git a/market/management/commands/create_preregister_users.py b/market/management/commands/create_preregister_users.py index 0d0be2a..fdaa43a 100644 --- a/market/management/commands/create_preregister_users.py +++ b/market/management/commands/create_preregister_users.py @@ -9,18 +9,22 @@ from market.models import Account, Consumer +def create_preregister(account): + PreRegisteredUser.create_user_and_preregister(account) + time.sleep(2) # to void possible errors due to very quick email sending + + class Command(BaseCommand): help = 'Create preregister user for all accounts of node' def add_arguments(self, parser): parser.add_argument('--node', type=int, help='Node Id') parser.add_argument('--intercoop', action=argparse.BooleanOptionalAction) - parser.set_defaults(intercoop=True) def handle(self, *args, **options): node_id = options['node'] - include_intercoop = options['intercoop'] + intercoop = options['intercoop'] node = Node.objects.get(pk=node_id) @@ -42,13 +46,15 @@ def handle(self, *args, **options): existing_emails.append(account.email) continue - if not include_intercoop and isinstance(account, Consumer) and account.is_intercoop: - print(f"Skiping intercoop: {account.email}") - continue - - print(f"Creating user and preregister: {account.email}") - PreRegisteredUser.create_user_and_preregister(account) - time.sleep(2) # to void possible errors due to very quick email sending + if intercoop is None: + print(f"Creating user and preregister: {account.email}") + create_preregister(account) + elif intercoop is True and account.is_intercoop: + print(f"Creating Intercoop user and preregister: {account.email}") + create_preregister(account) + elif intercoop is False and not account.is_intercoop: + print(f"Creating not Intercoop user and preregister: {account.email}") + create_preregister(account) print("\nExisting emails:") print("\n".join(existing_emails))