From b391bcbb9f1f2aff1f826a947f9182326f5d5fcf Mon Sep 17 00:00:00 2001 From: Jon Date: Thu, 14 Aug 2025 17:20:37 +0930 Subject: [PATCH] feat(core): add to migration signal system user and use for inventory objects ref: #948 closes #949 --- app/core/signal/centurion_model_migrate.py | 43 +++++++++++++++++-- .../centurion_erp/user/itam/device.md | 2 +- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/core/signal/centurion_model_migrate.py b/app/core/signal/centurion_model_migrate.py index df17afc0..17fcae19 100644 --- a/app/core/signal/centurion_model_migrate.py +++ b/app/core/signal/centurion_model_migrate.py @@ -1,4 +1,6 @@ from django.apps import apps +from django.conf import settings +from django.core.exceptions import ObjectDoesNotExist from django.db.models.signals import ( post_migrate, ) @@ -14,6 +16,31 @@ def centurion_model_migrate(sender, **kwargs): if sender.label != 'core': return + try: + + print('\n\nFetching System User.\n') + + user = apps.get_model(settings.AUTH_USER_MODEL).objects.get( + username = 'system' + ) + + if user.is_active: + print(' System user is set as "Active", disabling.\n') + user.is_active = False + + user.save() + + except ObjectDoesNotExist: + + print(' System user not found, creating.\n') + + user = apps.get_model(settings.AUTH_USER_MODEL).objects.create( + username = 'system', + first_name = 'System', + last_name = 'User', + is_active = False, + ) + print('\n\nCenturion Model Migration Signal.....\n') models: list[ dict ] = [ @@ -261,7 +288,7 @@ def centurion_model_migrate(sender, **kwargs): model_name = model.get_history_model_name( model ) ) - history = original_history.objects.filter().exclude( user = None ) + history = original_history.objects.filter() print(f' Found {len(history)} history entries to migrate.') @@ -269,18 +296,28 @@ def centurion_model_migrate(sender, **kwargs): try: + after = {} + if entry.after: + after = entry.after + entry_model = entry.model if hasattr(entry, 'child_model'): entry_model = entry.child_model + entry_user = entry.user + + if not entry_user: + + entry_user = user + migrated_history = audit_history.objects.create( organization = entry.organization, content_type = entry.content_type, model = entry_model, before = entry.before, - after = entry.after, + after = after, action = entry.action, - user = entry.user, + user = entry_user, created = entry.created ) diff --git a/docs/projects/centurion_erp/user/itam/device.md b/docs/projects/centurion_erp/user/itam/device.md index 1b28ad8d..152afcbb 100644 --- a/docs/projects/centurion_erp/user/itam/device.md +++ b/docs/projects/centurion_erp/user/itam/device.md @@ -81,7 +81,7 @@ The device can also have configuration defined. this configuration is intended t It's possible for a machine to be inventoried and have the report passed to the [inventory endpoint](../api.md#inventory-reports). This report will update the device within the interface and provides the option to use scheduled inventory gathering to keep the device up to date. -Inventory processing is conducted by a background worker. As soon as the inventory is uploaded, the inventory processing is added to the background worker queue. Further information about the background worker can be found within its [documentation](../core/index.md#background-worker) +Inventory processing is conducted by a background worker. As soon as the inventory is uploaded, the inventory processing is added to the background worker queue. Further information about the background worker can be found within its [documentation](../core/index.md#background-worker). All inventory objects history entries will be added by a user called `System User` regardless of the user that was used to authenticated to upload the inventory. !!! tip Inventory not uploading? review the task logs by navigating to `Settings -> Application -> Task Logs`