mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-25 02:24:26 +00:00
Add some pagination on forum topics
This commit is contained in:
parent
65e2514df2
commit
729a3608ba
@ -144,6 +144,13 @@ class ForumTopic(models.Model):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('forum:view_topic', kwargs={'topic_id': self.id})
|
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
|
@property
|
||||||
def title(self):
|
def title(self):
|
||||||
return self.messages.order_by('date').first().title
|
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
|
return self.topic.forum.is_owned_by(user) or user.id == self.author.id
|
||||||
|
|
||||||
def get_absolute_url(self):
|
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):
|
def mark_as_read(self, user):
|
||||||
try: # Need the try/except because of AnonymousUser
|
try: # Need the try/except because of AnonymousUser
|
||||||
|
@ -42,7 +42,7 @@
|
|||||||
<div class="topic">
|
<div class="topic">
|
||||||
<div class="ib w_medium">
|
<div class="ib w_medium">
|
||||||
{% if first_unread %}
|
{% 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 %}
|
{% else %}
|
||||||
<a class="ib w_big" href="{{ url('forum:view_topic', topic_id=topic.id) }}">
|
<a class="ib w_big" href="{{ url('forum:view_topic', topic_id=topic.id) }}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@ -67,7 +67,9 @@
|
|||||||
<div class="ib w_medium" style="text-align: center;">
|
<div class="ib w_medium" style="text-align: center;">
|
||||||
{% set last_msg = topic.messages.order_by('id').select_related('author').last() %}
|
{% set last_msg = topic.messages.order_by('id').select_related('author').last() %}
|
||||||
{{ user_profile_link(last_msg.author) }} <br/>
|
{{ 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
<hr>
|
<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) }}
|
{{ display_message(m, user) }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
@ -38,7 +38,13 @@
|
|||||||
<p>{{ topic.description }}</p>
|
<p>{{ topic.description }}</p>
|
||||||
<p><a href="{{ url('forum:new_message', topic_id=topic.id) }}">{% trans %}Reply{% endtrans %}</a></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 %}
|
{% if m.id == first_unread_message_id %}
|
||||||
<span id="first_unread"></span>
|
<span id="first_unread"></span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@ -50,6 +56,12 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
<p><a href="{{ url('forum:new_message', topic_id=topic.id) }}">{% trans %}Reply{% endtrans %}</a></p>
|
<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 %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
||||||
|
@ -135,10 +135,20 @@ class ForumTopicDetailView(CanViewMixin, DetailView):
|
|||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super(ForumTopicDetailView, self).get_context_data(**kwargs)
|
kwargs = super(ForumTopicDetailView, self).get_context_data(**kwargs)
|
||||||
try:
|
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
|
kwargs['first_unread_message_id'] = msg.id
|
||||||
except:
|
except:
|
||||||
kwargs['first_unread_message_id'] = float("inf")
|
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
|
return kwargs
|
||||||
|
|
||||||
class ForumMessageEditView(CanEditMixin, UpdateView):
|
class ForumMessageEditView(CanEditMixin, UpdateView):
|
||||||
|
@ -281,6 +281,9 @@ SITH_CLUB_REFOUND_ID = 89
|
|||||||
SITH_COUNTER_REFOUND_ID = 38
|
SITH_COUNTER_REFOUND_ID = 38
|
||||||
SITH_PRODUCT_REFOUND_ID = 5
|
SITH_PRODUCT_REFOUND_ID = 5
|
||||||
|
|
||||||
|
# Forum
|
||||||
|
|
||||||
|
SITH_FORUM_PAGE_LENGTH = 30
|
||||||
|
|
||||||
# SAS variables
|
# SAS variables
|
||||||
SITH_SAS_ROOT_DIR_ID = 4
|
SITH_SAS_ROOT_DIR_ID = 4
|
||||||
|
Loading…
Reference in New Issue
Block a user