Only load intranet documents for a single module

This commit is contained in:
alazo 2018-09-01 15:53:09 +02:00 committed by Claude Paroz
parent ff88669ae4
commit 000e74d31a
9 changed files with 113 additions and 24 deletions

View file

View file

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

Binary file not shown.

Binary file not shown.

68
intranet/tests.py Normal file
View file

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

View file

@ -3,5 +3,5 @@ from intranet import views
urlpatterns = [
path('list/', views.IntranetListView.as_view(), name='intranet-list'),
path('list/<int:module>/', views.IntranetListView.as_view(), name='intranet-list'),
]

View file

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