Commit 97a39b06 authored by Skia's avatar Skia

Make **a lot** of Forum improvements in reducing the number of queries per page

parent d7135e4d
...@@ -29,3 +29,4 @@ from forum.models import * ...@@ -29,3 +29,4 @@ from forum.models import *
admin.site.register(Forum) admin.site.register(Forum)
admin.site.register(ForumTopic) admin.site.register(ForumTopic)
admin.site.register(ForumMessage) admin.site.register(ForumMessage)
admin.site.register(ForumUserInfo)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0003_auto_20170510_1754'),
]
operations = [
migrations.AlterModelOptions(
name='forummessage',
options={'ordering': ['-date']},
),
migrations.AlterModelOptions(
name='forumtopic',
options={'ordering': ['-_last_message__date']},
),
migrations.AddField(
model_name='forum',
name='_last_message',
field=models.ForeignKey(related_name='forums_where_its_last', to='forum.ForumMessage', null=True, verbose_name='the last message'),
),
migrations.AddField(
model_name='forum',
name='_topic_number',
field=models.IntegerField(default=0, verbose_name='number of topics'),
),
migrations.AddField(
model_name='forummessage',
name='_deleted',
field=models.BooleanField(default=False, verbose_name='is deleted'),
),
migrations.AddField(
model_name='forumtopic',
name='_last_message',
field=models.ForeignKey(related_name='+', to='forum.ForumMessage', null=True, verbose_name='the last message'),
),
migrations.AddField(
model_name='forumtopic',
name='_title',
field=models.CharField(max_length=64, blank=True, verbose_name='title'),
),
migrations.AlterField(
model_name='forum',
name='description',
field=models.CharField(max_length=512, default='', verbose_name='description'),
),
migrations.AlterField(
model_name='forum',
name='id',
field=models.AutoField(primary_key=True, serialize=False, db_index=True),
),
]
This diff is collapsed.
...@@ -38,6 +38,7 @@ urlpatterns = [ ...@@ -38,6 +38,7 @@ urlpatterns = [
url(r'^topic/(?P<topic_id>[0-9]+)$', ForumTopicDetailView.as_view(), name='view_topic'), url(r'^topic/(?P<topic_id>[0-9]+)$', ForumTopicDetailView.as_view(), name='view_topic'),
url(r'^topic/(?P<topic_id>[0-9]+)/edit$', ForumTopicEditView.as_view(), name='edit_topic'), url(r'^topic/(?P<topic_id>[0-9]+)/edit$', ForumTopicEditView.as_view(), name='edit_topic'),
url(r'^topic/(?P<topic_id>[0-9]+)/new_message$', ForumMessageCreateView.as_view(), name='new_message'), url(r'^topic/(?P<topic_id>[0-9]+)/new_message$', ForumMessageCreateView.as_view(), name='new_message'),
url(r'^message/(?P<message_id>[0-9]+)$', ForumMessageView.as_view(), name='view_message'),
url(r'^message/(?P<message_id>[0-9]+)/edit$', ForumMessageEditView.as_view(), name='edit_message'), url(r'^message/(?P<message_id>[0-9]+)/edit$', ForumMessageEditView.as_view(), name='edit_message'),
url(r'^message/(?P<message_id>[0-9]+)/delete$', ForumMessageDeleteView.as_view(), name='delete_message'), url(r'^message/(?P<message_id>[0-9]+)/delete$', ForumMessageDeleteView.as_view(), name='delete_message'),
url(r'^message/(?P<message_id>[0-9]+)/undelete$', ForumMessageUndeleteView.as_view(), name='undelete_message'), url(r'^message/(?P<message_id>[0-9]+)/undelete$', ForumMessageUndeleteView.as_view(), name='undelete_message'),
......
...@@ -41,7 +41,7 @@ from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMi ...@@ -41,7 +41,7 @@ from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMi
from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta
class ForumMainView(ListView): class ForumMainView(ListView):
queryset = Forum.objects.filter(parent=None) queryset = Forum.objects.filter(parent=None).select_related("_last_message__author", "_last_message__topic___title")
template_name = "forum/main.jinja" template_name = "forum/main.jinja"
class ForumMarkAllAsRead(RedirectView): class ForumMarkAllAsRead(RedirectView):
...@@ -53,6 +53,8 @@ class ForumMarkAllAsRead(RedirectView): ...@@ -53,6 +53,8 @@ class ForumMarkAllAsRead(RedirectView):
fi = request.user.forum_infos fi = request.user.forum_infos
fi.last_read_date = timezone.now() fi.last_read_date = timezone.now()
fi.save() fi.save()
for m in request.user.read_messages.filter(date__lt=fi.last_read_date):
m.readers.remove(request.user) # Clean up to keep table low in data
except: pass except: pass
return super(ForumMarkAllAsRead, self).get(request, *args, **kwargs) return super(ForumMarkAllAsRead, self).get(request, *args, **kwargs)
...@@ -62,9 +64,11 @@ class ForumLastUnread(ListView): ...@@ -62,9 +64,11 @@ class ForumLastUnread(ListView):
paginate_by = settings.SITH_FORUM_PAGE_LENGTH / 2 paginate_by = settings.SITH_FORUM_PAGE_LENGTH / 2
def get_queryset(self): def get_queryset(self):
l = ForumMessage.objects.exclude(readers=self.request.user).filter( topic_list = self.model.objects.filter(_last_message__date__gt=self.request.user.forum_infos.last_read_date)\
date__gt=self.request.user.forum_infos.last_read_date).values_list('topic') # TODO try to do better .exclude(_last_message__readers=self.request.user)\
return self.model.objects.filter(id__in=l).annotate(models.Max('messages__date')).order_by('-messages__date__max').select_related('author') .order_by('-_last_message__date')\
.select_related('_last_message__author__avatar_pict')
return topic_list
class ForumForm(forms.ModelForm): class ForumForm(forms.ModelForm):
class Meta: class Meta:
...@@ -185,6 +189,15 @@ class ForumTopicDetailView(CanViewMixin, DetailView): ...@@ -185,6 +189,15 @@ class ForumTopicDetailView(CanViewMixin, DetailView):
kwargs["msgs"] = paginator.page(paginator.num_pages) kwargs["msgs"] = paginator.page(paginator.num_pages)
return kwargs return kwargs
class ForumMessageView(SingleObjectMixin, RedirectView):
model = ForumMessage
pk_url_kwarg = "message_id"
permanent = False
def get_redirect_url(self, *args, **kwargs):
self.object = self.get_object()
return self.object.get_url()
class ForumMessageEditView(CanEditMixin, UpdateView): class ForumMessageEditView(CanEditMixin, UpdateView):
model = ForumMessage model = ForumMessage
fields = ['title', 'message'] fields = ['title', 'message']
......
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