[extensions-web/wip/disallow-comments: 1/2] ratings: allow to disable comments for particular extension




commit d60967d65bf0b508be4466201381340bf0248dbb
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Tue Aug 9 12:37:46 2022 +0400

    ratings: allow to disable comments for particular extension
    
    See-Also: https://gitlab.gnome.org/Infrastructure/extensions-web/-/issues/197

 .../migrations/0011_extension_allow_comments.py        | 18 ++++++++++++++++++
 sweettooth/extensions/models.py                        |  1 +
 sweettooth/extensions/templates/extensions/detail.html |  4 ++++
 .../extensions/templates/extensions/detail_edit.html   |  5 ++++-
 sweettooth/ratings/__init__.py                         | 11 +++++++++++
 sweettooth/ratings/moderation.py                       |  6 ++++++
 sweettooth/ratings/views.py                            |  5 +++++
 7 files changed, 49 insertions(+), 1 deletion(-)
---
diff --git a/sweettooth/extensions/migrations/0011_extension_allow_comments.py 
b/sweettooth/extensions/migrations/0011_extension_allow_comments.py
new file mode 100644
index 0000000..083cbb8
--- /dev/null
+++ b/sweettooth/extensions/migrations/0011_extension_allow_comments.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.2.14 on 2022-08-09 10:15
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extensions', '0010_session_modes'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='extension',
+            name='allow_comments',
+            field=models.BooleanField(default=True),
+        ),
+    ]
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index aaca32a..760eb1f 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -128,6 +128,7 @@ class Extension(models.Model):
     created = models.DateTimeField(auto_now_add=True)
     downloads = models.PositiveIntegerField(default=0)
     popularity = models.IntegerField(default=0)
+    allow_comments = models.BooleanField(default=True)
 
     class Meta:
         permissions = (
diff --git a/sweettooth/extensions/templates/extensions/detail.html 
b/sweettooth/extensions/templates/extensions/detail.html
index b0eb3f2..88f0d15 100644
--- a/sweettooth/extensions/templates/extensions/detail.html
+++ b/sweettooth/extensions/templates/extensions/detail.html
@@ -88,7 +88,11 @@
       <hr style="clear: both;">
 
       {% block comments %}
+      {% if not extension.allow_comments %}
+        <p style="text-align: center;">{% trans 'Comments are disabled for this extension.' %}</p>
+      {% else %}
       {% include "extensions/comments.html" %}
+      {% endif %}
       {% endblock comments %}
 
       {% block extra %}
diff --git a/sweettooth/extensions/templates/extensions/detail_edit.html 
b/sweettooth/extensions/templates/extensions/detail_edit.html
index 6fbc3a8..de99e66 100644
--- a/sweettooth/extensions/templates/extensions/detail_edit.html
+++ b/sweettooth/extensions/templates/extensions/detail_edit.html
@@ -1,8 +1,11 @@
 {% extends "extensions/detail.html" %}
+{% load i18n %}
 {% load extension_icon %}
 
 {% block comments %}
-  {% if is_visible %}
+  {% if not extension.allow_comments %}
+    <p style="text-align: center;">{% trans 'Comments are disabled for this extension.' %}</p>
+  {% elif is_visible %}
     {% include "extensions/comments.html" %}
   {% else %}
     <p style="text-align: center;">Comments will appear here after your extension is approved.</p>
diff --git a/sweettooth/ratings/__init__.py b/sweettooth/ratings/__init__.py
index 70c469a..8de33b5 100644
--- a/sweettooth/ratings/__init__.py
+++ b/sweettooth/ratings/__init__.py
@@ -1,3 +1,5 @@
+from django.apps import AppConfig
+
 def get_model():
     from sweettooth.ratings.models import RatingComment
     return RatingComment
@@ -6,3 +8,12 @@ def get_model():
 def get_form():
     from sweettooth.ratings.forms import RatingCommentForm
     return RatingCommentForm
+
+
+class RatingsConfig(AppConfig):
+    def ready(self):
+        from django_comments.moderation import moderator
+        from sweettooth.extensions.models import Extension
+        from .moderation import ExtensionCommentsModerator
+
+        moderator.register(Extension, ExtensionCommentsModerator)
diff --git a/sweettooth/ratings/moderation.py b/sweettooth/ratings/moderation.py
new file mode 100644
index 0000000..0bc5cd7
--- /dev/null
+++ b/sweettooth/ratings/moderation.py
@@ -0,0 +1,6 @@
+from django_comments.moderation import CommentModerator
+
+
+class ExtensionCommentsModerator(CommentModerator):
+    email_notification = False
+    enable_field = 'allow_comments'
diff --git a/sweettooth/ratings/views.py b/sweettooth/ratings/views.py
index ec45e03..75a9081 100644
--- a/sweettooth/ratings/views.py
+++ b/sweettooth/ratings/views.py
@@ -3,6 +3,7 @@ import json
 
 import django_comments as comments
 from django.contrib.messages import info
+from django.http import HttpResponseForbidden
 from django.shortcuts import redirect
 from django.template.defaultfilters import linebreaks
 from django.urls import reverse
@@ -40,6 +41,10 @@ def comment_details(request, comment):
 @ajax_view
 def get_comments(request):
     extension = models.Extension.objects.get(pk=request.GET['pk'])
+
+    if not extension.allow_comments:
+        return HttpResponseForbidden()
+
     show_all = json.loads(request.GET.get('all', 'false'))
 
     comment_list = comments.get_model().objects.for_model(extension)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]