43 lines
2.0 KiB
Python
43 lines
2.0 KiB
Python
from django.core.management.base import BaseCommand
|
|
from django.db import connections
|
|
from django.db.models import Q
|
|
from establishment.management.commands.add_position import namedtuplefetchall
|
|
from account.models import User
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Add account from old db to new db'
|
|
|
|
def account_sql(self):
|
|
with connections['legacy'].cursor() as cursor:
|
|
cursor.execute('''
|
|
select a.email, a.id as account_id, a.encrypted_password
|
|
from accounts as a
|
|
where a.email is not null
|
|
and a.email not in ('cyril@tomatic.net',
|
|
'cyril2@tomatic.net',
|
|
'd.sadykova@id-east.ru',
|
|
'd.sadykova@octopod.ru',
|
|
'n.yurchenko@id-east.ru'
|
|
)
|
|
and a.confirmed_at is not null
|
|
''')
|
|
return namedtuplefetchall(cursor)
|
|
|
|
def handle(self, *args, **kwargs):
|
|
objects = []
|
|
for a in self.account_sql():
|
|
count = User.objects.filter(Q(email=a.email) | Q(old_id=a.account_id)).count()
|
|
if count == 0:
|
|
objects.append(User(email=a.email,
|
|
unconfirmed_email=False,
|
|
email_confirmed=True,
|
|
old_id=a.account_id,
|
|
password='bcrypt$'+a.encrypted_password
|
|
))
|
|
else:
|
|
user = User.objects.filter(Q(email=a.email) | Q(old_id=a.account_id))
|
|
user.update(password='bcrypt$'+a.encrypted_password)
|
|
|
|
User.objects.bulk_create(objects)
|
|
self.stdout.write(self.style.WARNING(f'Created accounts objects.')) |