From 85929b5327ce7dfbfe528546cf484b3df6b19056 Mon Sep 17 00:00:00 2001 From: Real-Gecko Date: Thu, 28 May 2020 18:19:25 +0600 Subject: [PATCH] Fixe migrations for new deployments --- accounts/__init__.py | 1 + accounts/apps.py | 51 +++++++++++++++++++ accounts/migrations/0002_addAdmin.py | 23 --------- ...permissionset.py => 0002_permissionset.py} | 4 +- .../migrations/0004_apply_change_password.py | 25 --------- ...move_userattributes_can_clone_instances.py | 18 ------- accounts/models.py | 1 + admin/forms.py | 2 +- instances/__init__.py | 1 + instances/apps.py | 31 +++++++++++ .../0003_migrate_can_clone_instances.py | 35 ------------- 11 files changed, 88 insertions(+), 104 deletions(-) create mode 100644 accounts/apps.py delete mode 100644 accounts/migrations/0002_addAdmin.py rename accounts/migrations/{0003_permissionset.py => 0002_permissionset.py} (91%) delete mode 100644 accounts/migrations/0004_apply_change_password.py delete mode 100644 accounts/migrations/0005_remove_userattributes_can_clone_instances.py create mode 100644 instances/apps.py delete mode 100644 instances/migrations/0003_migrate_can_clone_instances.py diff --git a/accounts/__init__.py b/accounts/__init__.py index e69de29..8319823 100644 --- a/accounts/__init__.py +++ b/accounts/__init__.py @@ -0,0 +1 @@ +default_app_config = 'accounts.apps.AccountsConfig' diff --git a/accounts/apps.py b/accounts/apps.py new file mode 100644 index 0000000..4f128c1 --- /dev/null +++ b/accounts/apps.py @@ -0,0 +1,51 @@ +from django.apps import AppConfig +from django.db.models.signals import post_migrate + + +def apply_change_password(sender, **kwargs): + ''' + Apply new change_password permission for all users + Depending on settings SHOW_PROFILE_EDIT_PASSWORD + ''' + from django.conf import settings + from django.contrib.auth.models import User, Permission + if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'): + print('\033[92mSHOW_PROFILE_EDIT_PASSWORD is found inside settings.py') + print('\033[92mApplying permission can_change_password for all users') + users = User.objects.all() + permission = Permission.objects.get(codename='change_password') + if settings.SHOW_PROFILE_EDIT_PASSWORD: + print('\033[91mWarning!!! Setting to True for all users') + for user in users: + user.user_permissions.add(permission) + else: + print('\033[91mWarning!!! Setting to False for all users') + for user in users: + user.user_permissions.remove(permission) + print('\033[1mDon`t forget to remove the option from settings.py') + + +def create_admin(sender, **kwargs): + ''' + Create initial admin user + ''' + from django.contrib.auth.models import User + from accounts.models import UserAttributes + + plan = kwargs['plan'] + for migration, rolled_back in plan: + if migration.app_label == 'accounts' and migration.name == '0001_initial' and not rolled_back: + if User.objects.count() == 0: + print('\033[92mCreating default admin user') + 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() + break + + +class AccountsConfig(AppConfig): + name = 'accounts' + verbose_name = 'Accounts' + + def ready(self): + post_migrate.connect(apply_change_password, sender=self) + post_migrate.connect(create_admin, sender=self) diff --git a/accounts/migrations/0002_addAdmin.py b/accounts/migrations/0002_addAdmin.py deleted file mode 100644 index 09a91fc..0000000 --- a/accounts/migrations/0002_addAdmin.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 2.2.10 on 2020-01-28 07:01 - -from django.db import migrations - - -def add_useradmin(apps, schema_editor): - from django.utils import timezone - from django.contrib.auth.models import User - from accounts.models import UserAttributes - - admin = User.objects.create_superuser('admin', None, 'admin', last_login=timezone.now()) - UserAttributes(user=admin, max_instances=-1, max_cpus=-1, max_memory=-1, max_disk_size=-1).save() - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0001_initial'), - ] - - operations = [ - migrations.RunPython(add_useradmin), - ] diff --git a/accounts/migrations/0003_permissionset.py b/accounts/migrations/0002_permissionset.py similarity index 91% rename from accounts/migrations/0003_permissionset.py rename to accounts/migrations/0002_permissionset.py index d7cf465..5b36210 100644 --- a/accounts/migrations/0003_permissionset.py +++ b/accounts/migrations/0002_permissionset.py @@ -6,7 +6,7 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('accounts', '0002_addAdmin'), + ('accounts', '0001_initial'), ] operations = [ @@ -16,7 +16,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ], options={ - 'permissions': (('change_password', 'Can change password'),), + 'permissions': (('change_password', 'Can change password'), ), 'managed': False, 'default_permissions': (), }, diff --git a/accounts/migrations/0004_apply_change_password.py b/accounts/migrations/0004_apply_change_password.py deleted file mode 100644 index f313b7e..0000000 --- a/accounts/migrations/0004_apply_change_password.py +++ /dev/null @@ -1,25 +0,0 @@ -from django.db import migrations - - -def apply_change_password(apps, schema_editor): - from django.conf import settings - from django.contrib.auth.models import User, Permission - - if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'): - if settings.SHOW_PROFILE_EDIT_PASSWORD: - permission = Permission.objects.get(codename='change_password') - users = User.objects.all() - user: User - for user in users: - user.user_permissions.add(permission) - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0003_permissionset'), - ] - - operations = [ - migrations.RunPython(apply_change_password), - ] diff --git a/accounts/migrations/0005_remove_userattributes_can_clone_instances.py b/accounts/migrations/0005_remove_userattributes_can_clone_instances.py deleted file mode 100644 index 76e0458..0000000 --- a/accounts/migrations/0005_remove_userattributes_can_clone_instances.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2.12 on 2020-05-28 04:24 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0004_apply_change_password'), - ('instances', '0003_migrate_can_clone_instances'), - ] - - operations = [ - migrations.RemoveField( - model_name='userattributes', - name='can_clone_instances', - ), - ] diff --git a/accounts/models.py b/accounts/models.py index 8484a7e..33b6251 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -29,6 +29,7 @@ class UserSSHKey(models.Model): class UserAttributes(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) + can_clone_instances = models.BooleanField(default=True) max_instances = models.IntegerField(default=1, help_text="-1 for unlimited. Any integer value", validators=[ diff --git a/admin/forms.py b/admin/forms.py index 29c87b8..5c658aa 100644 --- a/admin/forms.py +++ b/admin/forms.py @@ -91,4 +91,4 @@ class UserCreateForm(UserForm): class UserAttributesForm(forms.ModelForm): class Meta: model = UserAttributes - exclude = ['user'] + exclude = ['user', 'can_clone_instances'] diff --git a/instances/__init__.py b/instances/__init__.py index e69de29..bf9df05 100644 --- a/instances/__init__.py +++ b/instances/__init__.py @@ -0,0 +1 @@ +default_app_config = 'instances.apps.InstancesConfig' diff --git a/instances/apps.py b/instances/apps.py new file mode 100644 index 0000000..f84e217 --- /dev/null +++ b/instances/apps.py @@ -0,0 +1,31 @@ +from django.apps import AppConfig +from django.db.models.signals import post_migrate + + +def migrate_can_clone_instances(sender, **kwargs): + ''' + Migrate can clone instances user attribute to permission + ''' + from django.conf import settings + from django.contrib.auth.models import User, Permission + from accounts.models import UserAttributes + + plan = kwargs['plan'] + for migration, rolled_back in plan: + if migration.app_label == 'instances' and migration.name == '0002_permissionset' and not rolled_back: + users = User.objects.all() + permission = Permission.objects.get(codename='clone_instances') + print('\033[92mMigrating can_clone_instaces user attribute to permission') + for user in users: + if user.userattributes: + if user.userattributes.can_clone_instances: + user.user_permissions.add(permission) + break + + +class InstancesConfig(AppConfig): + name = 'instances' + verbose_name = 'instances' + + def ready(self): + post_migrate.connect(migrate_can_clone_instances, sender=self) diff --git a/instances/migrations/0003_migrate_can_clone_instances.py b/instances/migrations/0003_migrate_can_clone_instances.py deleted file mode 100644 index 6b7cc16..0000000 --- a/instances/migrations/0003_migrate_can_clone_instances.py +++ /dev/null @@ -1,35 +0,0 @@ -from django.db import migrations - - -def migrate_can_clone_instances(apps, schema_editor): - from django.contrib.auth.models import User, Permission - user: User - users = User.objects.all() - - permission = Permission.objects.get(codename='clone_instances') - - for user in users: - if user.userattributes.can_clone_instances: - user.user_permissions.add(permission) - - -def reverse_can_clone_instances(apps, schema_editor): - from django.contrib.auth.models import User, Permission - user: User - users = User.objects.all() - - permission = Permission.objects.get(codename='clone_instances') - - for user in users: - user.user_permissions.remove(permission) - - -class Migration(migrations.Migration): - - dependencies = [ - ('instances', '0002_permissionset'), - ] - - operations = [ - migrations.RunPython(migrate_can_clone_instances, reverse_can_clone_instances), - ]