diff --git a/forum/models.py b/forum/models.py index 45006ba0..f7c4a481 100644 --- a/forum/models.py +++ b/forum/models.py @@ -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 diff --git a/forum/templates/forum/macros.jinja b/forum/templates/forum/macros.jinja index ba1dada2..dbf412a3 100644 --- a/forum/templates/forum/macros.jinja +++ b/forum/templates/forum/macros.jinja @@ -42,7 +42,7 @@
{{ topic.description }}
{% trans %}Reply{% endtrans %}
- {% for m in topic.messages.select_related('author__avatar_pict').all() %} ++ {% for p in range(1, page_count + 1) %} + {{ p }} + {% endfor %} +
+ + {% for m in msgs %} {% if m.id == first_unread_message_id %} {% endif %} @@ -50,6 +56,12 @@ {% endfor %}{% trans %}Reply{% endtrans %}
+ ++ {% for p in range(1, page_count + 1) %} + {{ p }} + {% endfor %} +
{% endblock %} diff --git a/forum/views.py b/forum/views.py index 4bbc39eb..e5ebc989 100644 --- a/forum/views.py +++ b/forum/views.py @@ -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): diff --git a/sith/settings.py b/sith/settings.py index 24b0ea62..86fc473b 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -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