mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-26 02:54:20 +00:00
Still reducing the number of queries on the Forum
Signed-off-by: Skia <skia@libskia.so>
This commit is contained in:
parent
ba65dc5d46
commit
06b67f1d27
@ -22,36 +22,41 @@ class Migration(migrations.Migration):
|
|||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='forum',
|
model_name='forum',
|
||||||
name='_last_message',
|
name='_last_message',
|
||||||
field=models.ForeignKey(related_name='forums_where_its_last', to='forum.ForumMessage', null=True, verbose_name='the last message'),
|
field=models.ForeignKey(related_name='forums_where_its_last', null=True, to='forum.ForumMessage', verbose_name='the last message'),
|
||||||
),
|
),
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='forum',
|
model_name='forum',
|
||||||
name='_topic_number',
|
name='_topic_number',
|
||||||
field=models.IntegerField(default=0, verbose_name='number of topics'),
|
field=models.IntegerField(verbose_name='number of topics', default=0),
|
||||||
),
|
),
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='forummessage',
|
model_name='forummessage',
|
||||||
name='_deleted',
|
name='_deleted',
|
||||||
field=models.BooleanField(default=False, verbose_name='is deleted'),
|
field=models.BooleanField(verbose_name='is deleted', default=False),
|
||||||
),
|
),
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='forumtopic',
|
model_name='forumtopic',
|
||||||
name='_last_message',
|
name='_last_message',
|
||||||
field=models.ForeignKey(related_name='+', to='forum.ForumMessage', null=True, verbose_name='the last message'),
|
field=models.ForeignKey(related_name='+', null=True, to='forum.ForumMessage', verbose_name='the last message'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='forumtopic',
|
||||||
|
name='_message_number',
|
||||||
|
field=models.IntegerField(verbose_name='number of messages', default=0),
|
||||||
),
|
),
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='forumtopic',
|
model_name='forumtopic',
|
||||||
name='_title',
|
name='_title',
|
||||||
field=models.CharField(max_length=64, blank=True, verbose_name='title'),
|
field=models.CharField(max_length=64, verbose_name='title', blank=True),
|
||||||
),
|
),
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='forum',
|
model_name='forum',
|
||||||
name='description',
|
name='description',
|
||||||
field=models.CharField(max_length=512, default='', verbose_name='description'),
|
field=models.CharField(max_length=512, verbose_name='description', default=''),
|
||||||
),
|
),
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='forum',
|
model_name='forum',
|
||||||
name='id',
|
name='id',
|
||||||
field=models.AutoField(primary_key=True, serialize=False, db_index=True),
|
field=models.AutoField(serialize=False, db_index=True, primary_key=True),
|
||||||
),
|
),
|
||||||
]
|
]
|
@ -183,6 +183,7 @@ class ForumTopic(models.Model):
|
|||||||
description = models.CharField(_('description'), max_length=256, default="")
|
description = models.CharField(_('description'), max_length=256, default="")
|
||||||
_last_message = models.ForeignKey('ForumMessage', related_name="+", verbose_name=_("the last message"), null=True)
|
_last_message = models.ForeignKey('ForumMessage', related_name="+", verbose_name=_("the last message"), null=True)
|
||||||
_title = models.CharField(_('title'), max_length=64, blank=True)
|
_title = models.CharField(_('title'), max_length=64, blank=True)
|
||||||
|
_message_number = models.IntegerField(_("number of messages"), default=0)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ['-_last_message__date']
|
ordering = ['-_last_message__date']
|
||||||
@ -220,12 +221,7 @@ class ForumTopic(models.Model):
|
|||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def title(self):
|
def title(self):
|
||||||
if self._title:
|
return self._title
|
||||||
return self._title
|
|
||||||
else:
|
|
||||||
self._title = self.messages.order_by('id').first().title
|
|
||||||
self.save()
|
|
||||||
return self._title
|
|
||||||
|
|
||||||
class ForumMessage(models.Model):
|
class ForumMessage(models.Model):
|
||||||
"""
|
"""
|
||||||
@ -250,10 +246,10 @@ class ForumMessage(models.Model):
|
|||||||
super(ForumMessage, self).save(*args, **kwargs)
|
super(ForumMessage, self).save(*args, **kwargs)
|
||||||
if self.is_last_in_topic():
|
if self.is_last_in_topic():
|
||||||
self.topic._last_message_id = self.id
|
self.topic._last_message_id = self.id
|
||||||
self.topic.save()
|
|
||||||
if self.is_first_in_topic():
|
if self.is_first_in_topic():
|
||||||
self.topic._title = self.title
|
self.topic._title = self.title
|
||||||
self.topic.save()
|
self.topic._message_number = self.topic.messages.count()
|
||||||
|
self.topic.save()
|
||||||
|
|
||||||
def is_first_in_topic(self):
|
def is_first_in_topic(self):
|
||||||
return bool(self.id == self.topic.messages.order_by('date').first().id)
|
return bool(self.id == self.topic.messages.order_by('date').first().id)
|
||||||
@ -285,7 +281,8 @@ class ForumMessage(models.Model):
|
|||||||
|
|
||||||
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
|
||||||
self.readers.add(user)
|
if not self.is_read(user):
|
||||||
|
self.readers.add(user)
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
def is_read(self, user):
|
def is_read(self, user):
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{ display_forum(forum, user, True) }}
|
{{ display_forum(forum, user, True) }}
|
||||||
{% for f in forum.children.all() %}
|
{% for f in forum.children.all().select_related("_last_message__author", "_last_message__topic") %}
|
||||||
{{ display_forum(f, user) }}
|
{{ display_forum(f, user) }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -69,7 +69,7 @@
|
|||||||
{{ user_profile_link(topic.author) }}
|
{{ user_profile_link(topic.author) }}
|
||||||
</p>
|
</p>
|
||||||
<p class="ib w_medium" style="text-align: center;">
|
<p class="ib w_medium" style="text-align: center;">
|
||||||
{{ topic.messages.count() }}
|
{{ topic._message_number }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<p class="ib w_medium" style="text-align: center;">
|
<p class="ib w_medium" style="text-align: center;">
|
||||||
|
@ -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).select_related("_last_message__author", "_last_message__topic___title")
|
queryset = Forum.objects.filter(parent=None).prefetch_related("children___last_message__author", "children___last_message__topic")
|
||||||
template_name = "forum/main.jinja"
|
template_name = "forum/main.jinja"
|
||||||
|
|
||||||
class ForumMarkAllAsRead(RedirectView):
|
class ForumMarkAllAsRead(RedirectView):
|
||||||
@ -122,7 +122,8 @@ class ForumDetailView(CanViewMixin, DetailView):
|
|||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super(ForumDetailView, self).get_context_data(**kwargs)
|
kwargs = super(ForumDetailView, self).get_context_data(**kwargs)
|
||||||
qs = self.object.topics.order_by('-_last_message__date').select_related('_last_message')
|
qs = self.object.topics.order_by('-_last_message__date').select_related('_last_message__author', 'author')\
|
||||||
|
.prefetch_related("forum__edit_groups")
|
||||||
paginator = Paginator(qs,
|
paginator = Paginator(qs,
|
||||||
settings.SITH_FORUM_PAGE_LENGTH)
|
settings.SITH_FORUM_PAGE_LENGTH)
|
||||||
page = self.request.GET.get('topic_page')
|
page = self.request.GET.get('topic_page')
|
||||||
@ -178,7 +179,8 @@ class ForumTopicDetailView(CanViewMixin, DetailView):
|
|||||||
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")
|
||||||
paginator = Paginator(self.object.messages.select_related('author__avatar_pict').order_by('date'),
|
paginator = Paginator(self.object.messages.select_related('author__avatar_pict')\
|
||||||
|
.prefetch_related('topic__forum__edit_groups', 'readers').order_by('date'),
|
||||||
settings.SITH_FORUM_PAGE_LENGTH)
|
settings.SITH_FORUM_PAGE_LENGTH)
|
||||||
page = self.request.GET.get('page')
|
page = self.request.GET.get('page')
|
||||||
try:
|
try:
|
||||||
|
@ -1245,7 +1245,7 @@ def migrate_forum():
|
|||||||
id=r['id_sujet'],
|
id=r['id_sujet'],
|
||||||
author=author or root,
|
author=author or root,
|
||||||
forum=parent or saloon,
|
forum=parent or saloon,
|
||||||
_title=to_unicode(r['titre_sujet']),
|
_title=to_unicode(r['titre_sujet'])[:64],
|
||||||
description=to_unicode(r['soustitre_sujet']),
|
description=to_unicode(r['soustitre_sujet']),
|
||||||
)
|
)
|
||||||
topic.save()
|
topic.save()
|
||||||
|
Loading…
Reference in New Issue
Block a user