45 lines
2.1 KiB
Python
45 lines
2.1 KiB
Python
from account.models import OldRole
|
|
from django.core.management.base import BaseCommand
|
|
from django.db import connections
|
|
from establishment.management.commands.add_position import namedtuplefetchall
|
|
from tqdm import tqdm
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = '''Add site affilations from old db to new db.
|
|
Run after migrate {}!!!'''
|
|
|
|
def map_role_sql(self):
|
|
with connections['legacy'].cursor() as cursor:
|
|
cursor.execute('''
|
|
select distinct
|
|
case when role = 'news_editor' then 'CONTENT_PAGE_MANAGER'
|
|
when role in ('reviewer', 'reviwer', 'reviewer_manager') then 'REVIEWER_MANGER'
|
|
when role = 'admin' then 'SUPERUSER'
|
|
when role ='community_manager' then 'COUNTRY_ADMIN'
|
|
when role = 'site_admin' then 'COUNTRY_ADMIN'
|
|
when role = 'wine_reviewer' then 'WINERY_REVIEWER'
|
|
when role in ('salesman', 'sales_man') then 'SALES_MAN'
|
|
when role = 'seller' then 'SELLER'
|
|
else role
|
|
end as new_role,
|
|
case when role = 'GUEST' then null else role end as role
|
|
from
|
|
(
|
|
SELECT
|
|
DISTINCT
|
|
COALESCE(role, 'GUEST') as role
|
|
FROM site_affiliations AS sa
|
|
) t
|
|
''')
|
|
return namedtuplefetchall(cursor)
|
|
|
|
def handle(self, *args, **kwargs):
|
|
objects = []
|
|
OldRole.objects.all().delete()
|
|
for s in tqdm(self.map_role_sql(), desc='Add permissions old'):
|
|
objects.append(
|
|
OldRole(new_role=s.new_role, old_role=s.role)
|
|
)
|
|
OldRole.objects.bulk_create(objects)
|
|
self.stdout.write(self.style.WARNING(f'Migrated old roles.')) |