← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands-website/anti_spam_4 into lp:widelands-website

 

kaputtnik has proposed merging lp:~widelands-dev/widelands-website/anti_spam_4 into lp:widelands-website.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1614403 in Widelands Website: "Ideas to prevent spammers, make their work harder"
  https://bugs.launchpad.net/widelands-website/+bug/1614403

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands-website/anti_spam_4/+merge/309642

Instead preventing the templates from showing hidden posts, this is now managed through the database query's in the models and views. So each pagination uses only non hidden data and the templates are as they were before (i copied the old templates over the changed ones).

Added a management command for sending emails, called 'send_hidden_post_mail'. The email is only send if hidden posts were found and contains:
1. The amount of hidden posts
2. A list in form of 'username: ' 'first 70 chars of post'
3. A link to admin/pybb/post

Changes in settings.py:
- BASE_DIR: Preventing the static string 'widelands'. This makes working with branches much easier and shouldn't be a problem on the server as long the path over there ends in  'widelands'. Otherwise wlwebsite_wsgi.py needs also some changes.
- django_comments is useless in INSTALLED_APPS, because threadedcomments is used for commenting. I just commented the line... there is a plan for cleaning settings.py up (there ar some more things commented out)

Maybe the changes in settings.py have to be in another branch. These are here just because i changed them during the work in this branch.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/anti_spam_4 into lp:widelands-website.
=== modified file 'pybb/forms.py'
--- pybb/forms.py	2016-10-26 15:28:04 +0000
+++ pybb/forms.py	2016-10-30 19:21:47 +0000
@@ -67,9 +67,11 @@
             topic_is_new = False
             topic = self.topic
 
-        # Check for spam
+        # Check for spam and hide the post
         # TODO: This is currently a simple keyword search. Maybe add akismet check here
-        # and move it to an own directory/app
+        # could be improved...
+        # The admins get informed of hidden post(s) over
+        # a Django command. See pybb/management/commands
         hidden = False
         text = self.cleaned_data['body']
         if any(x in text.lower() for x in settings.ANTI_SPAM_BODY):
@@ -100,17 +102,6 @@
             else:
                 send(self.topic.subscribers.all(), "forum_new_post",
                     {'post':post, 'topic':topic, 'user':post.user})
-        else:
-            # Inform admins of a hidden post
-            # Moving this to an own method makes the code clearer
-            # There is also similar code in mainpage.views.py
-            recipients = [addr[1] for addr in settings.ADMINS]
-            message = '\n'.join(['Hidden post:',
-                                 'Topic name: ' + topic.name,
-                                 'Post body: ' + post.body,
-                                 'Admin page: http://'+ Site.objects.get_current().domain + '/admin/login/?next=/admin/pybb/post/'])
-            send_mail('A post was hidden by spam check', message, 'pybb@xxxxxxxxxxxxx',
-                      recipients, fail_silently=False)
 
         return post
 

=== added file 'pybb/management/commands/send_hidden_post_mail.py'
--- pybb/management/commands/send_hidden_post_mail.py	1970-01-01 00:00:00 +0000
+++ pybb/management/commands/send_hidden_post_mail.py	2016-10-30 19:21:47 +0000
@@ -0,0 +1,22 @@
+from django.core.management.base import BaseCommand
+from pybb.models import Post
+from django.core.mail import send_mail
+from django.conf import settings
+from django.contrib.sites.models import Site
+
+class Command(BaseCommand):
+    help = 'Send emails if hidden posts are found'
+
+    def handle(self, *args, **options):
+        hidden_posts = Post.objects.filter(hidden=True)
+
+        if hidden_posts:
+            message = 'There were %d hidden posts found:' % len(hidden_posts)
+            for post in hidden_posts:
+                message += '\n' + post.user.username + ': ' + post.body_text[:70]
+
+            message += '\n\nAdmin page: ' + Site.objects.get_current().domain + \
+                '/admin/pybb/post/'
+            recipients = [addr[1] for addr in settings.ADMINS]
+            send_mail('Hidden posts were found', message, 'pybb@xxxxxxxxxxxxx',
+                      recipients, fail_silently=False)

=== modified file 'pybb/models.py'
--- pybb/models.py	2016-10-24 19:15:08 +0000
+++ pybb/models.py	2016-10-30 19:21:47 +0000
@@ -83,23 +83,15 @@
 
     @property
     def posts(self):
-        return Post.objects.filter(topic__forum=self).select_related()
+        return Post.objects.filter(topic__forum=self).exclude(hidden=True).select_related()
 
     @property
     def post_count(self):
-        return Post.objects.filter(topic__forum=self).count()
+        return Post.objects.filter(topic__forum=self).exclude(hidden=True).count()
 
     @property
     def last_post(self):
-        posts = self.posts.order_by('-created').select_related()
-        try:
-            return posts[0]
-        except IndexError:
-            return None
-
-    @property
-    def last_nonhidden_post(self):
-        posts = self.posts.order_by('-created').filter(hidden=False).select_related()
+        posts = self.posts.exclude(hidden=True).order_by('-created').select_related()
         try:
             return posts[0]
         except IndexError:
@@ -138,27 +130,20 @@
 
     @property
     def last_post(self):
-        return self.posts.all().order_by('-created').select_related()[0]
-
-    @property
-    def last_nonhidden_post(self):
-        try:
-            return self.posts.all().order_by('-created').filter(hidden=False).select_related()[0]
-        except IndexError:
-            return self.posts.all().order_by('-created').select_related()[0]
+        return self.posts.exclude(hidden=True).order_by('-created').select_related()[0]
 
     # If the first post of this topic is hidden, the topic is hidden
     @property
     def is_hidden(self):
         try:
-            p = self.posts.all().order_by('created').filter(hidden=False).select_related()[0]
+            p = self.posts.order_by('created').filter(hidden=False).select_related()[0]
         except IndexError:
             return True
         return False
 
     @property
     def post_count(self):
-        return Post.objects.filter(topic=self).count()
+        return Post.objects.filter(topic=self).exclude(hidden=True).count()
 
     def get_absolute_url(self):
         return reverse('pybb_topic', args=[self.id])

=== modified file 'pybb/views.py'
--- pybb/views.py	2016-10-23 10:56:48 +0000
+++ pybb/views.py	2016-10-30 19:21:47 +0000
@@ -66,9 +66,9 @@
                 [:pybb_settings.QUICK_POSTS_NUMBER],
              }
 
-    topics = forum.topics.order_by('-sticky', '-updated').select_related()
+    topics = forum.topics.order_by('-sticky', '-updated').exclude(posts__hidden=True).select_related()
     page, paginator = paginate(topics, request, pybb_settings.FORUM_PAGE_SIZE)
-
+    
     return {'forum': forum,
             'topics': page.object_list,
             'quick': quick,
@@ -79,7 +79,6 @@
 
 
 def show_topic_ctx(request, topic_id):
-
     try:
         topic = Topic.objects.select_related().get(pk=topic_id)
     except Topic.DoesNotExist:
@@ -107,7 +106,7 @@
     subscribed = (request.user.is_authenticated() and
                   request.user in topic.subscribers.all())
 
-    posts = topic.posts.all().select_related()
+    posts = topic.posts.exclude(hidden=True).select_related()
     page, paginator = paginate(posts, request, pybb_settings.TOPIC_PAGE_SIZE,
                                total_count=topic.post_count)
 

=== modified file 'settings.py'
--- settings.py	2016-07-25 19:57:43 +0000
+++ settings.py	2016-10-30 19:21:47 +0000
@@ -3,9 +3,8 @@
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 import os
 
-BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+ '/widelands'
+BASE_DIR = os.path.dirname(os.path.abspath(__file__))#+ '/widelands'
 DEBUG = True
-
 ADMINS = (
     # ('Your Name', 'your_email@xxxxxxxxxx'),
 )
@@ -76,7 +75,7 @@
     'django.contrib.staticfiles',
     'django.contrib.sites',
     'django.contrib.humanize',
-    'django_comments',
+    #'django_comments',
     'nocaptcha_recaptcha',
     # Thirdparty apps, but need preload
     'tracking', # included as wlapp

=== modified file 'templates/pybb/forum.html'
--- templates/pybb/forum.html	2016-10-08 09:30:34 +0000
+++ templates/pybb/forum.html	2016-10-30 19:21:47 +0000
@@ -41,7 +41,6 @@
 		</thead>
 		<tbody>
 		{% for topic in topics %}
-		{% if not topic.is_hidden %}
 		<tr class="{% cycle 'odd' 'even' %}">
 			<td class="forumIcon center">
 				{% if topic|pybb_has_unreads:user %}
@@ -61,43 +60,13 @@
 				Views: {{ topic.views }}
 			</td>
 			<td class="lastPost">
-				{% if user.is_superuser %}
-					{% if topic.last_post %}
-						{{ topic.last_post.user|user_link }} <a href="{{ topic.last_post.get_absolute_url }}">&#187;</a><br />
-						<span class="small">on {{ topic.last_post.created|custom_date:user }}</span>
-					{% endif %}
-				{% else %}
-					{{ topic.last_nonhidden_post.user|user_link }} <a href="{{ topic.last_nonhidden_post.get_absolute_url }}">&#187;</a><br />
-					<span class="small">on {{ topic.last_nonhidden_post.created|custom_date:user }}</span>
-				{% endif %}
-			</td>
-		</tr>
-		{% elif user.is_superuser %}
-		<tr class="{% cycle 'odd' 'even' %}">
-			<td class="forumIcon center">
-				{% if topic|pybb_has_unreads:user %}
-				<img src="{{ MEDIA_URL }}forum/img/doc_big_work_star.png" style="margin: 0px;" alt="" class="middle" />
-				{% else %}
-					<img src="{{ MEDIA_URL }}forum/img/doc_big_work.png" style="margin: 0px;" alt="" class="middle" />
-				{% endif %}
-			</td>
-			<td class="forumTitle">
-				{% if topic.sticky %}<img src="{{ MEDIA_URL }}forum/img/sticky.png" alt="Sticky" title="Sticky" />{% endif %}
-				{% if topic.closed %}<img src="{{ MEDIA_URL }}forum/img/closed.png" alt="Closed" title="Closed" />{% endif %}
-				<a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a><br />
-				<span class="small">Created by {{ topic.user|user_link }} on {{ topic.created|custom_date:user }}</span>
-			</td>
-			<td class="forumCount center small" style="width: 120px;">
-				Posts: {{ topic.post_count }}<br/>
-				Views: {{ topic.views }}
-			</td>
-			<td class="lastPost">
+				{%if topic.last_post %}
 				{{ topic.last_post.user|user_link }} <a href="{{ topic.last_post.get_absolute_url }}">&#187;</a><br />
 				<span class="small">on {{ topic.last_post.created|custom_date:user }}</span>
+				{% endif %}
 			</td>
 		</tr>
-		{% endif %} {# topic.is_hidden #}
-		{% endfor %} {# topic #}
+		{% endfor %}
 		</tbody>
 	</table>
 

=== modified file 'templates/pybb/inlines/display_category.html'
--- templates/pybb/inlines/display_category.html	2016-10-08 09:30:34 +0000
+++ templates/pybb/inlines/display_category.html	2016-10-30 19:21:47 +0000
@@ -29,22 +29,13 @@
 			Topics: {{ forum.topics.count }}<br/>
 			Posts: {{ forum.posts.count }}
 		</td>
-		{% if user.is_superuser %} {# Show all to superuser #}
-			{% if forum.last_post %}
-			<td class="lastPost">
-				<a href="{{forum.last_post.get_absolute_url}}">{{ forum.last_post.topic.name }}</a><br />
-				<span class="small">by {{ forum.last_post.user|user_link }}<br />
-				on {{ forum.last_post.created|custom_date:user}}</span>
-			</td>
-			{% endif %}
-		{% else %} {# no super_user: Show only nonhidden posts#}
-			{% if forum.last_nonhidden_post %}
-			<td class="lastPost">
-				<a href="{{forum.last_nonhidden_post.get_absolute_url}}">{{ forum.last_nonhidden_post.topic.name }}</a><br />
-				<span class="small">by {{ forum.last_nonhidden_post.user|user_link }}<br />
-				on {{ forum.last_nonhidden_post.created|custom_date:user}}</span>
-			</td>
-			{% endif %}
+		<td class="lastPost">
+		{%if forum.last_post %}
+			<a href="{{forum.last_post.get_absolute_url}}">{{ forum.last_post.topic.name }}</a><br />
+			<span class="small">by {{ forum.last_post.user|user_link }}<br />
+			on {{ forum.last_post.created|custom_date:user}}</span>
+		{% else %}
+			&nbsp;
 		{% endif %}
 		</td>
 	</tr>

=== modified file 'templates/pybb/topic.html'
--- templates/pybb/topic.html	2016-10-08 09:30:34 +0000
+++ templates/pybb/topic.html	2016-10-30 19:21:47 +0000
@@ -17,7 +17,7 @@
 {% endblock %}
 
 {% block content %}
-<h1>Topic: {{ topic }}</h1>
+<h1>Topic: {{ topic }} </h1>
 <div class="blogEntry">
 	<a href="{% url 'pybb_index' %}">Forums</a> &#187; 
 	{% pybb_link topic.forum.category %} &#187; 
@@ -157,7 +157,6 @@
 	<table class="forum">
 		<tbody>
 	{% for post in posts %}
-		{% if not post.hidden or user.is_superuser %}
 		<tr class="{% cycle 'odd' 'even' %}">
 			<td class="author">
 				{{ post.user|user_link }}<br />
@@ -229,7 +228,6 @@
 				{% endif %}
 			</td>
 		</tr>
-		{% endif %}
 		<tr class="spacer">
 			<td></td>
 			<td></td>


Follow ups