Skip to content

Commit

Permalink
#148 - command to create users and preregisters for node
Browse files Browse the repository at this point in the history
- other command to check duplicated account emails previously
  • Loading branch information
jbc25 committed May 23, 2024
1 parent 3fbf970 commit fbb77bf
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
23 changes: 23 additions & 0 deletions market/management/commands/check_duplicated_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.core.management.base import BaseCommand

from market.models import Account


class Command(BaseCommand):
help = 'Check duplicated account emails for all accounts of all nodes'

def handle(self, *args, **options):

accounts = Account.objects.all()

print(f'Checking accounts. Total {len(accounts)}')

emails = []

for account in accounts:

email = account.email
if email in emails:
print(f"Email duplicated: {email}. Node {account.node}")
else:
emails.append(email)
54 changes: 54 additions & 0 deletions market/management/commands/create_preregister_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import argparse
import time

from django.core.management.base import BaseCommand

from authentication.models import User
from authentication.models.preregister import PreRegisteredUser
from core.models import Node
from market.models import Account, Consumer


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']

node = Node.objects.get(pk=node_id)

accounts = Account.objects.filter(node=node)

print(f'Generating users and preregisters. Total {len(accounts)}')
current = 0

existing_emails = []

for account in accounts:
current += 1
print(f'Current: {current}')

# Check user email
user = User.objects.filter(email=account.email).first()
if user:
print(f"User email already exists: {account.email}")
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

print("\nExisting emails:")
print("\n".join(existing_emails))

0 comments on commit fbb77bf

Please sign in to comment.