feat(setting): Enable super admin to set ALL device types as global
sets is_global=true and creates device types in global organization !12 closes #31
This commit is contained in:
@ -11,10 +11,21 @@ from itam.models.device_models import DeviceModel
|
||||
from itam.models.software import Software, SoftwareVersion
|
||||
from itam.models.operating_system import OperatingSystemVersion
|
||||
|
||||
|
||||
from settings.models.app_settings import AppSettings
|
||||
|
||||
class DeviceType(DeviceCommonFieldsName):
|
||||
|
||||
|
||||
def clean(self):
|
||||
|
||||
app_settings = AppSettings.objects.get(owner_organization=None)
|
||||
|
||||
if app_settings.device_type_is_global:
|
||||
|
||||
self.organization = app_settings.global_organization
|
||||
self.is_global = app_settings.device_type_is_global
|
||||
|
||||
|
||||
def __str__(self):
|
||||
|
||||
return self.name
|
||||
|
@ -39,7 +39,7 @@ class View(OrganizationPermission, generic.UpdateView):
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
|
||||
return f"/settings/device_type/{self.kwargs['pk']}"
|
||||
return reverse('Settings:_device_type_view', args=(self.kwargs['pk'],))
|
||||
|
||||
|
||||
|
||||
@ -65,7 +65,7 @@ class Add(PermissionRequiredMixin, OrganizationPermission, generic.CreateView):
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
|
||||
return f"/settings/device_type/"
|
||||
return reverse('Settings:_device_types')
|
||||
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
@ -90,7 +90,7 @@ class Delete(PermissionRequiredMixin, OrganizationPermission, generic.DeleteView
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
|
||||
return f"/settings/device_type/"
|
||||
return reverse('Settings:_device_types')
|
||||
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
|
64
app/settings/management/commands/device_type.py
Normal file
64
app/settings/management/commands/device_type.py
Normal 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 import DeviceType
|
||||
|
||||
from settings.models.app_settings import AppSettings
|
||||
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Manage ITAM Device Types for the entire application.'
|
||||
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('-g', '--global', action='store_true', help='Sets all device types to be global (device types will be migrated to global organization if set)')
|
||||
parser.add_argument('-m', '--migrate', action='store_true', help='Migrate existing global device types to global organization')
|
||||
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
|
||||
if kwargs['global']:
|
||||
|
||||
softwares = DeviceType.objects.filter(is_global = False)
|
||||
|
||||
self.stdout.write('Running global')
|
||||
|
||||
self.stdout.write(f'found device types {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 = DeviceType.objects.filter(
|
||||
~Q(organization = app_settings.global_organization)
|
||||
|
|
||||
Q(is_global = False)
|
||||
&
|
||||
Q(organization=app_settings.global_organization),
|
||||
)
|
||||
|
||||
self.stdout.write(f'found device types {str(len(softwares))} to migrate')
|
||||
|
||||
for software in softwares:
|
||||
|
||||
software.clean()
|
||||
software.save()
|
||||
|
||||
self.stdout.write(f"Migrating type {software} to organization {app_settings.global_organization.name}")
|
||||
|
||||
self.stdout.write('Migrate finished')
|
@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.0.6 on 2024-05-27 04:52
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('settings', '0007_appsettings_device_model_is_global'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='appsettings',
|
||||
name='device_type_is_global',
|
||||
field=models.BooleanField(default=False, verbose_name='All Device Types is global'),
|
||||
),
|
||||
]
|
@ -54,6 +54,12 @@ class AppSettings(AppSettingsCommonFields, SaveHistory):
|
||||
default = False,
|
||||
)
|
||||
|
||||
device_type_is_global = models.BooleanField (
|
||||
verbose_name = 'All Device Types is global',
|
||||
blank= False,
|
||||
default = False,
|
||||
)
|
||||
|
||||
software_is_global = models.BooleanField (
|
||||
verbose_name = 'All Software is global',
|
||||
blank= False,
|
||||
@ -85,6 +91,7 @@ class AppSettings(AppSettingsCommonFields, SaveHistory):
|
||||
|
||||
__all__ = [
|
||||
'device_model_is_global',
|
||||
'device_type_is_global',
|
||||
'software_is_global',
|
||||
'software_categories_is_global',
|
||||
'global_organization',
|
||||
|
Reference in New Issue
Block a user