Old role migrate1
This commit is contained in:
parent
4797225beb
commit
cc7754a9d7
|
|
@ -1,4 +1,5 @@
|
||||||
from account.models import OldRole
|
from account.models import OldRole, Role
|
||||||
|
from main.models import SiteSettings
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.db import connections
|
from django.db import connections
|
||||||
from establishment.management.commands.add_position import namedtuplefetchall
|
from establishment.management.commands.add_position import namedtuplefetchall
|
||||||
|
|
@ -34,12 +35,51 @@ class Command(BaseCommand):
|
||||||
''')
|
''')
|
||||||
return namedtuplefetchall(cursor)
|
return namedtuplefetchall(cursor)
|
||||||
|
|
||||||
def handle(self, *args, **kwargs):
|
def add_old_roles(self):
|
||||||
objects = []
|
objects = []
|
||||||
OldRole.objects.all().delete()
|
OldRole.objects.all().delete()
|
||||||
for s in tqdm(self.map_role_sql(), desc='Add permissions old'):
|
for s in tqdm(self.map_role_sql(), desc='Add permissions old'):
|
||||||
objects.append(
|
objects.append(
|
||||||
OldRole(new_role=s.new_role, old_role=s.role)
|
OldRole(new_role=s.new_role, old_role=s.role)
|
||||||
)
|
)
|
||||||
OldRole.objects.bulk_create(objects)
|
OldRole.objects.bulk_create(objects)
|
||||||
self.stdout.write(self.style.WARNING(f'Migrated old roles.'))
|
self.stdout.write(self.style.WARNING(f'Migrated old roles.'))
|
||||||
|
|
||||||
|
def site_role_sql(self):
|
||||||
|
with connections['legacy'].cursor() as cursor:
|
||||||
|
cursor.execute('''
|
||||||
|
select site_id,
|
||||||
|
role
|
||||||
|
from
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
DISTINCT
|
||||||
|
site_id,
|
||||||
|
COALESCE(role, 'GUEST') as role
|
||||||
|
FROM site_affiliations AS sa
|
||||||
|
) t
|
||||||
|
where t.role not in ('admin', 'GUEST')
|
||||||
|
''')
|
||||||
|
return namedtuplefetchall(cursor)
|
||||||
|
|
||||||
|
def add_site_role(self):
|
||||||
|
objects = []
|
||||||
|
for s in tqdm(self.site_role_sql(), desc='Add site role'):
|
||||||
|
old_role = OldRole.objects.get(old_role=s.role)
|
||||||
|
role_choice = getattr(Role, old_role.new_role)
|
||||||
|
sites = SiteSettings.objects.filter(old_id=s.site_id)
|
||||||
|
for site in sites:
|
||||||
|
role = Role.objects.filter(site=site, role=role_choice)
|
||||||
|
if not role.exists():
|
||||||
|
objects.append(
|
||||||
|
Role(site=site, role=role_choice)
|
||||||
|
)
|
||||||
|
|
||||||
|
Role.objects.bulk_create(objects)
|
||||||
|
self.stdout.write(self.style.WARNING(f'Added site roles.'))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def handle(self, *args, **kwargs):
|
||||||
|
# self.add_old_roles()
|
||||||
|
self.add_site_role()
|
||||||
Loading…
Reference in New Issue
Block a user