SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit f67bc9b8 authored by Jean-Sébastien Caux's avatar Jean-Sébastien Caux
Browse files

Start work on permissions for forums

parent 2ac78ba5
No related branches found
No related tags found
No related merge requests found
...@@ -4,10 +4,12 @@ __license__ = "AGPL v3" ...@@ -4,10 +4,12 @@ __license__ = "AGPL v3"
from django.contrib import admin from django.contrib import admin
from guardian.admin import GuardedModelAdmin
from .models import Forum, Post from .models import Forum, Post
class ForumAdmin(admin.ModelAdmin): class ForumAdmin(GuardedModelAdmin):
prepopulated_fields = {'slug': ('name',)} prepopulated_fields = {'slug': ('name',)}
search_fields = ['name',] search_fields = ['name',]
......
...@@ -6,6 +6,7 @@ from django.db import models ...@@ -6,6 +6,7 @@ from django.db import models
class ForumQuerySet(models.QuerySet): class ForumQuerySet(models.QuerySet):
def anchors(self): def anchors(self):
"""Return only the Forums which do not have a parent.""" """Return only the Forums which do not have a parent."""
return self.filter(parent_object_id__isnull=True) return self.filter(parent_object_id__isnull=True)
# -*- 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')]},
),
]
...@@ -48,6 +48,9 @@ class Forum(models.Model): ...@@ -48,6 +48,9 @@ class Forum(models.Model):
class Meta: class Meta:
ordering = ['name',] ordering = ['name',]
permissions = [
('can_view_forum', 'Can view Forum'),
]
def __str__(self): def __str__(self):
return self.slug return self.slug
......
...@@ -15,11 +15,11 @@ ...@@ -15,11 +15,11 @@
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
<h3 class="highlight">{{ forum.name }}</h3> <h3 class="highlight">{{ forum.name }}</h3>
<p>Visible to:</p>
{% if perms.forums.can_add_forum %} {% if perms.forums.can_add_forum %}
<p> <ul>
<a href="{% url 'forums:forum_create' parent_model='forum' parent_id=forum.id %}">Create a (sub)Forum within this one</a> <li><a href="{% url 'forums:forum_create' parent_model='forum' parent_id=forum.id %}">Create a (sub)Forum within this one</a></li>
</p> </ul>
{% endif %} {% endif %}
{% for post in forum.posts.all %} {% for post in forum.posts.all %}
......
...@@ -17,9 +17,11 @@ ...@@ -17,9 +17,11 @@
<div class="col-12"> <div class="col-12">
<h3 class="highlight">Forums</h3> <h3 class="highlight">Forums</h3>
{% if perms.forums.can_add_forum %}
<ul> <ul>
<li><a href="{% url 'forums:forum_create' %}">Create a new Forum</a></li> <li><a href="{% url 'forums:forum_create' %}">Create a new Forum</a></li>
</ul> </ul>
{% endif %}
<div class="card-columns"> <div class="card-columns">
{% for forum in object_list %} {% for forum in object_list %}
...@@ -40,6 +42,8 @@ ...@@ -40,6 +42,8 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% empty %}
<p>No visible Forums found.</p>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
...@@ -67,7 +71,7 @@ ...@@ -67,7 +71,7 @@
</tr> </tr>
{% empty %} {% empty %}
<tr> <tr>
<td>No Forum defined</td> <td>No visible Posts found.</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
......
...@@ -9,6 +9,8 @@ from django.views.generic.detail import DetailView ...@@ -9,6 +9,8 @@ from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from guardian.shortcuts import get_objects_for_user
from .models import Forum, Post from .models import Forum, Post
from .forms import ForumForm, PostForm from .forms import ForumForm, PostForm
...@@ -45,10 +47,9 @@ class ForumListView(ListView): ...@@ -45,10 +47,9 @@ class ForumListView(ListView):
model = Forum model = Forum
template_name = 'forum_list.html' template_name = 'forum_list.html'
def get_context_data(self, *args, **kwargs): def get_queryset(self):
context = super().get_context_data(*args, **kwargs) queryset = get_objects_for_user(self.request.user, 'forums.can_view_forum')
context['latest_'] = Forum.objects.order_by return queryset
return context
class PostCreateView(CreateView): class PostCreateView(CreateView):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment