2018-08-13 10:26:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
from django.db.models.signals import m2m_changed
|
2018-08-13 10:26:13 +00:00
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
|
|
from .models import Organization
|
2019-06-28 14:07:22 +00:00
|
|
|
from .hands import set_current_org, current_org, Node, get_current_org
|
2018-09-03 03:24:25 +00:00
|
|
|
from perms.models import AssetPermission
|
|
|
|
from users.models import UserGroup
|
2018-08-13 10:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Organization)
|
2018-08-14 02:16:38 +00:00
|
|
|
def on_org_create_or_update(sender, instance=None, created=False, **kwargs):
|
|
|
|
if instance:
|
2019-06-28 14:07:22 +00:00
|
|
|
old_org = get_current_org()
|
2018-08-14 02:16:38 +00:00
|
|
|
set_current_org(instance)
|
2019-08-23 10:23:07 +00:00
|
|
|
node_root = Node.org_root()
|
2018-08-14 02:16:38 +00:00
|
|
|
if node_root.value != instance.name:
|
|
|
|
node_root.value = instance.name
|
|
|
|
node_root.save()
|
|
|
|
set_current_org(old_org)
|
|
|
|
|
2018-08-13 10:26:13 +00:00
|
|
|
if instance and not created:
|
2019-03-11 02:06:45 +00:00
|
|
|
instance.expire_cache()
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=Organization.users.through)
|
|
|
|
def on_org_user_changed(sender, instance=None, **kwargs):
|
|
|
|
if isinstance(instance, Organization):
|
|
|
|
old_org = current_org
|
|
|
|
set_current_org(instance)
|
|
|
|
if kwargs['action'] == 'pre_remove':
|
|
|
|
users = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
|
|
|
for user in users:
|
|
|
|
perms = AssetPermission.objects.filter(users=user)
|
|
|
|
user_groups = UserGroup.objects.filter(users=user)
|
|
|
|
for perm in perms:
|
|
|
|
perm.users.remove(user)
|
|
|
|
for user_group in user_groups:
|
|
|
|
user_group.users.remove(user)
|
|
|
|
set_current_org(old_org)
|
2019-07-02 14:08:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=Organization.admins.through)
|
|
|
|
def on_org_admin_change(sender, **kwargs):
|
|
|
|
Organization._user_admin_orgs = None
|