From 7d17b7e60edc3ec3b6243c8bf30bf8870ccde4a7 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sat, 9 Mar 2019 06:36:06 +0100
Subject: [PATCH] Add description field and update method for Forum

---
 forums/forms.py                               |  3 ++-
 forums/migrations/0005_forum_description.py   | 20 +++++++++++++++++++
 forums/models.py                              | 14 ++++++++++++-
 .../forums/forum_confirm_delete.html          | 13 +++++++++++-
 forums/templates/forums/forum_detail.html     | 11 ++++++++--
 forums/templates/forums/forum_list.html       |  3 ++-
 forums/urls.py                                |  5 +++++
 forums/views.py                               |  7 +++++++
 scipost/templatetags/restructuredtext.py      |  2 ++
 9 files changed, 72 insertions(+), 6 deletions(-)
 create mode 100644 forums/migrations/0005_forum_description.py

diff --git a/forums/forms.py b/forums/forms.py
index ce91c1096..76c774e4e 100644
--- a/forums/forms.py
+++ b/forums/forms.py
@@ -12,7 +12,8 @@ from .models import Forum, Post
 class ForumForm(forms.ModelForm):
     class Meta:
         model = Forum
-        fields = ['name', 'slug', 'publicly_visible', 'moderators',
+        fields = ['name', 'slug', 'description',
+                  'publicly_visible', 'moderators',
                   'parent_content_type', 'parent_object_id']
 
     def __init__(self, *args, **kwargs):
diff --git a/forums/migrations/0005_forum_description.py b/forums/migrations/0005_forum_description.py
new file mode 100644
index 000000000..609fd9d50
--- /dev/null
+++ b/forums/migrations/0005_forum_description.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.4 on 2019-03-09 05:18
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('forums', '0004_auto_20190308_1055'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='forum',
+            name='description',
+            field=models.TextField(blank=True, help_text='You can use ReStructuredText, see a <a href="https://devguide.python.org/documenting/#restructuredtext-primer" target="_blank">primer on python.org</a>', null=True),
+        ),
+    ]
diff --git a/forums/models.py b/forums/models.py
index b80d55574..ae15f3a8e 100644
--- a/forums/models.py
+++ b/forums/models.py
@@ -27,6 +27,13 @@ class Forum(models.Model):
     """
     name = models.CharField(max_length=256)
     slug = models.SlugField(allow_unicode=True)
+    description = models.TextField(
+        blank=True, null=True,
+        help_text=(
+            'You can use ReStructuredText, see a '
+            '<a href="https://devguide.python.org/documenting/#restructuredtext-primer" '
+            'target="_blank">primer on python.org</a>')
+        )
     publicly_visible = models.BooleanField(default=False)
     moderators = models.ManyToManyField('auth.User', related_name='moderated_forums')
 
@@ -117,7 +124,12 @@ class Post(models.Model):
                                      object_id_field='parent_object_id',
                                      related_query_name='parent_posts')
     subject = models.CharField(max_length=256)
-    text = models.TextField(help_text='You can use ReStructuredText, see a <a href="https://devguide.python.org/documenting/#restructuredtext-primer" target="_blank">primer on python.org</a>')
+    text = models.TextField(
+        help_text=(
+            'You can use ReStructuredText, see a '
+            '<a href="https://devguide.python.org/documenting/#restructuredtext-primer" '
+            'target="_blank">primer on python.org</a>')
+        )
 
     class Meta:
         ordering = ['posted_on',]
diff --git a/forums/templates/forums/forum_confirm_delete.html b/forums/templates/forums/forum_confirm_delete.html
index 6727f68ef..e1b83558d 100644
--- a/forums/templates/forums/forum_confirm_delete.html
+++ b/forums/templates/forums/forum_confirm_delete.html
@@ -1,6 +1,7 @@
 {% extends 'forums/base.html' %}
 
 {% load bootstrap %}
+{% load restructuredtext %}
 
 {% block pagetitle %}: Delete Forum{% endblock pagetitle %}
 
@@ -8,11 +9,21 @@
 <div class="row">
     <div class="col-12">
         <h1 class="highlight">Delete Forum</h1>
-	{{ object }}
+
+	<h3 class="highlight">Description</h3>
+	{{ object.description|restructuredtext }}
+
+	<h3 class="highlight">Posts</h3>
+	{% for post in object.posts.all %}
+	{% include 'forums/post_card.html' with forum=object post=post %}
+	{% endfor %}
+
     </div>
 </div>
+
 <div class="row">
   <div class="col-12">
+
     <form method="post">
       {% csrf_token %}
       <h3 class="mb-2">Are you sure you want to delete this Forum (and all associated Posts)?</h3>
diff --git a/forums/templates/forums/forum_detail.html b/forums/templates/forums/forum_detail.html
index 425c78890..5820665ae 100644
--- a/forums/templates/forums/forum_detail.html
+++ b/forums/templates/forums/forum_detail.html
@@ -2,6 +2,7 @@
 
 {% load bootstrap %}
 {% load guardian_tags %}
+{% load restructuredtext %}
 
 {% block breadcrumb_items %}
     {{ block.super }}
@@ -32,14 +33,15 @@
     {% if perms.forums.add_forum or "can_change_forum" in user_perms %}
     <h4>Admin actions:</h4>
     <ul>
-      <li><a href="{% url 'forums:forum_create' parent_model='forum' parent_id=forum.id %}">Create a (sub)Forum within this one</a></li>
+      <li><a href="{% url 'forums:forum_update' slug=forum.slug %}" class="text-warning">Update this Forum</a></li>
       <li>
-	{% if not forum.child_forums %}
+	{% if not forum.child_forums.all|length > 0 %}
 	<a href="{% url 'forums:forum_delete' slug=forum.slug %}" class="text-danger">Delete this Forum (and all Posts it contains)</a>
 	{% else %}
 	<span class="text-danger" style="text-decoration: line-through;">Delete this Forum</span> Please delete descendant Forums first.
 	{% endif %}
       </li>
+      <li><a href="{% url 'forums:forum_create' parent_model='forum' parent_id=forum.id %}">Create a (sub)Forum within this one</a></li>
     </ul>
 
     <div class="card">
@@ -72,6 +74,11 @@
 
     {% endif %}
 
+    <h3 class="highlight">Description</h3>
+    {{ forum.description|restructuredtext }}
+
+    <h3 class="highlight">Posts</h3>
+
     {% for post in forum.posts.all %}
     {% include 'forums/post_card.html' with forum=forum post=post %}
     {% endfor %}
diff --git a/forums/templates/forums/forum_list.html b/forums/templates/forums/forum_list.html
index d5f0b6948..b4a815c4b 100644
--- a/forums/templates/forums/forum_list.html
+++ b/forums/templates/forums/forum_list.html
@@ -1,6 +1,7 @@
 {% extends 'forums/base.html' %}
 
 {% load bootstrap %}
+{% load restructuredtext %}
 
 
 {% block breadcrumb_items %}
@@ -31,7 +32,7 @@
 	  <span class="badge badge-primary badge-pill">{{ forum.nr_posts }} post{{ forum.nr_posts|pluralize }}</span>
 	</div>
 	<div class="card-body">
-	  Forum description
+	  {{ forum.description|restructuredtext }}
 	  {% if forum.child_forums.all|length > 0 %}
 	  <hr/>
 	  <p>Descendants:</p>
diff --git a/forums/urls.py b/forums/urls.py
index f2577a96f..989b3229c 100644
--- a/forums/urls.py
+++ b/forums/urls.py
@@ -22,6 +22,11 @@ urlpatterns = [
         views.ForumDetailView.as_view(),
         name='forum_detail'
     ),
+    url(
+        r'^(?P<slug>[\w-]+)/update/$',
+        views.ForumUpdateView.as_view(),
+        name='forum_update'
+    ),
     url(
         r'^(?P<slug>[\w-]+)/delete/$',
         views.ForumDeleteView.as_view(),
diff --git a/forums/views.py b/forums/views.py
index 41429eb72..5716fbd20 100644
--- a/forums/views.py
+++ b/forums/views.py
@@ -49,6 +49,13 @@ class ForumCreateView(PermissionsMixin, CreateView):
         return initial
 
 
+class ForumUpdateView(PermissionRequiredMixin, UpdateView):
+    permission_required = 'forums.update_forum'
+    model = Forum
+    form_class = ForumForm
+    template_name = 'forums/forum_form.html'
+
+
 class ForumDeleteView(PermissionRequiredMixin, DeleteView):
     permission_required = 'forums.delete_forum'
     model = Forum
diff --git a/scipost/templatetags/restructuredtext.py b/scipost/templatetags/restructuredtext.py
index 535184b64..a13399059 100644
--- a/scipost/templatetags/restructuredtext.py
+++ b/scipost/templatetags/restructuredtext.py
@@ -12,6 +12,8 @@ register = template.Library()
 
 @register.filter(name='restructuredtext')
 def restructuredtext(text):
+    if not text:
+        return ''
     from docutils.core import publish_parts
     parts = publish_parts(source=text,
                           writer_name='html5_polyglot')
-- 
GitLab