2021-07-08 06:23:18 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
from django.apps import apps
|
|
|
|
from simple_history.signals import pre_create_historical_record
|
2021-09-16 02:36:24 +00:00
|
|
|
from django.db.models.signals import post_save, pre_save, pre_delete
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
from common.utils import get_logger
|
|
|
|
from ..models import AuthBook, SystemUser
|
|
|
|
|
|
|
|
AuthBookHistory = apps.get_model('assets', 'HistoricalAuthBook')
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_create_historical_record, sender=AuthBookHistory)
|
2021-07-15 02:08:50 +00:00
|
|
|
def pre_create_historical_record_callback(sender, history_instance=None, **kwargs):
|
2021-07-08 06:23:18 +00:00
|
|
|
attrs_to_copy = ['username', 'password', 'private_key']
|
|
|
|
|
|
|
|
for attr in attrs_to_copy:
|
|
|
|
if getattr(history_instance, attr):
|
|
|
|
continue
|
2021-07-13 10:03:07 +00:00
|
|
|
try:
|
|
|
|
system_user = history_instance.systemuser
|
|
|
|
except SystemUser.DoesNotExist:
|
|
|
|
continue
|
|
|
|
if not system_user:
|
2021-07-08 06:23:18 +00:00
|
|
|
continue
|
|
|
|
system_user_attr_value = getattr(history_instance.systemuser, attr)
|
|
|
|
if system_user_attr_value:
|
|
|
|
setattr(history_instance, attr, system_user_attr_value)
|
|
|
|
|
|
|
|
|
2021-09-16 02:36:24 +00:00
|
|
|
@receiver(pre_delete, sender=AuthBook)
|
2021-09-15 11:11:19 +00:00
|
|
|
def on_authbook_post_delete(sender, instance, **kwargs):
|
|
|
|
instance.remove_asset_admin_user_if_need()
|
|
|
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
@receiver(post_save, sender=AuthBook)
|
2021-09-24 03:45:39 +00:00
|
|
|
def on_authbook_post_create(sender, instance, created, **kwargs):
|
2021-09-15 11:11:19 +00:00
|
|
|
instance.sync_to_system_user_account()
|
2021-09-24 03:45:39 +00:00
|
|
|
if created:
|
2021-10-28 02:56:39 +00:00
|
|
|
pass
|
|
|
|
# # 不再自动更新资产管理用户,只允许用户手动指定。
|
2021-09-24 03:45:39 +00:00
|
|
|
# 只在创建时进行更新资产的管理用户
|
2021-10-28 02:56:39 +00:00
|
|
|
# instance.update_asset_admin_user_if_need()
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_save, sender=AuthBook)
|
|
|
|
def on_authbook_pre_create(sender, instance, **kwargs):
|
|
|
|
# 升级版本号
|
2021-08-19 03:13:36 +00:00
|
|
|
instance.version += 1
|
2021-07-15 02:08:50 +00:00
|
|
|
# 即使在 root 组织也不怕
|
|
|
|
instance.org_id = instance.asset.org_id
|