1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 15:45:23 +00:00

Minor improvements for post_migrate signals

This commit is contained in:
Real-Gecko 2020-10-15 17:49:36 +06:00
parent 0f66187e80
commit d04267eaa4
2 changed files with 21 additions and 17 deletions

View file

@ -10,19 +10,19 @@ def apply_change_password(sender, **kwargs):
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User, Permission from django.contrib.auth.models import User, Permission
if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'): if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'):
print('\033[92mSHOW_PROFILE_EDIT_PASSWORD is found inside settings.py\033[0m') print('\033[1m! \033[92mSHOW_PROFILE_EDIT_PASSWORD is found inside settings.py\033[0m')
print('\033[92mApplying permission can_change_password for all users\033[0m') print('\033[1m* \033[92mApplying permission can_change_password for all users\033[0m')
users = User.objects.all() users = User.objects.all()
permission = Permission.objects.get(codename='change_password') permission = Permission.objects.get(codename='change_password')
if settings.SHOW_PROFILE_EDIT_PASSWORD: if settings.SHOW_PROFILE_EDIT_PASSWORD:
print('\033[91mWarning!!! Setting to True for all users\033[0m') print('\033[1m! \033[91mWarning!!! Setting to True for all users\033[0m')
for user in users: for user in users:
user.user_permissions.add(permission) user.user_permissions.add(permission)
else: else:
print('\033[91mWarning!!! Setting to False for all users\033[0m') print('\033[1m* \033[91mWarning!!! Setting to False for all users\033[0m')
for user in users: for user in users:
user.user_permissions.remove(permission) user.user_permissions.remove(permission)
print('\033[1mDon`t forget to remove the option from settings.py\033[0m') print('\033[1m! Don`t forget to remove the option from settings.py\033[0m')
def create_admin(sender, **kwargs): def create_admin(sender, **kwargs):
@ -32,11 +32,11 @@ def create_admin(sender, **kwargs):
from django.contrib.auth.models import User from django.contrib.auth.models import User
from accounts.models import UserAttributes from accounts.models import UserAttributes
plan = kwargs['plan'] plan = kwargs.get('plan', [])
for migration, rolled_back in plan: for migration, rolled_back in plan:
if migration.app_label == 'accounts' and migration.name == '0001_initial' and not rolled_back: if migration.app_label == 'accounts' and migration.name == '0001_initial' and not rolled_back:
if User.objects.count() == 0: if User.objects.count() == 0:
print('\033[92mCreating default admin user\033[0m') print('\033[1m* \033[92mCreating default admin user\033[0m')
admin = User.objects.create_superuser('admin', None, 'admin') admin = User.objects.create_superuser('admin', None, 'admin')
UserAttributes(user=admin, max_instances=-1, max_cpus=-1, max_memory=-1, max_disk_size=-1).save() UserAttributes(user=admin, max_instances=-1, max_cpus=-1, max_memory=-1, max_disk_size=-1).save()
break break
@ -47,5 +47,5 @@ class AccountsConfig(AppConfig):
verbose_name = 'Accounts' verbose_name = 'Accounts'
def ready(self): def ready(self):
post_migrate.connect(apply_change_password, sender=self)
post_migrate.connect(create_admin, sender=self) post_migrate.connect(create_admin, sender=self)
post_migrate.connect(apply_change_password, sender=self)

View file

@ -8,12 +8,12 @@ def migrate_can_clone_instances(sender, **kwargs):
''' '''
from django.contrib.auth.models import Permission, User from django.contrib.auth.models import Permission, User
plan = kwargs['plan'] plan = kwargs.get('plan', [])
for migration, rolled_back in plan: for migration, rolled_back in plan:
if migration.app_label == 'instances' and migration.name == '0002_permissionset' and not rolled_back: if migration.app_label == 'instances' and migration.name == '0002_permissionset' and not rolled_back:
users = User.objects.all() users = User.objects.all()
permission = Permission.objects.get(codename='clone_instances') permission = Permission.objects.get(codename='clone_instances')
print('\033[92mMigrating can_clone_instaces user attribute to permission\033[0m') print('\033[1m* \033[92mMigrating can_clone_instaces user attribute to permission\033[0m')
for user in users: for user in users:
if user.userattributes: if user.userattributes:
if user.userattributes.can_clone_instances: if user.userattributes.can_clone_instances:
@ -25,9 +25,13 @@ def apply_passwordless_console(sender, **kwargs):
''' '''
Apply new passwordless_console permission for all users Apply new passwordless_console permission for all users
''' '''
from django.contrib.auth.models import Permission, User from django.contrib.auth.models import Permission
from django.contrib.auth import get_user_model
print('\033[92mApplying permission passwordless_console for all users\033[0m') User = get_user_model()
plan = kwargs.get('plan', [])
for migration, rolled_back in plan:
if migration.app_label == 'instances' and migration.name == '0009_auto_20200717_0524' and not rolled_back:
print('\033[1m* \033[92mApplying permission passwordless_console for all users\033[0m')
users = User.objects.all() users = User.objects.all()
permission = Permission.objects.get(codename='passwordless_console') permission = Permission.objects.get(codename='passwordless_console')
for user in users: for user in users: