diff --git a/intranet/templatetags/__init__.py b/intranet/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/intranet/templatetags/intranet.py b/intranet/templatetags/intranet.py new file mode 100644 index 0000000..1508c7b --- /dev/null +++ b/intranet/templatetags/intranet.py @@ -0,0 +1,9 @@ +from django import template +from django.contrib.auth.models import Group + +register = template.Library() + + +@register.filter +def has_group(user, group_name): + return user.groups.filter(name=group_name).exists() diff --git a/intranet/test_files/doc_etudiant.pdf b/intranet/test_files/doc_etudiant.pdf new file mode 100644 index 0000000..ad0194f Binary files /dev/null and b/intranet/test_files/doc_etudiant.pdf differ diff --git a/intranet/test_files/doc_prof.pdf b/intranet/test_files/doc_prof.pdf new file mode 100644 index 0000000..ad0194f Binary files /dev/null and b/intranet/test_files/doc_prof.pdf differ diff --git a/intranet/tests.py b/intranet/tests.py new file mode 100644 index 0000000..08d9d65 --- /dev/null +++ b/intranet/tests.py @@ -0,0 +1,68 @@ +import os +import shutil +import tempfile + +from django.contrib.auth.models import Group, User +from django.core.files import File +from django.test import TestCase, override_settings +from django.urls import reverse + +from cms.models import Module +from.models import IntranetDoc + +media_dir = tempfile.mkdtemp() + + +@override_settings(MEDIA_ROOT=media_dir) +class IntranetTests(TestCase): + fixtures = ['enseignant.json', 'domaine.json', 'processus.json', 'module.json'] + + @classmethod + def tearDownClass(cls): + shutil.rmtree(media_dir) + + def test_document_view(self): + # Create IntranetDoc instances + module1 = Module.objects.get(code="M01") + module2 = Module.objects.get(code="M02") + doc_etudiant_path = os.path.join(os.path.dirname(__file__), 'test_files', 'doc_etudiant.pdf') + doc_prof_path = os.path.join(os.path.dirname(__file__), 'test_files', 'doc_prof.pdf') + with open(doc_etudiant_path, 'rb') as fh: + IntranetDoc.objects.create( + doc=File(fh, name='doc_etudiant.pdf'), + module=module1, published=True, authorization=1 + ) + IntranetDoc.objects.create( + doc=File(fh, name='doc_etudiant2.pdf'), + module=module1, published=False, authorization=1 + ) + with open(doc_prof_path, 'rb') as fh: + IntranetDoc.objects.create( + doc=File(fh, name='doc_prof.pdf'), + module=module1, published=True, authorization=2 + ) + IntranetDoc.objects.create( + doc=File(fh, name='doc_prof2.pdf'), + module=module2, published=True, authorization=2 + ) + # Create groups and users + gr_stud1 = Group.objects.create(name='Student_1_year') + etudiant = User.objects.create(username='student') + etudiant.groups.add(gr_stud1) + gr_profs = Group.objects.create(name='prof') + prof = User.objects.create(username='prof') + prof.groups.add(gr_profs) + + # Test document visibility by users + self.client.force_login(etudiant) + response = self.client.get(reverse('intranet-list', args=[module1.pk])) + self.assertContains(response, 'doc_etudiant.pdf') + self.assertNotContains(response, 'doc_etudiant2.pdf') # Not published + self.assertNotContains(response, 'doc_prof.pdf') + + self.client.force_login(prof) + response = self.client.get(reverse('intranet-list', args=[module1.pk])) + self.assertContains(response, 'doc_etudiant.pdf') + self.assertNotContains(response, 'doc_etudiant2.pdf') # Not published + self.assertContains(response, 'doc_prof.pdf') + self.assertNotContains(response, 'doc_prof2.pdf') # Other module diff --git a/intranet/urls.py b/intranet/urls.py index a4f6d8f..6ea9f1a 100644 --- a/intranet/urls.py +++ b/intranet/urls.py @@ -3,5 +3,5 @@ from intranet import views urlpatterns = [ - path('list/', views.IntranetListView.as_view(), name='intranet-list'), + path('list//', views.IntranetListView.as_view(), name='intranet-list'), ] diff --git a/intranet/views.py b/intranet/views.py index 65c34ed..c78d43d 100644 --- a/intranet/views.py +++ b/intranet/views.py @@ -1,4 +1,5 @@ from django.contrib.auth.mixins import LoginRequiredMixin +from django.shortcuts import get_object_or_404 from django.views.generic import ListView from cms.models import Module @@ -12,7 +13,7 @@ class IntranetListView(LoginRequiredMixin, ListView): def get_queryset(self): groups = self.request.user.groups.values_list('name',flat=True) - qs = IntranetDoc.objects.filter(published=True) + qs = IntranetDoc.objects.filter(module=self.kwargs['module'], published=True) if self.request.user.is_superuser: qs = qs.filter(authorization__in=[1,2,3]) elif 'prof' in groups: @@ -32,3 +33,8 @@ class IntranetListView(LoginRequiredMixin, ListView): else: qs = qs.none() return qs + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['module'] = get_object_or_404(Module, pk=self.kwargs['module']) + return context diff --git a/templates/cms/module_detail.html b/templates/cms/module_detail.html index 1b11f3f..5477df0 100644 --- a/templates/cms/module_detail.html +++ b/templates/cms/module_detail.html @@ -87,7 +87,7 @@

Imprimer en PDF -     Documents de cours (connexion requise) +     Documents de cours (connexion requise)

{% endblock %} diff --git a/templates/intranet/list.html b/templates/intranet/list.html index 3b45da7..cf29c48 100644 --- a/templates/intranet/list.html +++ b/templates/intranet/list.html @@ -1,32 +1,38 @@ {% extends "cms/base_site.html" %} -{% load i18n static %} - -{% block extrastyle %} -{{ block.super }} - -{% endblock %} +{% load i18n static intranet %} {% block content %}
-

Documents de cours

- {% regroup object_list by module as module_list %} - +
+ {% if user|has_group:"prof" or user.is_staff %} +
+

Documents ENSEIGNANT

+
    + {% for doc in object_list %} + {% if doc.get_authorization_display == 'prof' %} +
  1. {{ doc.doc.name }}
  2. + {% endif %} + {% empty %} +

    Aucun document disponible.

    + {% endfor %} +
+
+ {% endif %} + {% if user.is_staff %} -
+

Gestion des documents

{% endif %}