feat(setting): Enable super admin to set ALL software categories as global
sets is_global=true and creates software categories in global organization !12 closes #30
This commit is contained in:
64
app/settings/management/commands/software_categories.py
Normal file
64
app/settings/management/commands/software_categories.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.software import SoftwareCategory
|
||||
|
||||
from settings.models.app_settings import AppSettings
|
||||
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Manage ITAM Software Categories for the entire application.'
|
||||
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('-g', '--global', action='store_true', help='Sets all software categories to be global (software categories will be migrated to global organization if set)')
|
||||
parser.add_argument('-m', '--migrate', action='store_true', help='Migrate existing global software categories to global organization')
|
||||
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
|
||||
if kwargs['global']:
|
||||
|
||||
softwares = SoftwareCategory.objects.filter(is_global = False)
|
||||
|
||||
self.stdout.write('Running global')
|
||||
|
||||
self.stdout.write(f'found software category {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 = SoftwareCategory.objects.filter(
|
||||
~Q(organization = app_settings.global_organization)
|
||||
|
|
||||
Q(is_global = False)
|
||||
&
|
||||
Q(organization=app_settings.global_organization),
|
||||
)
|
||||
|
||||
self.stdout.write(f'found software category {str(len(softwares))} to migrate')
|
||||
|
||||
for software in softwares:
|
||||
|
||||
software.clean()
|
||||
software.save()
|
||||
|
||||
self.stdout.write(f"Migrating category {software} to organization {app_settings.global_organization.name}")
|
||||
|
||||
self.stdout.write('Migrate finished')
|
@ -0,0 +1,25 @@
|
||||
# Generated by Django 5.0.6 on 2024-05-27 04:16
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('access', '0002_alter_team_organization'),
|
||||
('settings', '0005_create_settings_for_app'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='appsettings',
|
||||
name='software_categories_is_global',
|
||||
field=models.BooleanField(default=False, verbose_name='All Software Categories are global'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='appsettings',
|
||||
name='global_organization',
|
||||
field=models.ForeignKey(blank=True, default=None, help_text='Organization global items will be created in', null=True, on_delete=django.db.models.deletion.SET_DEFAULT, related_name='global_organization', to='access.organization'),
|
||||
),
|
||||
]
|
@ -54,9 +54,15 @@ class AppSettings(AppSettingsCommonFields, SaveHistory):
|
||||
default = False,
|
||||
)
|
||||
|
||||
software_categories_is_global = models.BooleanField (
|
||||
verbose_name = 'All Software Categories are global',
|
||||
blank= False,
|
||||
default = False,
|
||||
)
|
||||
|
||||
global_organization = models.ForeignKey(
|
||||
Organization,
|
||||
on_delete=models.CASCADE,
|
||||
on_delete=models.SET_DEFAULT,
|
||||
blank= True,
|
||||
default = None,
|
||||
null = True,
|
||||
@ -73,5 +79,6 @@ class AppSettings(AppSettingsCommonFields, SaveHistory):
|
||||
|
||||
__all__ = [
|
||||
'software_is_global',
|
||||
'software_categories_is_global',
|
||||
'global_organization',
|
||||
]
|
||||
|
Reference in New Issue
Block a user