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

Proposed by kaputtnik
Status: Merged
Merged at revision: 430
Proposed branch: lp:~widelands-dev/widelands-website/anti_spam_4
Merge into: lp:widelands-website
Diff against target: 316 lines (+49/-95)
9 files modified
pybb/forms.py (+4/-13)
pybb/management/commands/send_hidden_post_mail.py (+22/-0)
pybb/models.py (+6/-21)
pybb/templatetags/pybb_extras.py (+1/-1)
pybb/views.py (+3/-4)
settings.py (+2/-3)
templates/pybb/forum.html (+3/-34)
templates/pybb/inlines/display_category.html (+7/-16)
templates/pybb/topic.html (+1/-3)
To merge this branch: bzr merge lp:~widelands-dev/widelands-website/anti_spam_4
Reviewer Review Type Date Requested Status
SirVer Approve
Review via email: mp+309642@code.launchpad.net

Description of the change

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.

To post a comment you must log in.
Revision history for this message
GunChleoc (gunchleoc) wrote :

Looks good to this Django agnostic.

434. By kaputtnik

fix unread posts in forums overview

Revision history for this message
SirVer (sirver) wrote :

lgtm.

review: Approve
Revision history for this message
kaputtnik (franku) wrote :

Thanks :-)

Merged, deployed and added the management command to /etc/cron.daily/django_regular_commands

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pybb/forms.py'
2--- pybb/forms.py 2016-10-26 15:28:04 +0000
3+++ pybb/forms.py 2016-11-01 14:47:32 +0000
4@@ -67,9 +67,11 @@
5 topic_is_new = False
6 topic = self.topic
7
8- # Check for spam
9+ # Check for spam and hide the post
10 # TODO: This is currently a simple keyword search. Maybe add akismet check here
11- # and move it to an own directory/app
12+ # could be improved...
13+ # The admins get informed of hidden post(s) over
14+ # a Django command. See pybb/management/commands
15 hidden = False
16 text = self.cleaned_data['body']
17 if any(x in text.lower() for x in settings.ANTI_SPAM_BODY):
18@@ -100,17 +102,6 @@
19 else:
20 send(self.topic.subscribers.all(), "forum_new_post",
21 {'post':post, 'topic':topic, 'user':post.user})
22- else:
23- # Inform admins of a hidden post
24- # Moving this to an own method makes the code clearer
25- # There is also similar code in mainpage.views.py
26- recipients = [addr[1] for addr in settings.ADMINS]
27- message = '\n'.join(['Hidden post:',
28- 'Topic name: ' + topic.name,
29- 'Post body: ' + post.body,
30- 'Admin page: http://'+ Site.objects.get_current().domain + '/admin/login/?next=/admin/pybb/post/'])
31- send_mail('A post was hidden by spam check', message, 'pybb@widelands.org',
32- recipients, fail_silently=False)
33
34 return post
35
36
37=== added file 'pybb/management/commands/send_hidden_post_mail.py'
38--- pybb/management/commands/send_hidden_post_mail.py 1970-01-01 00:00:00 +0000
39+++ pybb/management/commands/send_hidden_post_mail.py 2016-11-01 14:47:32 +0000
40@@ -0,0 +1,22 @@
41+from django.core.management.base import BaseCommand
42+from pybb.models import Post
43+from django.core.mail import send_mail
44+from django.conf import settings
45+from django.contrib.sites.models import Site
46+
47+class Command(BaseCommand):
48+ help = 'Send emails if hidden posts are found'
49+
50+ def handle(self, *args, **options):
51+ hidden_posts = Post.objects.filter(hidden=True)
52+
53+ if hidden_posts:
54+ message = 'There were %d hidden posts found:' % len(hidden_posts)
55+ for post in hidden_posts:
56+ message += '\n' + post.user.username + ': ' + post.body_text[:70]
57+
58+ message += '\n\nAdmin page: ' + Site.objects.get_current().domain + \
59+ '/admin/pybb/post/'
60+ recipients = [addr[1] for addr in settings.ADMINS]
61+ send_mail('Hidden posts were found', message, 'pybb@widelands.org',
62+ recipients, fail_silently=False)
63
64=== modified file 'pybb/models.py'
65--- pybb/models.py 2016-10-24 19:15:08 +0000
66+++ pybb/models.py 2016-11-01 14:47:32 +0000
67@@ -83,23 +83,15 @@
68
69 @property
70 def posts(self):
71- return Post.objects.filter(topic__forum=self).select_related()
72+ return Post.objects.filter(topic__forum=self).exclude(hidden=True).select_related()
73
74 @property
75 def post_count(self):
76- return Post.objects.filter(topic__forum=self).count()
77+ return Post.objects.filter(topic__forum=self).exclude(hidden=True).count()
78
79 @property
80 def last_post(self):
81- posts = self.posts.order_by('-created').select_related()
82- try:
83- return posts[0]
84- except IndexError:
85- return None
86-
87- @property
88- def last_nonhidden_post(self):
89- posts = self.posts.order_by('-created').filter(hidden=False).select_related()
90+ posts = self.posts.exclude(hidden=True).order_by('-created').select_related()
91 try:
92 return posts[0]
93 except IndexError:
94@@ -138,27 +130,20 @@
95
96 @property
97 def last_post(self):
98- return self.posts.all().order_by('-created').select_related()[0]
99-
100- @property
101- def last_nonhidden_post(self):
102- try:
103- return self.posts.all().order_by('-created').filter(hidden=False).select_related()[0]
104- except IndexError:
105- return self.posts.all().order_by('-created').select_related()[0]
106+ return self.posts.exclude(hidden=True).order_by('-created').select_related()[0]
107
108 # If the first post of this topic is hidden, the topic is hidden
109 @property
110 def is_hidden(self):
111 try:
112- p = self.posts.all().order_by('created').filter(hidden=False).select_related()[0]
113+ p = self.posts.order_by('created').filter(hidden=False).select_related()[0]
114 except IndexError:
115 return True
116 return False
117
118 @property
119 def post_count(self):
120- return Post.objects.filter(topic=self).count()
121+ return Post.objects.filter(topic=self).exclude(hidden=True).count()
122
123 def get_absolute_url(self):
124 return reverse('pybb_topic', args=[self.id])
125
126=== modified file 'pybb/templatetags/pybb_extras.py'
127--- pybb/templatetags/pybb_extras.py 2016-10-21 16:56:00 +0000
128+++ pybb/templatetags/pybb_extras.py 2016-11-01 14:47:32 +0000
129@@ -135,7 +135,7 @@
130 return not _is_topic_read(topic,user)
131 if isinstance(topic,Forum):
132 forum = topic
133- for t in forum.topics.all():
134+ for t in forum.topics.exclude(posts__hidden=True):
135 rv = _is_topic_read(t,user)
136
137 if rv == False:
138
139=== modified file 'pybb/views.py'
140--- pybb/views.py 2016-10-23 10:56:48 +0000
141+++ pybb/views.py 2016-11-01 14:47:32 +0000
142@@ -66,9 +66,9 @@
143 [:pybb_settings.QUICK_POSTS_NUMBER],
144 }
145
146- topics = forum.topics.order_by('-sticky', '-updated').select_related()
147+ topics = forum.topics.order_by('-sticky', '-updated').exclude(posts__hidden=True).select_related()
148 page, paginator = paginate(topics, request, pybb_settings.FORUM_PAGE_SIZE)
149-
150+
151 return {'forum': forum,
152 'topics': page.object_list,
153 'quick': quick,
154@@ -79,7 +79,6 @@
155
156
157 def show_topic_ctx(request, topic_id):
158-
159 try:
160 topic = Topic.objects.select_related().get(pk=topic_id)
161 except Topic.DoesNotExist:
162@@ -107,7 +106,7 @@
163 subscribed = (request.user.is_authenticated() and
164 request.user in topic.subscribers.all())
165
166- posts = topic.posts.all().select_related()
167+ posts = topic.posts.exclude(hidden=True).select_related()
168 page, paginator = paginate(posts, request, pybb_settings.TOPIC_PAGE_SIZE,
169 total_count=topic.post_count)
170
171
172=== modified file 'settings.py'
173--- settings.py 2016-07-25 19:57:43 +0000
174+++ settings.py 2016-11-01 14:47:32 +0000
175@@ -3,9 +3,8 @@
176 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
177 import os
178
179-BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+ '/widelands'
180+BASE_DIR = os.path.dirname(os.path.abspath(__file__))#+ '/widelands'
181 DEBUG = True
182-
183 ADMINS = (
184 # ('Your Name', 'your_email@domain.com'),
185 )
186@@ -76,7 +75,7 @@
187 'django.contrib.staticfiles',
188 'django.contrib.sites',
189 'django.contrib.humanize',
190- 'django_comments',
191+ #'django_comments',
192 'nocaptcha_recaptcha',
193 # Thirdparty apps, but need preload
194 'tracking', # included as wlapp
195
196=== modified file 'templates/pybb/forum.html'
197--- templates/pybb/forum.html 2016-10-08 09:30:34 +0000
198+++ templates/pybb/forum.html 2016-11-01 14:47:32 +0000
199@@ -41,7 +41,6 @@
200 </thead>
201 <tbody>
202 {% for topic in topics %}
203- {% if not topic.is_hidden %}
204 <tr class="{% cycle 'odd' 'even' %}">
205 <td class="forumIcon center">
206 {% if topic|pybb_has_unreads:user %}
207@@ -61,43 +60,13 @@
208 Views: {{ topic.views }}
209 </td>
210 <td class="lastPost">
211- {% if user.is_superuser %}
212- {% if topic.last_post %}
213- {{ topic.last_post.user|user_link }} <a href="{{ topic.last_post.get_absolute_url }}">&#187;</a><br />
214- <span class="small">on {{ topic.last_post.created|custom_date:user }}</span>
215- {% endif %}
216- {% else %}
217- {{ topic.last_nonhidden_post.user|user_link }} <a href="{{ topic.last_nonhidden_post.get_absolute_url }}">&#187;</a><br />
218- <span class="small">on {{ topic.last_nonhidden_post.created|custom_date:user }}</span>
219- {% endif %}
220- </td>
221- </tr>
222- {% elif user.is_superuser %}
223- <tr class="{% cycle 'odd' 'even' %}">
224- <td class="forumIcon center">
225- {% if topic|pybb_has_unreads:user %}
226- <img src="{{ MEDIA_URL }}forum/img/doc_big_work_star.png" style="margin: 0px;" alt="" class="middle" />
227- {% else %}
228- <img src="{{ MEDIA_URL }}forum/img/doc_big_work.png" style="margin: 0px;" alt="" class="middle" />
229- {% endif %}
230- </td>
231- <td class="forumTitle">
232- {% if topic.sticky %}<img src="{{ MEDIA_URL }}forum/img/sticky.png" alt="Sticky" title="Sticky" />{% endif %}
233- {% if topic.closed %}<img src="{{ MEDIA_URL }}forum/img/closed.png" alt="Closed" title="Closed" />{% endif %}
234- <a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a><br />
235- <span class="small">Created by {{ topic.user|user_link }} on {{ topic.created|custom_date:user }}</span>
236- </td>
237- <td class="forumCount center small" style="width: 120px;">
238- Posts: {{ topic.post_count }}<br/>
239- Views: {{ topic.views }}
240- </td>
241- <td class="lastPost">
242+ {%if topic.last_post %}
243 {{ topic.last_post.user|user_link }} <a href="{{ topic.last_post.get_absolute_url }}">&#187;</a><br />
244 <span class="small">on {{ topic.last_post.created|custom_date:user }}</span>
245+ {% endif %}
246 </td>
247 </tr>
248- {% endif %} {# topic.is_hidden #}
249- {% endfor %} {# topic #}
250+ {% endfor %}
251 </tbody>
252 </table>
253
254
255=== modified file 'templates/pybb/inlines/display_category.html'
256--- templates/pybb/inlines/display_category.html 2016-10-08 09:30:34 +0000
257+++ templates/pybb/inlines/display_category.html 2016-11-01 14:47:32 +0000
258@@ -29,22 +29,13 @@
259 Topics: {{ forum.topics.count }}<br/>
260 Posts: {{ forum.posts.count }}
261 </td>
262- {% if user.is_superuser %} {# Show all to superuser #}
263- {% if forum.last_post %}
264- <td class="lastPost">
265- <a href="{{forum.last_post.get_absolute_url}}">{{ forum.last_post.topic.name }}</a><br />
266- <span class="small">by {{ forum.last_post.user|user_link }}<br />
267- on {{ forum.last_post.created|custom_date:user}}</span>
268- </td>
269- {% endif %}
270- {% else %} {# no super_user: Show only nonhidden posts#}
271- {% if forum.last_nonhidden_post %}
272- <td class="lastPost">
273- <a href="{{forum.last_nonhidden_post.get_absolute_url}}">{{ forum.last_nonhidden_post.topic.name }}</a><br />
274- <span class="small">by {{ forum.last_nonhidden_post.user|user_link }}<br />
275- on {{ forum.last_nonhidden_post.created|custom_date:user}}</span>
276- </td>
277- {% endif %}
278+ <td class="lastPost">
279+ {%if forum.last_post %}
280+ <a href="{{forum.last_post.get_absolute_url}}">{{ forum.last_post.topic.name }}</a><br />
281+ <span class="small">by {{ forum.last_post.user|user_link }}<br />
282+ on {{ forum.last_post.created|custom_date:user}}</span>
283+ {% else %}
284+ &nbsp;
285 {% endif %}
286 </td>
287 </tr>
288
289=== modified file 'templates/pybb/topic.html'
290--- templates/pybb/topic.html 2016-10-08 09:30:34 +0000
291+++ templates/pybb/topic.html 2016-11-01 14:47:32 +0000
292@@ -17,7 +17,7 @@
293 {% endblock %}
294
295 {% block content %}
296-<h1>Topic: {{ topic }}</h1>
297+<h1>Topic: {{ topic }} </h1>
298 <div class="blogEntry">
299 <a href="{% url 'pybb_index' %}">Forums</a> &#187;
300 {% pybb_link topic.forum.category %} &#187;
301@@ -157,7 +157,6 @@
302 <table class="forum">
303 <tbody>
304 {% for post in posts %}
305- {% if not post.hidden or user.is_superuser %}
306 <tr class="{% cycle 'odd' 'even' %}">
307 <td class="author">
308 {{ post.user|user_link }}<br />
309@@ -229,7 +228,6 @@
310 {% endif %}
311 </td>
312 </tr>
313- {% endif %}
314 <tr class="spacer">
315 <td></td>
316 <td></td>

Subscribers

People subscribed via source and target branches