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

View file

@ -87,7 +87,7 @@
</table>
<p>
<a href="{% url 'module-pdf' object.id %}">Imprimer en PDF</a>
&nbsp;&nbsp; &nbsp;<a href="{% url 'intranet-list' %}">Documents de cours (connexion requise)</a>
&nbsp;&nbsp; &nbsp;<a href="{% url 'intranet-list' object.id %}">Documents de cours (connexion requise)</a>
</p>
</div>
{% endblock %}

View file

@ -1,32 +1,38 @@
{% extends "cms/base_site.html" %}
{% load i18n static %}
{% block extrastyle %}
{{ block.super }}
<style type="text/css">
ul li {float: none;}
</style>
{% endblock %}
{% load i18n static intranet %}
{% block content %}
<div id="content-main">
<h1>Documents de cours</h1>
{% regroup object_list by module as module_list %}
<ul>
{% for module in module_list %}
<li><h3>{{ module.grouper }}</h3>
<h1>Module {{ module.url }} &mdash; Documents de cours</h1>
<div style="float: left; width: 500px">
<h3>Documents Étudiant</h3>
<ol>
{% for doc in module.list %}
<li><a href="{{ doc.doc.url }}">{{ doc.doc.name }} ({{ doc.get_authorization_display }})</a></li>
{% for doc in object_list %}
{% if doc.get_authorization_display == 'étudiant' %}
<li><a href="{{ doc.doc.url }}">{{ doc.doc.name }}</a></li>
{% endif %}
{% empty %}
<p><i>Aucun document disponible.</i></p>
{% endfor %}
</ol>
</li>
</div>
{% if user|has_group:"prof" or user.is_staff %}
<div style="margin-left: 600px; width: 500px">
<h3>Documents ENSEIGNANT</h3>
<ol>
{% for doc in object_list %}
{% if doc.get_authorization_display == 'prof' %}
<li><a href="{{ doc.doc.url }}">{{ doc.doc.name }}</a></li>
{% endif %}
{% empty %}
<p><i>Désolé, aucun document disponible pour le moment.</i></p>
<p><i>Aucun document disponible.</i></p>
{% endfor %}
</ul>
</ol>
</div>
{% endif %}
{% if user.is_staff %}
<hr>
<hr style="margin-top: 100px">
<p><a href="{% url 'admin:intranet_intranetdoc_changelist' %}">Gestion des documents</a></p>
{% endif %}
</div>