feat(setting): Enable super admin to set ALL device models as global

sets is_global=true and creates device models in global organization

!12 closes #29
This commit is contained in:
2024-05-27 14:14:29 +09:30
parent 935e119e64
commit abbda7b400
5 changed files with 104 additions and 4 deletions

View File

@ -0,0 +1,64 @@
from django.core.management.base import BaseCommand
from django.db.models import Q
from django.utils import timezone
from itam.models.device_models import DeviceModel
from settings.models.app_settings import AppSettings
class Command(BaseCommand):
help = 'Manage ITAM Device Models for the entire application.'
def add_arguments(self, parser):
parser.add_argument('-g', '--global', action='store_true', help='Sets all device models to be global (device models will be migrated to global organization if set)')
parser.add_argument('-m', '--migrate', action='store_true', help='Migrate existing global device models to global organization')
def handle(self, *args, **kwargs):
if kwargs['global']:
softwares = DeviceModel.objects.filter(is_global = False)
self.stdout.write('Running global')
self.stdout.write(f'found device model {str(len(softwares))} to set as global')
for software in softwares:
software.clean()
software.save()
self.stdout.write(f"Setting {software} as global")
self.stdout.write('Global finished')
if kwargs['migrate']:
app_settings = AppSettings.objects.get(owner_organization=None)
self.stdout.write('Running Migrate')
self.stdout.write(f'Global organization: {app_settings.global_organization}')
softwares = DeviceModel.objects.filter(
~Q(organization = app_settings.global_organization)
|
Q(is_global = False)
&
Q(organization=app_settings.global_organization),
)
self.stdout.write(f'found device model {str(len(softwares))} to migrate')
for software in softwares:
software.clean()
software.save()
self.stdout.write(f"Migrating model {software} to organization {app_settings.global_organization.name}")
self.stdout.write('Migrate finished')

View File

@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-05-27 04:38
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('settings', '0006_appsettings_software_categories_is_global_and_more'),
]
operations = [
migrations.AddField(
model_name='appsettings',
name='device_model_is_global',
field=models.BooleanField(default=False, verbose_name='All Device Models are global'),
),
]

View File

@ -48,6 +48,12 @@ class AppSettings(AppSettingsCommonFields, SaveHistory):
related_name = 'owner_organization'
)
device_model_is_global = models.BooleanField (
verbose_name = 'All Device Models are global',
blank= False,
default = False,
)
software_is_global = models.BooleanField (
verbose_name = 'All Software is global',
blank= False,
@ -78,6 +84,7 @@ class AppSettings(AppSettingsCommonFields, SaveHistory):
raise ValidationError("Global Software must have a global organization")
__all__ = [
'device_model_is_global',
'software_is_global',
'software_categories_is_global',
'global_organization',