mirror of https://github.com/doccano/doccano.git
Hironsan
2 years ago
8 changed files with 49 additions and 46 deletions
Split View
Diff Options
-
25backend/api/migrations/0029_auto_20220119_2333.py
-
6backend/api/models.py
-
3backend/api/serializers.py
-
2backend/api/tests/api/utils.py
-
2backend/api/tests/test_models.py
-
4backend/api/views/project.py
-
7backend/members/apps.py
-
46backend/members/signals.py
@ -0,0 +1,25 @@ |
|||
# Generated by Django 3.2.11 on 2022-01-19 23:33 |
|||
|
|||
from django.conf import settings |
|||
from django.db import migrations, models |
|||
import django.db.models.deletion |
|||
|
|||
|
|||
class Migration(migrations.Migration): |
|||
|
|||
dependencies = [ |
|||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), |
|||
('api', '0028_auto_20220111_0655'), |
|||
] |
|||
|
|||
operations = [ |
|||
migrations.RemoveField( |
|||
model_name='project', |
|||
name='users', |
|||
), |
|||
migrations.AddField( |
|||
model_name='project', |
|||
name='created_by', |
|||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), |
|||
), |
|||
] |
@ -1,48 +1,16 @@ |
|||
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 api.models import Project |
|||
from roles.models import Role |
|||
from .models import Member |
|||
|
|||
|
|||
@receiver(post_save, sender=Member) |
|||
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: |
|||
Member.objects.filter(user__in=user_ids, project=instance).delete() |
|||
elif action.startswith('post_add') and not reverse: |
|||
def add_administrator_on_project_creation(sender, instance: Project, created: bool, **kwargs): |
|||
# In the case of creating a project. |
|||
if created: |
|||
admin_role = Role.objects.get(name=settings.ROLE_PROJECT_ADMIN) |
|||
Member.objects.bulk_create( |
|||
[Member(role=admin_role, project=instance, user_id=user) |
|||
for user in user_ids |
|||
if not Member.objects.filter(project=instance, user_id=user).exists()] |
|||
Member.objects.create( |
|||
project=instance, |
|||
user=instance.created_by, |
|||
role=admin_role, |
|||
) |
|||
|
|||
|
|||
@receiver(pre_delete, sender=Member) |
|||
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() |
Write
Preview
Loading…
Cancel
Save