diff --git a/app/core/management/commands/manufacturer.py b/app/core/management/commands/manufacturer.py new file mode 100644 index 00000000..a0adcaf8 --- /dev/null +++ b/app/core/management/commands/manufacturer.py @@ -0,0 +1,64 @@ +from django.core.management.base import BaseCommand +from django.db.models import Q +from django.utils import timezone + +from core.models.manufacturer import Manufacturer + +from settings.models.app_settings import AppSettings + + + +class Command(BaseCommand): + help = 'Manage Common item Manufacturer for the entire application.' + + + def add_arguments(self, parser): + parser.add_argument('-g', '--global', action='store_true', help='Sets all manufacturer to be global (manufacturers will be migrated to global organization if set)') + parser.add_argument('-m', '--migrate', action='store_true', help='Migrate existing global manufacturers to global organization') + + + def handle(self, *args, **kwargs): + + if kwargs['global']: + + softwares = Manufacturer.objects.filter(is_global = False) + + self.stdout.write('Running global') + + self.stdout.write(f'found manufacturer {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 = Manufacturer.objects.filter( + ~Q(organization = app_settings.global_organization) + | + Q(is_global = False) + & + Q(organization=app_settings.global_organization), + ) + + self.stdout.write(f'found manufacturer {str(len(softwares))} to migrate') + + for software in softwares: + + software.clean() + software.save() + + self.stdout.write(f"Migrating manufacturer {software} to organization {app_settings.global_organization.name}") + + self.stdout.write('Migrate finished') diff --git a/app/core/models/manufacturer.py b/app/core/models/manufacturer.py index bff186be..8f831eea 100644 --- a/app/core/models/manufacturer.py +++ b/app/core/models/manufacturer.py @@ -6,6 +6,8 @@ from access.models import TenancyObject from core.mixin.history_save import SaveHistory +from settings.models.app_settings import AppSettings + class ManufacturerCommonFields(models.Model): class Meta: @@ -42,6 +44,16 @@ class Manufacturer(TenancyObject, ManufacturerCommonFields, SaveHistory): slug = AutoSlugField() + def clean(self): + + app_settings = AppSettings.objects.get(owner_organization=None) + + if app_settings.manufacturer_is_global: + + self.organization = app_settings.global_organization + self.is_global = app_settings.manufacturer_is_global + + def __str__(self): return self.name diff --git a/app/settings/migrations/0009_appsettings_manufacturer_is_global.py b/app/settings/migrations/0009_appsettings_manufacturer_is_global.py new file mode 100644 index 00000000..6fb06b0f --- /dev/null +++ b/app/settings/migrations/0009_appsettings_manufacturer_is_global.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2024-05-27 05:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('settings', '0008_appsettings_device_type_is_global'), + ] + + operations = [ + migrations.AddField( + model_name='appsettings', + name='manufacturer_is_global', + field=models.BooleanField(default=False, verbose_name='All Manufacturer / Publishers are global'), + ), + ] diff --git a/app/settings/models/app_settings.py b/app/settings/models/app_settings.py index 0f14d360..52ee3382 100644 --- a/app/settings/models/app_settings.py +++ b/app/settings/models/app_settings.py @@ -60,6 +60,12 @@ class AppSettings(AppSettingsCommonFields, SaveHistory): default = False, ) + manufacturer_is_global = models.BooleanField ( + verbose_name = 'All Manufacturer / Publishers are global', + blank= False, + default = False, + ) + software_is_global = models.BooleanField ( verbose_name = 'All Software is global', blank= False, @@ -92,6 +98,7 @@ class AppSettings(AppSettingsCommonFields, SaveHistory): __all__ = [ 'device_model_is_global', 'device_type_is_global', + 'manufacturer_is_global', 'software_is_global', 'software_categories_is_global', 'global_organization',