Commit 729a3608 authored by Skia's avatar Skia

Add some pagination on forum topics

parent 65e2514d
Pipeline #795 passed with stage
in 3 minutes and 22 seconds
......@@ -144,6 +144,13 @@ class ForumTopic(models.Model):
def get_absolute_url(self):
return reverse('forum:view_topic', kwargs={'topic_id': self.id})
def get_first_unread_message(self, user):
try:
msg = self.messages.exclude(readers=user).filter(date__gte=user.forum_infos.last_read_date).order_by('id').first()
return msg
except:
return None
@property
def title(self):
return self.messages.order_by('date').first().title
......@@ -179,7 +186,10 @@ class ForumMessage(models.Model):
return self.topic.forum.is_owned_by(user) or user.id == self.author.id
def get_absolute_url(self):
return self.topic.get_absolute_url() + "#msg_" + str(self.id)
return self.topic.get_absolute_url() + "?page=" + str(self.get_page()) + "#msg_" + str(self.id)
def get_page(self):
return int(self.topic.messages.filter(id__lt=self.id).count() / settings.SITH_FORUM_PAGE_LENGTH) + 1
def mark_as_read(self, user):
try: # Need the try/except because of AnonymousUser
......
......@@ -42,7 +42,7 @@
<div class="topic">
<div class="ib w_medium">
{% if first_unread %}
<a class="ib w_big" href="{{ url('forum:view_topic', topic_id=topic.id) }}#first_unread">
<a class="ib w_big" href="{{ topic.get_first_unread_message(user).get_absolute_url() }}">
{% else %}
<a class="ib w_big" href="{{ url('forum:view_topic', topic_id=topic.id) }}">
{% endif %}
......@@ -67,7 +67,9 @@
<div class="ib w_medium" style="text-align: center;">
{% set last_msg = topic.messages.order_by('id').select_related('author').last() %}
{{ user_profile_link(last_msg.author) }} <br/>
{{ last_msg.date|date(DATETIME_FORMAT) }} {{ last_msg.date|time(DATETIME_FORMAT) }}
<a href="{{ last_msg.get_absolute_url() }}">
{{ last_msg.date|date(DATETIME_FORMAT) }} {{ last_msg.date|time(DATETIME_FORMAT) }}
</a>
</div>
</div>
</div>
......
......@@ -24,7 +24,7 @@
<hr>
{% for m in topic.messages.select_related('author__avatar_pict').order_by('-id') %}
{% for m in topic.messages.select_related('author__avatar_pict').order_by('-id')[:10] %}
{{ display_message(m, user) }}
{% endfor %}
......
......@@ -38,7 +38,13 @@
<p>{{ topic.description }}</p>
<p><a href="{{ url('forum:new_message', topic_id=topic.id) }}">{% trans %}Reply{% endtrans %}</a></p>
{% for m in topic.messages.select_related('author__avatar_pict').all() %}
<p style="text-align: right; background: #d8e7f3;">
{% for p in range(1, page_count + 1) %}
<span class="ib" style="background: {% if p == current_page %}white{% endif %}; margin: 0;"><a href="?page={{ p }}">{{ p }}</a></span>
{% endfor %}
</p>
{% for m in msgs %}
{% if m.id == first_unread_message_id %}
<span id="first_unread"></span>
{% endif %}
......@@ -50,6 +56,12 @@
{% endfor %}
<p><a href="{{ url('forum:new_message', topic_id=topic.id) }}">{% trans %}Reply{% endtrans %}</a></p>
<p style="text-align: right; background: #d8e7f3;">
{% for p in range(1, page_count + 1) %}
<span class="ib" style="background: {% if p == current_page %}white{% endif %}; margin: 0;"><a href="?page={{ p }}">{{ p }}</a></span>
{% endfor %}
</p>
{% endblock %}
......
......@@ -135,10 +135,20 @@ class ForumTopicDetailView(CanViewMixin, DetailView):
def get_context_data(self, **kwargs):
kwargs = super(ForumTopicDetailView, self).get_context_data(**kwargs)
try:
msg = self.object.messages.exclude(readers=self.request.user).filter(date__gte=self.request.user.forum_infos.last_read_date).order_by('id').first()
msg = self.object.get_first_unread_message(user)
kwargs['first_unread_message_id'] = msg.id
except:
kwargs['first_unread_message_id'] = float("inf")
page_length = settings.SITH_FORUM_PAGE_LENGTH
try:
page = int(self.request.GET['page']) - 1
if page < 0:
page = 0
except:
page = 0
kwargs["msgs"] = self.object.messages.select_related('author__avatar_pict').all()[page*page_length:page*page_length+page_length]
kwargs["page_count"] = int(self.object.messages.count() / page_length) + 1
kwargs["current_page"] = page + 1
return kwargs
class ForumMessageEditView(CanEditMixin, UpdateView):
......
......@@ -281,6 +281,9 @@ SITH_CLUB_REFOUND_ID = 89
SITH_COUNTER_REFOUND_ID = 38
SITH_PRODUCT_REFOUND_ID = 5
# Forum
SITH_FORUM_PAGE_LENGTH = 30
# SAS variables
SITH_SAS_ROOT_DIR_ID = 4
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment