Repository: 3cn-ecn/nantralPlatform

Issue:
Ajouter les parents dans les groupes
Ajouter un field parent dans les groupes pour pouvoir faire des liens entre groupes


Discussion:

Resolving patch:
diff --git a/server/apps/group/migrations/0004_club_parent.py b/server/apps/group/migrations/0004_club_parent.py
new file mode 100644
--- /dev/null
+++ b/server/apps/group/migrations/0004_club_parent.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.0.5 on 2020-06-02 07:16
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('group', '0003_auto_20200601_2109'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='club',
+            name='parent',
+            field=models.SlugField(blank=True, max_length=40, null=True),
+        ),
+    ]
diff --git a/server/apps/group/models.py b/server/apps/group/models.py
--- a/server/apps/group/models.py
+++ b/server/apps/group/models.py
@@ -24,6 +24,7 @@ class Group(models.Model):
     members = models.ManyToManyField(Student, verbose_name='Membres du groupe', related_name='members')
     logo = models.ImageField(verbose_name='Logo du groupe', blank=True, null=True, upload_to=path_and_rename)
     slug = models.SlugField(max_length=40, unique=True, blank=True)
+    parent = models.SlugField(max_length=40, blank=True, null=True)
     class Meta:
         abstract = True
 
@@ -33,8 +34,13 @@ def __str__(self):
     def is_admin(self, user: User) -> bool:
         """Indicates if a user is admin."""
         student = Student.objects.filter(user=user).first()
-        return student in self.admins.all()
+        return student in self.admins.all() or self.get_parent().is_admin(user)
     
+    @property
+    def get_parent(self):
+        """Get the parent group of this group."""
+        return Group.get_group_by_slug(self.parent)
+
     @staticmethod
     def get_group_by_slug(slug:  str):
         """Get a group from a slug."""
diff --git a/server/apps/group/views.py b/server/apps/group/views.py
--- a/server/apps/group/views.py
+++ b/server/apps/group/views.py
@@ -25,8 +25,10 @@ class UpdateClubView(UpdateView):
 class UpdateGroupEventsView(UserPassesTestMixin, View):
     template_name = 'group/club_events_update.html'
     def test_func(self):
-        group = Group.get_group_by_slug(self.kwargs['group_slug'])
-        return group.is_admin(self.request.user)
+        if self.request.user.is_authenticated:
+            group = Group.get_group_by_slug(self.kwargs['group_slug'])
+            return group.is_admin(self.request.user)
+        return False
     def get_context_data(self, **kwargs):
         context = {}
         context['object'] = Group.get_group_by_slug(kwargs['group_slug'])
@@ -44,8 +46,10 @@ def post(self, request,  group_slug):
 class UpdateGroupMembersView(UserPassesTestMixin, View):
     template_name = 'group/club_members_update.html'
     def test_func(self):
-        group = Group.get_group_by_slug(self.kwargs['group_slug'])
-        return group.is_admin(self.request.user)
+        if self.request.user.is_authenticated:
+            group = Group.get_group_by_slug(self.kwargs['group_slug'])
+            return group.is_admin(self.request.user)
+        return False
 
     def get_context_data(self, **kwargs):
         context = {}
@@ -71,7 +75,7 @@ def get_context_data(self, **kwargs):
         context = super().get_context_data(**kwargs)
         members = NamedMembership.objects.filter(group=self.object)
         context['members'] = members
-        context['is_admin'] = self.object.is_admin(self.request.user)
+        context['is_admin'] = self.object.is_admin(self.request.user) if self.request.user.is_authenticated else False
         context['events'] = Event.objects.filter(group=self.object.slug)
         return context
 


Test patch:
diff --git a/server/apps/group/tests.py b/server/apps/group/tests.py
--- a/server/apps/group/tests.py
+++ b/server/apps/group/tests.py
@@ -1,3 +1,30 @@
 from django.test import TestCase
+from apps.utils.utest import TestMixin
+from django.urls import reverse
+from rest_framework import status
+
+from .models import Club
+
+class TestGroups(TestCase, TestMixin):
+    def setUp(self):
+        self.user_setup()
+
+    def test_create_club(self):
+        Club.objects.create(name='TestClub')
+        self.assertEqual(len(Club.objects.all()),1)
+    
+    def test_club_views(self):
+        Club.objects.create(name='TestClub')
+        club = Club.objects.all().first()
+        print(club)
+        url = reverse('group:detail', args=[club.pk])
+        resp = self.client.get(url)
+        self.assertEqual(resp.status_code, status.HTTP_200_OK)
+
+        url = reverse('group:update', args=[club.pk])
+        resp = self.client.get(url)
+        self.assertEqual(resp.status_code, status.HTTP_200_OK)
+
+    def test_add_member(self):
+        pass
 
-# Create your tests here.