2022-09-22 10:47:16 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
2022-11-01 10:40:42 +00:00
|
|
|
from django.dispatch import receiver
|
2022-12-07 07:09:01 +00:00
|
|
|
from django.utils.functional import LazyObject
|
2022-09-22 10:47:16 +00:00
|
|
|
|
2023-05-09 11:46:34 +00:00
|
|
|
from accounts.models import Account
|
2022-09-22 10:47:16 +00:00
|
|
|
from common.signals import django_ready
|
|
|
|
from common.utils import get_logger
|
2022-12-07 07:09:01 +00:00
|
|
|
from common.utils.connection import RedisPubSub
|
2022-11-08 02:41:06 +00:00
|
|
|
from orgs.utils import tmp_to_builtin_org
|
2023-05-09 11:46:34 +00:00
|
|
|
from users.models import User
|
2023-01-16 11:02:09 +00:00
|
|
|
from ..models import Applet, AppletHost
|
2023-05-09 11:46:34 +00:00
|
|
|
from ..tasks import applet_host_generate_accounts
|
2023-01-16 11:02:09 +00:00
|
|
|
from ..utils import DBPortManager
|
2022-09-22 10:47:16 +00:00
|
|
|
|
|
|
|
db_port_manager: DBPortManager
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2022-11-01 10:40:42 +00:00
|
|
|
@receiver(post_save, sender=AppletHost)
|
|
|
|
def on_applet_host_create(sender, instance, created=False, **kwargs):
|
2022-11-01 12:37:04 +00:00
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
applets = Applet.objects.all()
|
|
|
|
instance.applets.set(applets)
|
2022-11-01 10:40:42 +00:00
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
applet_host_change_pub_sub.publish(True)
|
2023-07-28 02:41:37 +00:00
|
|
|
if instance.auto_create_accounts:
|
|
|
|
applet_host_generate_accounts.delay(instance.id)
|
2022-12-07 07:09:01 +00:00
|
|
|
|
2022-11-01 10:40:42 +00:00
|
|
|
|
2023-05-09 11:46:34 +00:00
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
def on_user_create_create_account(sender, instance, created=False, **kwargs):
|
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
|
|
|
|
with tmp_to_builtin_org(system=1):
|
|
|
|
applet_hosts = AppletHost.objects.all()
|
|
|
|
for host in applet_hosts:
|
2023-07-28 02:41:37 +00:00
|
|
|
if not host.auto_create_accounts:
|
|
|
|
continue
|
2023-05-09 11:46:34 +00:00
|
|
|
host.generate_private_accounts_by_usernames([instance.username])
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=User)
|
|
|
|
def on_user_delete_remove_account(sender, instance, **kwargs):
|
|
|
|
with tmp_to_builtin_org(system=1):
|
|
|
|
applet_hosts = AppletHost.objects.all().values_list('id', flat=True)
|
|
|
|
accounts = Account.objects.filter(asset_id__in=applet_hosts, username=instance.username)
|
|
|
|
accounts.delete()
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
@receiver(post_delete, sender=AppletHost)
|
|
|
|
def on_applet_host_delete(sender, instance, **kwargs):
|
|
|
|
applet_host_change_pub_sub.publish(True)
|
|
|
|
|
|
|
|
|
2022-11-01 10:40:42 +00:00
|
|
|
@receiver(post_save, sender=Applet)
|
|
|
|
def on_applet_create(sender, instance, created=False, **kwargs):
|
2022-11-01 12:37:04 +00:00
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
hosts = AppletHost.objects.all()
|
|
|
|
instance.hosts.set(hosts)
|
2022-12-07 07:09:01 +00:00
|
|
|
applet_host_change_pub_sub.publish(True)
|
|
|
|
|
2022-12-05 07:03:21 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
@receiver(post_delete, sender=Applet)
|
|
|
|
def on_applet_delete(sender, instance, **kwargs):
|
|
|
|
applet_host_change_pub_sub.publish(True)
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AppletHostPubSub(LazyObject):
|
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = RedisPubSub('fm.applet_host_change')
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(django_ready)
|
|
|
|
def subscribe_applet_host_change(sender, **kwargs):
|
|
|
|
logger.debug("Start subscribe for expire node assets id mapping from memory")
|
|
|
|
|
|
|
|
def on_change(message):
|
|
|
|
from terminal.connect_methods import ConnectMethodUtil
|
|
|
|
ConnectMethodUtil.refresh_methods()
|
|
|
|
|
|
|
|
applet_host_change_pub_sub.subscribe(on_change)
|
|
|
|
|
|
|
|
|
|
|
|
applet_host_change_pub_sub = AppletHostPubSub()
|