diff --git a/backend/projects/apps.py b/backend/projects/apps.py index 3d71e6b9..afae4983 100644 --- a/backend/projects/apps.py +++ b/backend/projects/apps.py @@ -1,18 +1,6 @@ -import importlib - from django.apps import AppConfig -from django.db.models.signals import post_save class ProjectsConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'projects' - - def ready(self): - importlib.import_module('projects.signals') - from projects.models import Project - from .signals import add_administrator_on_project_creation - - # Registering signals with the subclasses of project. - for project in Project.__subclasses__(): - post_save.connect(add_administrator_on_project_creation, project) diff --git a/backend/projects/models.py b/backend/projects/models.py index a0cbada1..cc64afb5 100644 --- a/backend/projects/models.py +++ b/backend/projects/models.py @@ -41,6 +41,14 @@ class Project(PolymorphicModel): collaborative_annotation = models.BooleanField(default=False) single_class_classification = models.BooleanField(default=False) + def add_admin(self): + admin_role = Role.objects.get(name=settings.ROLE_PROJECT_ADMIN) + Member.objects.create( + project=self, + user=self.created_by, + role=admin_role, + ) + @property @abc.abstractmethod def is_text_project(self) -> bool: diff --git a/backend/projects/signals.py b/backend/projects/signals.py deleted file mode 100644 index 1030be33..00000000 --- a/backend/projects/signals.py +++ /dev/null @@ -1,16 +0,0 @@ -from django.conf import settings - -from projects.models import Project -from roles.models import Role -from .models import Member - - -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.create( - project=instance, - user=instance.created_by, - role=admin_role, - ) diff --git a/backend/projects/tests/test_project.py b/backend/projects/tests/test_project.py index 10fda35f..68cb1f76 100644 --- a/backend/projects/tests/test_project.py +++ b/backend/projects/tests/test_project.py @@ -1,7 +1,9 @@ +from django.conf import settings from rest_framework import status from rest_framework.reverse import reverse from api.tests.utils import CRUDMixin +from projects.models import Member from projects.tests.utils import prepare_project from roles.tests.utils import create_default_roles from users.tests.utils import make_user @@ -48,6 +50,15 @@ class TestProjectCreate(CRUDMixin): response = self.assert_create(self.user, status.HTTP_201_CREATED) self.assertEqual(response.data['name'], self.data['name']) + def test_exists_project_administrator(self): + self.user.is_staff = True + self.user.save() + response = self.assert_create(self.user, status.HTTP_201_CREATED) + members = Member.objects.filter(project=response.data['id']) + self.assertEqual(members.count(), 1) + member = members.first() + self.assertEqual(member.role.name, settings.ROLE_PROJECT_ADMIN) + def test_denies_non_staff_user_to_create_project(self): self.assert_create(self.user, status.HTTP_403_FORBIDDEN) diff --git a/backend/projects/views/project.py b/backend/projects/views/project.py index 6f26626e..9ed92d42 100644 --- a/backend/projects/views/project.py +++ b/backend/projects/views/project.py @@ -26,7 +26,8 @@ class ProjectList(generics.ListCreateAPIView): return Project.objects.filter(role_mappings__user=self.request.user) def perform_create(self, serializer): - serializer.save(created_by=self.request.user) + project = serializer.save(created_by=self.request.user) + project.add_admin() def delete(self, request, *args, **kwargs): delete_ids = request.data['ids']