mirror of
https://github.com/retspen/webvirtcloud
synced 2024-10-31 19:44:16 +00:00
Fixe migrations for new deployments
This commit is contained in:
parent
e4e79d3d4b
commit
85929b5327
11 changed files with 88 additions and 104 deletions
|
@ -0,0 +1 @@
|
|||
default_app_config = 'accounts.apps.AccountsConfig'
|
51
accounts/apps.py
Normal file
51
accounts/apps.py
Normal file
|
@ -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)
|
|
@ -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),
|
||||
]
|
|
@ -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': (),
|
||||
},
|
|
@ -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),
|
||||
]
|
|
@ -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',
|
||||
),
|
||||
]
|
|
@ -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=[
|
||||
|
|
|
@ -91,4 +91,4 @@ class UserCreateForm(UserForm):
|
|||
class UserAttributesForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = UserAttributes
|
||||
exclude = ['user']
|
||||
exclude = ['user', 'can_clone_instances']
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
default_app_config = 'instances.apps.InstancesConfig'
|
31
instances/apps.py
Normal file
31
instances/apps.py
Normal file
|
@ -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)
|
|
@ -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),
|
||||
]
|
Loading…
Reference in a new issue