From f67bc9b8c746f6d05232343dcb6d6d19362e59d5 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Wed, 6 Mar 2019 09:03:22 +0100
Subject: [PATCH] Start work on permissions for forums

---
 forums/admin.py                              |  4 +++-
 forums/managers.py                           |  1 +
 forums/migrations/0002_auto_20190306_0807.py | 19 +++++++++++++++++++
 forums/models.py                             |  3 +++
 forums/templates/forums/forum_detail.html    |  8 ++++----
 forums/templates/forums/forum_list.html      |  6 +++++-
 forums/views.py                              |  9 +++++----
 7 files changed, 40 insertions(+), 10 deletions(-)
 create mode 100644 forums/migrations/0002_auto_20190306_0807.py

diff --git a/forums/admin.py b/forums/admin.py
index e8871f393..d2eb2e42b 100644
--- a/forums/admin.py
+++ b/forums/admin.py
@@ -4,10 +4,12 @@ __license__ = "AGPL v3"
 
 from django.contrib import admin
 
+from guardian.admin import GuardedModelAdmin
+
 from .models import Forum, Post
 
 
-class ForumAdmin(admin.ModelAdmin):
+class ForumAdmin(GuardedModelAdmin):
     prepopulated_fields = {'slug': ('name',)}
     search_fields = ['name',]
 
diff --git a/forums/managers.py b/forums/managers.py
index 9faa94c41..2292eafef 100644
--- a/forums/managers.py
+++ b/forums/managers.py
@@ -6,6 +6,7 @@ from django.db import models
 
 
 class ForumQuerySet(models.QuerySet):
+
     def anchors(self):
         """Return only the Forums which do not have a parent."""
         return self.filter(parent_object_id__isnull=True)
diff --git a/forums/migrations/0002_auto_20190306_0807.py b/forums/migrations/0002_auto_20190306_0807.py
new file mode 100644
index 000000000..6e5c4f9b1
--- /dev/null
+++ b/forums/migrations/0002_auto_20190306_0807.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.4 on 2019-03-06 07:07
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('forums', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='forum',
+            options={'ordering': ['name'], 'permissions': [('can_view_forum', 'Can view Forum')]},
+        ),
+    ]
diff --git a/forums/models.py b/forums/models.py
index e2d05d4a5..dcd9dc75b 100644
--- a/forums/models.py
+++ b/forums/models.py
@@ -48,6 +48,9 @@ class Forum(models.Model):
 
     class Meta:
         ordering = ['name',]
+        permissions = [
+            ('can_view_forum', 'Can view Forum'),
+        ]
 
     def __str__(self):
         return self.slug
diff --git a/forums/templates/forums/forum_detail.html b/forums/templates/forums/forum_detail.html
index 0acdea979..0c9f47b07 100644
--- a/forums/templates/forums/forum_detail.html
+++ b/forums/templates/forums/forum_detail.html
@@ -15,11 +15,11 @@
 <div class="row">
   <div class="col-12">
     <h3 class="highlight">{{ forum.name }}</h3>
-
+    <p>Visible to:</p>
     {% if perms.forums.can_add_forum %}
-    <p>
-      <a href="{% url 'forums:forum_create' parent_model='forum' parent_id=forum.id %}">Create a (sub)Forum within this one</a>
-    </p>
+    <ul>
+      <li><a href="{% url 'forums:forum_create' parent_model='forum' parent_id=forum.id %}">Create a (sub)Forum within this one</a></li>
+    </ul>
     {% endif %}
 
     {% for post in forum.posts.all %}
diff --git a/forums/templates/forums/forum_list.html b/forums/templates/forums/forum_list.html
index 9abf98a2f..c3176627a 100644
--- a/forums/templates/forums/forum_list.html
+++ b/forums/templates/forums/forum_list.html
@@ -17,9 +17,11 @@
   <div class="col-12">
 
     <h3 class="highlight">Forums</h3>
+    {% if perms.forums.can_add_forum %}
     <ul>
       <li><a href="{% url 'forums:forum_create' %}">Create a new Forum</a></li>
     </ul>
+    {% endif %}
 
     <div class="card-columns">
       {% for forum in object_list %}
@@ -40,6 +42,8 @@
 	  {% endif %}
 	</div>
       </div>
+      {% empty %}
+      <p>No visible Forums found.</p>
       {% endfor %}
     </div>
   </div>
@@ -67,7 +71,7 @@
 	</tr>
 	{% empty %}
 	<tr>
-	  <td>No Forum defined</td>
+	  <td>No visible Posts found.</td>
 	</tr>
 	{% endfor %}
       </tbody>
diff --git a/forums/views.py b/forums/views.py
index d97b6df11..99402400b 100644
--- a/forums/views.py
+++ b/forums/views.py
@@ -9,6 +9,8 @@ from django.views.generic.detail import DetailView
 from django.views.generic.edit import CreateView
 from django.views.generic.list import ListView
 
+from guardian.shortcuts import get_objects_for_user
+
 from .models import Forum, Post
 from .forms import ForumForm, PostForm
 
@@ -45,10 +47,9 @@ class ForumListView(ListView):
     model = Forum
     template_name = 'forum_list.html'
 
-    def get_context_data(self, *args, **kwargs):
-        context = super().get_context_data(*args, **kwargs)
-        context['latest_'] = Forum.objects.order_by
-        return context
+    def get_queryset(self):
+        queryset = get_objects_for_user(self.request.user, 'forums.can_view_forum')
+        return queryset
 
 
 class PostCreateView(CreateView):
-- 
GitLab