Browse Source

Remove unnecessary signal

pull/1671/head
Hironsan 2 years ago
parent
commit
d6e8e12abc
5 changed files with 21 additions and 29 deletions
  1. 12
      backend/projects/apps.py
  2. 8
      backend/projects/models.py
  3. 16
      backend/projects/signals.py
  4. 11
      backend/projects/tests/test_project.py
  5. 3
      backend/projects/views/project.py

12
backend/projects/apps.py

@ -1,18 +1,6 @@
import importlib
from django.apps import AppConfig from django.apps import AppConfig
from django.db.models.signals import post_save
class ProjectsConfig(AppConfig): class ProjectsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField' default_auto_field = 'django.db.models.BigAutoField'
name = 'projects' 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)

8
backend/projects/models.py

@ -41,6 +41,14 @@ class Project(PolymorphicModel):
collaborative_annotation = models.BooleanField(default=False) collaborative_annotation = models.BooleanField(default=False)
single_class_classification = 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 @property
@abc.abstractmethod @abc.abstractmethod
def is_text_project(self) -> bool: def is_text_project(self) -> bool:

16
backend/projects/signals.py

@ -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,
)

11
backend/projects/tests/test_project.py

@ -1,7 +1,9 @@
from django.conf import settings
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from api.tests.utils import CRUDMixin from api.tests.utils import CRUDMixin
from projects.models import Member
from projects.tests.utils import prepare_project from projects.tests.utils import prepare_project
from roles.tests.utils import create_default_roles from roles.tests.utils import create_default_roles
from users.tests.utils import make_user from users.tests.utils import make_user
@ -48,6 +50,15 @@ class TestProjectCreate(CRUDMixin):
response = self.assert_create(self.user, status.HTTP_201_CREATED) response = self.assert_create(self.user, status.HTTP_201_CREATED)
self.assertEqual(response.data['name'], self.data['name']) 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): def test_denies_non_staff_user_to_create_project(self):
self.assert_create(self.user, status.HTTP_403_FORBIDDEN) self.assert_create(self.user, status.HTTP_403_FORBIDDEN)

3
backend/projects/views/project.py

@ -26,7 +26,8 @@ class ProjectList(generics.ListCreateAPIView):
return Project.objects.filter(role_mappings__user=self.request.user) return Project.objects.filter(role_mappings__user=self.request.user)
def perform_create(self, serializer): 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): def delete(self, request, *args, **kwargs):
delete_ids = request.data['ids'] delete_ids = request.data['ids']

Loading…
Cancel
Save