diff --git a/apps/account/management/commands/add_affilations.py b/apps/account/management/commands/add_affilations.py new file mode 100644 index 00000000..4485d6f1 --- /dev/null +++ b/apps/account/management/commands/add_affilations.py @@ -0,0 +1,45 @@ +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.')) \ No newline at end of file diff --git a/apps/account/models.py b/apps/account/models.py index c212ffda..d422b772 100644 --- a/apps/account/models.py +++ b/apps/account/models.py @@ -291,3 +291,11 @@ class UserRole(ProjectBaseMixin): role = models.ForeignKey(Role, verbose_name=_('Role'), on_delete=models.SET_NULL, null=True) establishment = models.ForeignKey(Establishment, verbose_name=_('Establishment'), on_delete=models.SET_NULL, null=True, blank=True) + + +class OldRole(models.Model): + new_role = models.CharField(verbose_name=_('New role'), max_length=512) + old_role = models.CharField(verbose_name=_('Old role'), max_length=512, null=True) + + class Meta: + unique_together = ('new_role', 'old_role') \ No newline at end of file