Browse Source

Move signals from models.py to signals.py

pull/1349/head
Hironsan 3 years ago
parent
commit
1b00e9bd70
3 changed files with 50 additions and 69 deletions
  1. 4
      backend/api/apps.py
  2. 69
      backend/api/models.py
  3. 46
      backend/api/signals.py

4
backend/api/apps.py

@ -3,3 +3,7 @@ from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'
verbose_name = 'Api'
def ready(self):
import api.signals

69
backend/api/models.py

@ -1,12 +1,9 @@
import string
from auto_labeling_pipeline.models import RequestModelFactory
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import m2m_changed, post_save, pre_delete
from django.dispatch import receiver
from polymorphic.models import PolymorphicModel
from .managers import (AnnotationManager, RoleMappingManager,
@ -253,72 +250,6 @@ class RoleMapping(models.Model):
unique_together = ("user", "project")
@receiver(post_save, sender=RoleMapping)
def add_linked_project(sender, instance, created, **kwargs):
if not created:
return
userInstance = instance.user
projectInstance = instance.project
if userInstance and projectInstance:
user = User.objects.get(pk=userInstance.pk)
project = Project.objects.get(pk=projectInstance.pk)
user.projects.add(project)
user.save()
# @receiver(post_save)
# def add_superusers_to_project(sender, instance, created, **kwargs):
# if not created:
# return
# if sender not in Project.__subclasses__():
# return
# superusers = User.objects.filter(is_superuser=True)
# admin_role = Role.objects.filter(name=settings.ROLE_PROJECT_ADMIN).first()
# if superusers and admin_role:
# RoleMapping.objects.bulk_create(
# [RoleMapping(role_id=admin_role.id, user_id=superuser.id, project_id=instance.id)
# for superuser in superusers]
# )
#
#
# @receiver(post_save, sender=User)
# def add_new_superuser_to_projects(sender, instance, created, **kwargs):
# if created and instance.is_superuser:
# admin_role = Role.objects.filter(name=settings.ROLE_PROJECT_ADMIN).first()
# projects = Project.objects.all()
# if admin_role and projects:
# RoleMapping.objects.bulk_create(
# [RoleMapping(role_id=admin_role.id, user_id=instance.id, project_id=project.id)
# for project in projects]
# )
@receiver(m2m_changed, sender=Project.users.through)
def remove_mapping_on_remove_user_from_project(sender, instance, action, reverse, **kwargs):
# if reverse is True, pk_set is project_ids and instance is user.
# else, pk_set is user_ids and instance is project.
user_ids = kwargs['pk_set']
if action.startswith('post_remove') and not reverse:
RoleMapping.objects.filter(user__in=user_ids, project=instance).delete()
elif action.startswith('post_add') and not reverse:
admin_role = Role.objects.get(name=settings.ROLE_PROJECT_ADMIN)
RoleMapping.objects.bulk_create(
[RoleMapping(role=admin_role, project=instance, user_id=user)
for user in user_ids
if not RoleMapping.objects.filter(project=instance, user_id=user).exists()]
)
@receiver(pre_delete, sender=RoleMapping)
def delete_linked_project(sender, instance, using, **kwargs):
userInstance = instance.user
projectInstance = instance.project
if userInstance and projectInstance:
user = User.objects.get(pk=userInstance.pk)
project = Project.objects.get(pk=projectInstance.pk)
user.projects.remove(project)
user.save()
class AutoLabelingConfig(models.Model):
model_name = models.CharField(max_length=100)
model_attrs = models.JSONField(default=dict)

46
backend/api/signals.py

@ -0,0 +1,46 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import m2m_changed, post_save, pre_delete
from django.dispatch import receiver
from .models import Project, Role, RoleMapping
@receiver(post_save, sender=RoleMapping)
def add_linked_project(sender, instance, created, **kwargs):
if not created:
return
userInstance = instance.user
projectInstance = instance.project
if userInstance and projectInstance:
user = User.objects.get(pk=userInstance.pk)
project = Project.objects.get(pk=projectInstance.pk)
user.projects.add(project)
user.save()
@receiver(m2m_changed, sender=Project.users.through)
def remove_mapping_on_remove_user_from_project(sender, instance, action, reverse, **kwargs):
# if reverse is True, pk_set is project_ids and instance is user.
# else, pk_set is user_ids and instance is project.
user_ids = kwargs['pk_set']
if action.startswith('post_remove') and not reverse:
RoleMapping.objects.filter(user__in=user_ids, project=instance).delete()
elif action.startswith('post_add') and not reverse:
admin_role = Role.objects.get(name=settings.ROLE_PROJECT_ADMIN)
RoleMapping.objects.bulk_create(
[RoleMapping(role=admin_role, project=instance, user_id=user)
for user in user_ids
if not RoleMapping.objects.filter(project=instance, user_id=user).exists()]
)
@receiver(pre_delete, sender=RoleMapping)
def delete_linked_project(sender, instance, using, **kwargs):
userInstance = instance.user
projectInstance = instance.project
if userInstance and projectInstance:
user = User.objects.get(pk=userInstance.pk)
project = Project.objects.get(pk=projectInstance.pk)
user.projects.remove(project)
user.save()
Loading…
Cancel
Save