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

Proposed by kaputtnik
Status: Merged
Merged at revision: 528
Proposed branch: lp:~widelands-dev/widelands-website/more_latest_posts
Merge into: lp:widelands-website
Diff against target: 337 lines (+221/-10)
9 files modified
mainpage/templates/mainpage/header.html (+1/-1)
pybb/forms.py (+12/-0)
pybb/settings.py (+1/-0)
pybb/templates/pybb/all_last_posts.html (+73/-0)
pybb/templates/pybb/inlines/latest_posts_table.html (+21/-0)
pybb/templates/pybb/last_posts.html (+3/-0)
pybb/templatetags/pybb_extras.py (+11/-8)
pybb/urls.py (+1/-0)
pybb/views.py (+98/-1)
To merge this branch: bzr merge lp:~widelands-dev/widelands-website/more_latest_posts
Reviewer Review Type Date Requested Status
GunChleoc Approve
kaputtnik (community) Needs Resubmitting
Review via email: mp+364706@code.launchpad.net

Commit message

Add a 'more latest posts view'

Description of the change

Add a view to show more 'latest posts'

- Add a link to the bottom of the 'Latest Posts' box
- Add a view showing the latest posts
- Add a form to adjust the days and the sort option: by topic/forum
- Increased number for the 'Last Post' box to have more last posts in it if too many posts are created in one topic.
- fixed url parsing after login: A url with a query string like '.../?page=3#post-27347' does not work after login. Now it works.

I have merged it on alpha for testing: http://alpha.widelands.org/forum/latest_posts/?days=365&sort_by=topic

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

Added some nits for strings and comments.

The grouping doesn't work properly. In

http://alpha.widelands.org/forum/latest_posts/?days=400&sort_by=forum

I see the "Germany" forum twice and the topic "Na dann gucken wir mal wie das hier ist" 3 times.

In http://alpha.widelands.org/forum/latest_posts/?days=400&sort_by=topic, I see the same topic listed multiple times as well.

Revision history for this message
kaputtnik (franku) wrote :

Thanks. I have replied to one of your inline comments below.

About grouping: I agree that this can be confusing. Currently the list is always sorted by the posts dates, so grouping lead into this situation were some topics appear double. Say we have:

post1 created 12.3.2019 in topic foo
post2 created 11.3.2019 in topic bar
post3 created 10.3.2019 in topic baz
post4 created 1.1.2019 in topic foo

Will lead to:

topic foo:
 post1 (12.3.2019)

topic bar:
 post2 (11.3.1019)

topic baz:
 post3 (10.3.2019)

topic foo:
 post4 (1.1.2019)

If we change this to something like:

topic foo:
 post1 (12.3.2019)
 post4 (1.1.2019)

topic bar:
 post2 (11.3.1019)

topic baz:
 post3 (10.3.2019)

The posts are not sorted by date anymore: A post created yesterday may appear at the bottom of the page then, after several other old posts.

I am unsure about a good grouping. Maybe this should be tested in production to have real time data? Or adding a third option 'no grouping' which shows the posts by date descending and change the grouping by topic/forum to have no double entries?

Revision history for this message
GunChleoc (gunchleoc) wrote :

The change you mention is exactly what I would expect. Sorted by topics' last post date rather than each individual post's date.

The only reason we need this long list really is that we can't link to the first unread post.

539. By kaputtnik

stringfixes

Revision history for this message
kaputtnik (franku) wrote :

Done with stringfixes, also applied the change to the label of days as you suggested.

Will apply the needed changes to have the ordering/grouping you mentioned. This may take some time so i shut down alpha again.

540. By kaputtnik

rework of grouping algorithm

541. By kaputtnik

cleanups

Revision history for this message
kaputtnik (franku) wrote :
review: Needs Resubmitting
Revision history for this message
GunChleoc (gunchleoc) wrote :

Perfect, let's have it :)

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

Thanks :-)

Merged and deployed.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'mainpage/templates/mainpage/header.html'
--- mainpage/templates/mainpage/header.html 2019-03-03 15:17:58 +0000
+++ mainpage/templates/mainpage/header.html 2019-03-20 21:32:45 +0000
@@ -42,7 +42,7 @@
42 <br />42 <br />
43 <a href="{% url 'logout' %}?next=/">Logout</a>43 <a href="{% url 'logout' %}?next=/">Logout</a>
44 {% else %}44 {% else %}
45 <a href="{% url 'login' %}?next={{ request.path|iriencode }}">Login/Register</a>45 <a href="{% url 'login' %}?next={{ request.get_full_path|urlencode }}">Login/Register</a>
46 {% endif %}46 {% endif %}
47 </li>47 </li>
48</ul>48</ul>
4949
=== modified file 'pybb/forms.py'
--- pybb/forms.py 2018-12-21 11:26:08 +0000
+++ pybb/forms.py 2019-03-20 21:32:45 +0000
@@ -87,3 +87,15 @@
87 post.updated = datetime.now()87 post.updated = datetime.now()
88 post.save(*args, **kwargs)88 post.save(*args, **kwargs)
89 return post89 return post
90
91
92class LastPostsDayForm(forms.Form):
93 days = forms.IntegerField(
94 max_value = 1000,
95 min_value = 5,
96 )
97
98 sort_by = forms.ChoiceField(
99 choices = [('forum','Forum'),('topic', 'Topic'),],
100 label = 'Group by:',
101 )
90102
=== modified file 'pybb/settings.py'
--- pybb/settings.py 2018-12-21 12:50:32 +0000
+++ pybb/settings.py 2019-03-20 21:32:45 +0000
@@ -18,6 +18,7 @@
18ATTACHMENT_SIZE_LIMIT = get('PYBB_ATTACHMENT_SIZE_LIMIT', 1024 * 1024)18ATTACHMENT_SIZE_LIMIT = get('PYBB_ATTACHMENT_SIZE_LIMIT', 1024 * 1024)
19ATTACHMENT_ENABLE = get('PYBB_ATTACHMENT_ENABLE', True)19ATTACHMENT_ENABLE = get('PYBB_ATTACHMENT_ENABLE', True)
20INTERNAL_PERM = get('INTERNAL_PERM', 'pybb.can_access_internal')20INTERNAL_PERM = get('INTERNAL_PERM', 'pybb.can_access_internal')
21LAST_POSTS_DAYS = get('LAST_POSTS_DAYS', 30)
2122
22# That is used internally23# That is used internally
23DISABLE_NOTIFICATION = False24DISABLE_NOTIFICATION = False
2425
=== added file 'pybb/templates/pybb/all_last_posts.html'
--- pybb/templates/pybb/all_last_posts.html 1970-01-01 00:00:00 +0000
+++ pybb/templates/pybb/all_last_posts.html 2019-03-20 21:32:45 +0000
@@ -0,0 +1,73 @@
1{% extends 'pybb/base.html' %}
2
3{% block title %}
4 Latest posts - {{ block.super }}
5{% endblock title %}
6
7{% block content_header %}
8 <h1>Latest Posts</h1>
9{% endblock %}
10
11{% block content_main %}
12<div class="blogEntry">
13 <form action="." method="post" novalidate>
14 {% csrf_token %}
15 <span class="errormessage">{{ form.days.errors }}</span>
16 <label for="id_days">Show posts created up to
17 {{ form.days }} days ago. </label>
18 <span class="errormessage">{{ form.sort_by.errors }}</span>
19 {{ form.sort_by.label_tag }} {{ form.sort_by }}
20 <input type="submit" value="Submit" />
21 {% if form.errors %}
22 <p class="errormessage">
23 An error occured, some default values are used.
24 Please correct the errors above to get valuable results.
25 </p>
26 {% else %}
27 <p>
28 Found {{ posts_count }} posts. The list is always sorted by the most recent post first.
29 </p>
30 {% endif %}
31 </form>
32
33 <hr>
34
35{% if sort_by == 'topic' %}
36 {% for topic, posts in object_list.items %}
37 <h2>Topic: {{ topic }}</h2>
38 <p>
39 At Forum:
40 <a href="{% url 'pybb_forum' topic.forum.id %}">{{ topic.forum }}</a>
41 </p>
42 {% include 'pybb/inlines/latest_posts_table.html'%}
43 {% endfor %}
44
45{% else %} {# sort by forum #}
46 {% for forum, topics in object_list.items %}
47 <h2>Forum: {{ forum }}</h2>
48 <table class='forum'>
49 <thead>
50 <tr>
51 <th style="text-align: left; width: 30%;">Topic{{ topic_list|length|pluralize }}</th>
52 <th style="text-align: left;">Post{{ posts|length|pluralize }}</th>
53 </tr>
54 </thead>
55 <tbody>
56 {% for topic, posts in topics.items %}
57 <tr class={% cycle 'odd' 'even' %}>
58 <td class='post'>
59 <a href="{% url 'pybb_topic' topic.id %}">{{ topic }}</a>
60 </td>
61 <td>
62 {% include 'pybb/inlines/latest_posts_table.html'%}
63 </td>
64 </tr>
65 {% endfor %}
66 </tbody>
67 </table>
68 {% empty %}
69 <p>Sorry, no posts found...</p>
70 {% endfor %}
71{% endif %}
72</div>
73{% endblock %}
074
=== added file 'pybb/templates/pybb/inlines/latest_posts_table.html'
--- pybb/templates/pybb/inlines/latest_posts_table.html 1970-01-01 00:00:00 +0000
+++ pybb/templates/pybb/inlines/latest_posts_table.html 2019-03-20 21:32:45 +0000
@@ -0,0 +1,21 @@
1{% load custom_date %}
2{% load wlprofile_extras %}
3
4<table class='forum'>
5 <thead>
6 <tr>
7 <th style="text-align: left; width: 20%;">Date</th>
8 <th style="text-align: left; width: 20%;">User</th>
9 <th style="text-align: left;">Post{{posts|length|pluralize}}</th>
10 </tr>
11 </thead>
12 <tbody>
13 {% for post in posts %}
14 <tr class="{% cycle 'odd' 'even' %}">
15 <td class='post'>{{ post.created|custom_date:user }}</td>
16 <td class='post'>{{ post.user|user_link }}</td>
17 <td class='post'><a href="{{post.get_absolute_url}}">"{{ post.body_text|truncatechars:80}}"</a></td>
18 </tr>
19 {% endfor %}
20 </tbody>
21</table>
022
=== modified file 'pybb/templates/pybb/last_posts.html'
--- pybb/templates/pybb/last_posts.html 2018-12-10 16:37:12 +0000
+++ pybb/templates/pybb/last_posts.html 2019-03-20 21:32:45 +0000
@@ -15,6 +15,9 @@
15 by {{ post.user|user_link }} {{ post.created|minutes }} ago15 by {{ post.user|user_link }} {{ post.created|minutes }} ago
16 </li>16 </li>
17 {% endfor %}17 {% endfor %}
18 <li class="small">
19 <a href="{% url 'all_latest_posts' %}">More latest posts</a>
20 </li>
18 </ul>21 </ul>
19 </div>22 </div>
20</div>23</div>
2124
=== modified file 'pybb/templatetags/pybb_extras.py'
--- pybb/templatetags/pybb_extras.py 2019-02-27 17:20:01 +0000
+++ pybb/templatetags/pybb_extras.py 2019-03-20 21:32:45 +0000
@@ -20,15 +20,18 @@
2020
21@register.inclusion_tag('pybb/last_posts.html', takes_context=True)21@register.inclusion_tag('pybb/last_posts.html', takes_context=True)
22def pybb_last_posts(context, number=8):22def pybb_last_posts(context, number=8):
23
24 # Create a Queryset
25 last_posts = Post.objects.all().order_by(
26 '-created')
27
28 # Permission dependent Queryset filtering
23 if pybb.views.allowed_for(context.request.user):29 if pybb.views.allowed_for(context.request.user):
24 last_posts = Post.objects.filter(30 last_posts = last_posts.filter(
25 hidden=False).order_by(31 hidden=False)[:100]
26 '-created')[:45]
27 else:32 else:
28 last_posts = Post.objects.filter(33 last_posts = last_posts.filter(
29 hidden=False, topic__forum__category__internal=False).order_by(34 hidden=False, topic__forum__category__internal=False)[:100]
30 '-created')[:45]
31
3235
33 check = []36 check = []
34 answer = []37 answer = []
@@ -39,7 +42,7 @@
39 answer = answer + [post]42 answer = answer + [post]
40 return {43 return {
41 'posts': answer,44 'posts': answer,
42 }45 }
4346
4447
45@register.simple_tag48@register.simple_tag
4649
=== modified file 'pybb/urls.py'
--- pybb/urls.py 2018-12-21 09:50:32 +0000
+++ pybb/urls.py 2019-03-20 21:32:45 +0000
@@ -40,6 +40,7 @@
40 url('^post/(?P<post_id>\d+)/edit/$', views.edit_post, name='pybb_edit_post'),40 url('^post/(?P<post_id>\d+)/edit/$', views.edit_post, name='pybb_edit_post'),
41 url('^post/(?P<post_id>\d+)/delete/$',41 url('^post/(?P<post_id>\d+)/delete/$',
42 views.delete_post, name='pybb_delete_post'),42 views.delete_post, name='pybb_delete_post'),
43 url(r'^latest_posts/$', views.all_latest, name='all_latest_posts'),
4344
44 # Attachment45 # Attachment
45 url('^attachment/(?P<hash>\w+)/$',46 url('^attachment/(?P<hash>\w+)/$',
4647
=== modified file 'pybb/views.py'
--- pybb/views.py 2019-03-04 17:47:12 +0000
+++ pybb/views.py 2019-03-20 21:32:45 +0000
@@ -1,4 +1,5 @@
1import math1import math
2from collections import OrderedDict
2from mainpage.templatetags.wl_markdown import do_wl_markdown3from mainpage.templatetags.wl_markdown import do_wl_markdown
3from pybb.markups import mypostmarkup4from pybb.markups import mypostmarkup
45
@@ -14,12 +15,14 @@
14from pybb.util import render_to, build_form, quote_text, ajax, urlize15from pybb.util import render_to, build_form, quote_text, ajax, urlize
15from pybb.models import Category, Forum, Topic, Post, Attachment,\16from pybb.models import Category, Forum, Topic, Post, Attachment,\
16 MARKUP_CHOICES17 MARKUP_CHOICES
17from pybb.forms import AddPostForm, EditPostForm18from pybb.forms import AddPostForm, EditPostForm, LastPostsDayForm
18from pybb import settings as pybb_settings19from pybb import settings as pybb_settings
19from pybb.orm import load_related20from pybb.orm import load_related
20from pybb.templatetags.pybb_extras import pybb_moderated_by21from pybb.templatetags.pybb_extras import pybb_moderated_by
2122
22from check_input.models import SuspiciousInput23from check_input.models import SuspiciousInput
24from datetime import date, timedelta
25
2326
24try:27try:
25 from notification import models as notification28 from notification import models as notification
@@ -387,3 +390,97 @@
387 first_post.save()390 first_post.save()
388 391
389 return redirect(topic)392 return redirect(topic)
393
394
395def all_latest_posts(request):
396 """Provide a view to show more latest posts."""
397
398 # default values
399 sort_by_default = 'topic'
400 days_default = pybb_settings.LAST_POSTS_DAYS
401
402 if request.method == 'POST':
403 # Executed when the form is submitted
404 form = LastPostsDayForm(request.POST)
405 if form.is_valid():
406 days = form.cleaned_data['days']
407 sort_by = form.cleaned_data['sort_by']
408 url = '{}?days={days}&sort_by={sort_by}'.format(
409 reverse('all_latest_posts'),
410 days=days, sort_by=sort_by
411 )
412
413 return HttpResponseRedirect(url)
414
415 else: # request GET
416 # Initialize if no values are given or if the
417 # values are given in the url
418 days = request.GET.get('days', days_default)
419 sort_by = request.GET.get('sort_by', sort_by_default)
420
421 # Create a bound form, so error messages are shown if
422 # the given values don't validate against the form
423 form = LastPostsDayForm(
424 {
425 'days': days,
426 'sort_by': sort_by,
427 }
428 )
429
430 if not form.is_valid():
431 # If we are here, the user has likely modified the query in the url
432 # with invalid values and we apply defaults for the database query
433 days = days_default
434 sort_by = sort_by_default
435
436 # Executed on every request (POST and GET)
437 search_date = date.today() - timedelta(int(days))
438
439 # Create a QuerySet ordered by date
440 last_posts = Post.objects.filter(
441 created__gte=search_date,
442 hidden=False,
443 topic__forum__category__internal=False
444 ).order_by('-created')
445
446 # Exclude hidden topics. After this operation last_posts isn't a
447 # type of QuerySet anymore and django queries will not work
448 last_posts = [p for p in last_posts if not p.topic.is_hidden]
449
450 posts_count = len(last_posts)
451
452 if sort_by == 'topic':
453 # The use of an OrderedDict makes sure the ordering of
454 # last_posts get not arbitrary
455 topics = OrderedDict()
456 for post in last_posts:
457 if post.topic not in topics:
458 # Create a new key with a list as value
459 topics[post.topic] = [post]
460 else:
461 # key exists, just add the post
462 topics[post.topic].append(post)
463
464 object_list = topics
465
466 elif sort_by == 'forum':
467 forums = OrderedDict()
468 for post in last_posts:
469 if post.topic.forum.name not in forums:
470 forums[post.topic.forum.name] = OrderedDict({post.topic: [post]})
471 elif post.topic not in forums[post.topic.forum.name]:
472 forums[post.topic.forum.name].update({post.topic: [post]})
473 else:
474 forums[post.topic.forum.name][post.topic].append(post)
475
476 object_list = forums
477
478 return {
479 'object_list': object_list,
480 'posts_count': posts_count,
481 'form': form,
482 'sort_by': sort_by
483 }
484
485
486all_latest = render_to('pybb/all_last_posts.html')(all_latest_posts)

Subscribers

People subscribed via source and target branches