← Back to team overview

widelands-dev team mailing list archive

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

 

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

Commit message:
Add a 'more latest posts view'

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1635676 in Widelands Website: "Last posts didn't show last 5 posts"
  https://bugs.launchpad.net/widelands-website/+bug/1635676
  Bug #1791802 in Widelands Website: "Create a page to extend the latest posts"
  https://bugs.launchpad.net/widelands-website/+bug/1791802

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

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

I have merged it on alpha for testing: http://alpha.widelands.org/forum/latest_posts/?days=365&sort_by=topic
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/more_latest_posts into lp:widelands-website.
=== 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-18 21:30:36 +0000
@@ -42,7 +42,7 @@
 			<br />
 			<a href="{% url 'logout' %}?next=/">Logout</a>
 		{% else %}
-			<a href="{% url 'login' %}?next={{ request.path|iriencode }}">Login/Register</a>
+			<a href="{% url 'login' %}?next={{ request.get_full_path|urlencode }}">Login/Register</a>
 		{% endif %}
 	</li>
 </ul>

=== modified file 'pybb/forms.py'
--- pybb/forms.py	2018-12-21 11:26:08 +0000
+++ pybb/forms.py	2019-03-18 21:30:36 +0000
@@ -87,3 +87,17 @@
         post.updated = datetime.now()
         post.save(*args, **kwargs)
         return post
+
+
+class LastPostsDayForm(forms.Form):
+    days = forms.IntegerField(
+        max_value = 1000,
+        min_value = 5,
+        label = 'Show posts created these days before today:'
+        )
+    
+    sort_by = forms.ChoiceField(
+        #widget = forms.RadioSelect,
+        choices = [('forum','Forum'),('topic', 'Topic'),],
+        label = 'Sort by:',
+        )

=== modified file 'pybb/settings.py'
--- pybb/settings.py	2018-12-21 12:50:32 +0000
+++ pybb/settings.py	2019-03-18 21:30:36 +0000
@@ -18,6 +18,7 @@
 ATTACHMENT_SIZE_LIMIT = get('PYBB_ATTACHMENT_SIZE_LIMIT', 1024 * 1024)
 ATTACHMENT_ENABLE = get('PYBB_ATTACHMENT_ENABLE', True)
 INTERNAL_PERM =  get('INTERNAL_PERM', 'pybb.can_access_internal')
+LAST_POSTS_DAYS = get('LAST_POSTS_DAYS', 30)
 
 # That is used internally
 DISABLE_NOTIFICATION = False

=== 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-18 21:30:36 +0000
@@ -0,0 +1,74 @@
+{% extends 'pybb/base.html' %}
+
+{% block title %}
+    Latest posts - {{ block.super }}
+{% endblock title %}
+
+{% block content_header %}
+    <h1>Latest Posts</h1>
+{% endblock %}
+
+{% block content_main %}
+<div class="blogEntry">
+
+    <form action="." method="post" novalidate>
+        {% csrf_token %}
+        <span class="errormessage">{{ form.days.errors }}</span>
+        {{ form.days.label_tag }} {{ form.days }}
+        <span class="errormessage">{{ form.sort_by.errors }}</span>
+        {{ form.sort_by.label_tag }} {{ form.sort_by }}
+        <input type="submit" value="Submit" />
+        {% if form.errors %}
+          <p class="errormessage">
+            An error occured, some default values are used.
+            Please correct the erros above to get valuable results.
+          </p>
+        {% else %}
+          <p>
+            Found {{ posts|length }} posts. The list is always sorted by the most recent post first.
+          </p>
+        {% endif %}
+    </form>
+
+    <hr>
+
+    {% if sort_by == 'topic' %}
+      {% regroup posts by topic as topic_list %}
+      {% for topic, posts in topic_list %}
+        <h2>{{ topic }}</h2>
+        <p>At Forum:
+          <a href="{% url 'pybb_forum' posts.0.topic.forum.id %}">{{ posts.0.topic.forum }}</a>
+        </p>
+        {% include 'pybb/inlines/latest_posts_table.html'%}
+      {% endfor %}
+    {% else %} {# sort by forum #}
+      {% regroup posts by topic.forum as forum_list %}
+       {% for forum, posts in forum_list %}
+        <h2>{{ forum }}</h2>
+        {% regroup posts by topic as topic_list %}
+        <table class='forum'>
+          <thead>
+            <tr>
+              <th style="text-align: left; width: 30%;">Topic{{ topic_list|length|pluralize }}</th>
+              <th style="text-align: left;">Post{{ posts|length|pluralize }}</th>
+            </tr>
+          </thead>
+          <tbody>
+          {% for topic, posts in topic_list %}
+            <tr class={% cycle 'odd' 'even' %}>
+              <td class='post'>
+                <a href="{% url 'pybb_topic' posts.0.topic.id %}">{{ posts.0.topic }}</a>
+              </td>
+              <td>
+                  {% include 'pybb/inlines/latest_posts_table.html'%}
+              </td>
+            </tr>
+            {% endfor %}
+          </tbody>
+        </table>
+        {% empty %}
+        <p>Sorry, no posts found...</p>
+      {% endfor %}
+    {% endif %}
+</div>
+{% endblock %}

=== 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-18 21:30:36 +0000
@@ -0,0 +1,21 @@
+{% load custom_date %}
+{% load wlprofile_extras %}
+
+<table class='forum'>
+  <thead>
+    <tr>
+      <th style="text-align: left; width: 20%;">Date</th>
+      <th style="text-align: left; width: 20%;">User</th>
+      <th style="text-align: left;">Post{{posts|length|pluralize}}</th>
+    </tr>
+  </thead>
+  <tbody>
+    {% for post in posts %}
+    <tr class="{% cycle 'odd' 'even' %}">
+      <td class='post'>{{ post.created|custom_date:user }}</td>
+      <td class='post'>{{ post.user|user_link }}</td>
+      <td class='post'><a href="{{post.get_absolute_url}}">"{{ post.body_text|truncatechars:80}}"</a></td>
+    </tr>
+    {% endfor %}
+  </tbody>
+</table>

=== 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-18 21:30:36 +0000
@@ -15,6 +15,9 @@
 					by {{ post.user|user_link }} {{ post.created|minutes }} ago
 				</li>
 			{% endfor %}
+			<li class="small">
+				<a href="{% url 'all_latest_posts' %}">More latest posts</a>
+			</li>
 		</ul>
 	</div>
 </div>

=== 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-18 21:30:36 +0000
@@ -20,15 +20,18 @@
 
 @register.inclusion_tag('pybb/last_posts.html', takes_context=True)
 def pybb_last_posts(context, number=8):
+
+    # Create a Queryset
+    last_posts = Post.objects.all().order_by(
+            '-created')
+
+    # Permission depended Queryset filtering
     if pybb.views.allowed_for(context.request.user):
-        last_posts = Post.objects.filter(
-            hidden=False).order_by(
-            '-created')[:45]
+        last_posts = last_posts.filter(
+            hidden=False)[:100]
     else:
-        last_posts = Post.objects.filter(
-            hidden=False, topic__forum__category__internal=False).order_by(
-            '-created')[:45]
-            
+        last_posts = last_posts.filter(
+            hidden=False, topic__forum__category__internal=False)[:100]
 
     check = []
     answer = []
@@ -39,6 +42,7 @@
                 answer = answer + [post]
     return {
         'posts': answer,
+        'last_posts_days': pybb_settings.LAST_POSTS_DAYS,
     }
 
 

=== modified file 'pybb/urls.py'
--- pybb/urls.py	2018-12-21 09:50:32 +0000
+++ pybb/urls.py	2019-03-18 21:30:36 +0000
@@ -40,6 +40,7 @@
     url('^post/(?P<post_id>\d+)/edit/$', views.edit_post, name='pybb_edit_post'),
     url('^post/(?P<post_id>\d+)/delete/$',
         views.delete_post, name='pybb_delete_post'),
+    url(r'^latest_posts/$', views.all_latest, name='all_latest_posts'),
 
     # Attachment
     url('^attachment/(?P<hash>\w+)/$',

=== modified file 'pybb/views.py'
--- pybb/views.py	2019-03-04 17:47:12 +0000
+++ pybb/views.py	2019-03-18 21:30:36 +0000
@@ -14,12 +14,14 @@
 from pybb.util import render_to, build_form, quote_text, ajax, urlize
 from pybb.models import Category, Forum, Topic, Post, Attachment,\
     MARKUP_CHOICES
-from pybb.forms import AddPostForm, EditPostForm
+from pybb.forms import AddPostForm, EditPostForm, LastPostsDayForm
 from pybb import settings as pybb_settings
 from pybb.orm import load_related
 from pybb.templatetags.pybb_extras import pybb_moderated_by
 
 from check_input.models import SuspiciousInput
+from datetime import date, timedelta
+
 
 try:
     from notification import models as notification
@@ -387,3 +389,71 @@
     first_post.save()
     
     return redirect(topic)
+
+
+def all_latest_posts(request):
+    """Provide a view to show more latest posts."""
+
+    # default values
+    sort_by_default = 'topic'
+    days_default = pybb_settings.LAST_POSTS_DAYS
+
+    if request.method == 'POST':
+        # Executed if the form get submitted
+        form = LastPostsDayForm(request.POST)
+        if form.is_valid():
+            days = form.cleaned_data['days']
+            sort_by = form.cleaned_data['sort_by']
+            url = '{}?days={days}&sort_by={sort_by}'.format(
+                reverse('all_latest_posts'),
+                days=days, sort_by=sort_by
+                )
+
+            return HttpResponseRedirect(url)
+
+    else: # request GET
+        # Initialize if no values are given or if the
+        # values are given in the url
+        days = request.GET.get('days', days_default)
+        sort_by = request.GET.get('sort_by', sort_by_default)
+
+        # Create a bound form, so error messages are shown if
+        # the given values don't validate against the form
+        form = LastPostsDayForm(
+            {
+                'days': days,
+                'sort_by': sort_by,
+            }
+            )
+
+        if not form.is_valid():
+            # I we are here, the user has likely modified the url with invalid
+            # values and we apply defaults for the database query
+            days = days_default
+            sort_by = sort_by_default
+
+    # Executed on every request (POST and GET)
+    search_date = date.today() - timedelta(int(days))
+
+    last_posts = Post.objects.exclude(
+        topic__forum__category__internal=True).filter(
+        created__gte=search_date, hidden=False)
+    
+    if sort_by == 'topic':
+        last_posts = last_posts.order_by('-created', 'topic')
+    elif sort_by == 'forum':
+        last_posts = last_posts.order_by('-created', 'topic__forum')
+    else:
+        last_posts = []
+
+    # exclude hidden topics
+    last_posts = [p for p in last_posts if not p.topic.is_hidden]
+
+    return {
+        'posts': last_posts,
+        'form': form,
+        'sort_by': sort_by
+    }
+
+
+all_latest = render_to('pybb/all_last_posts.html')(all_latest_posts)


Follow ups