2020-05-28 12:19:25 +00:00
|
|
|
from django.apps import AppConfig
|
|
|
|
from django.db.models.signals import post_migrate
|
|
|
|
|
|
|
|
|
|
|
|
def migrate_can_clone_instances(sender, **kwargs):
|
2022-11-02 05:54:35 +00:00
|
|
|
"""
|
2020-05-28 12:19:25 +00:00
|
|
|
Migrate can clone instances user attribute to permission
|
2022-11-02 05:54:35 +00:00
|
|
|
"""
|
2020-10-15 05:58:42 +00:00
|
|
|
from django.contrib.auth.models import Permission, User
|
2020-05-28 12:19:25 +00:00
|
|
|
|
2022-11-02 05:54:35 +00:00
|
|
|
plan = kwargs.get("plan", [])
|
2020-05-28 12:19:25 +00:00
|
|
|
for migration, rolled_back in plan:
|
2022-11-02 05:54:35 +00:00
|
|
|
if (migration.app_label == "instances" and migration.name == "0002_permissionset" and not rolled_back):
|
2020-05-28 12:19:25 +00:00
|
|
|
users = User.objects.all()
|
2022-11-02 05:54:35 +00:00
|
|
|
permission = Permission.objects.get(codename="clone_instances")
|
|
|
|
print(
|
|
|
|
"\033[1m* \033[92mMigrating can_clone_instaces user attribute to permission\033[0m"
|
|
|
|
)
|
2020-05-28 12:19:25 +00:00
|
|
|
for user in users:
|
|
|
|
if user.userattributes:
|
|
|
|
if user.userattributes.can_clone_instances:
|
|
|
|
user.user_permissions.add(permission)
|
|
|
|
break
|
|
|
|
|
2020-10-15 05:58:42 +00:00
|
|
|
|
2020-07-20 07:44:33 +00:00
|
|
|
def apply_passwordless_console(sender, **kwargs):
|
2022-11-02 05:54:35 +00:00
|
|
|
"""
|
2020-07-20 07:44:33 +00:00
|
|
|
Apply new passwordless_console permission for all users
|
2022-11-02 05:54:35 +00:00
|
|
|
"""
|
2020-10-15 11:49:36 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-11-05 09:34:31 +00:00
|
|
|
from django.contrib.auth.models import Permission
|
2022-11-02 05:54:35 +00:00
|
|
|
|
2020-10-15 11:49:36 +00:00
|
|
|
User = get_user_model()
|
2022-11-02 05:54:35 +00:00
|
|
|
plan = kwargs.get("plan", [])
|
2020-10-15 11:49:36 +00:00
|
|
|
for migration, rolled_back in plan:
|
2022-11-02 05:54:35 +00:00
|
|
|
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")
|
2020-10-15 11:49:36 +00:00
|
|
|
users = User.objects.all()
|
2022-11-02 05:54:35 +00:00
|
|
|
permission = Permission.objects.get(codename="passwordless_console")
|
2020-10-15 11:49:36 +00:00
|
|
|
for user in users:
|
|
|
|
user.user_permissions.add(permission)
|
2020-10-15 05:58:42 +00:00
|
|
|
|
2020-05-28 12:19:25 +00:00
|
|
|
|
|
|
|
class InstancesConfig(AppConfig):
|
2022-11-02 05:54:35 +00:00
|
|
|
name = "instances"
|
|
|
|
verbose_name = "Instances"
|
2020-05-28 12:19:25 +00:00
|
|
|
|
|
|
|
def ready(self):
|
|
|
|
post_migrate.connect(migrate_can_clone_instances, sender=self)
|
2020-07-20 07:44:33 +00:00
|
|
|
post_migrate.connect(apply_passwordless_console, sender=self)
|