0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

Add raw SQL fallback for 'last edited pages' query for MySQL

This commit is contained in:
Matt Westcott 2017-05-04 12:20:19 +01:00
parent 3f2d299171
commit 844cb7746c

View File

@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import connection
from django.db.models import Max from django.db.models import Max
from django.shortcuts import render from django.shortcuts import render
from django.template.loader import render_to_string from django.template.loader import render_to_string
@ -56,10 +57,27 @@ class RecentEditsPanel(object):
# Last n edited pages # Last n edited pages
edit_count = getattr(settings, 'WAGTAILADMIN_RECENT_EDITS_LIMIT', 5) edit_count = getattr(settings, 'WAGTAILADMIN_RECENT_EDITS_LIMIT', 5)
last_edits_dates = (PageRevision.objects.filter(user=self.request.user) if connection.vendor == 'mysql':
.values('page_id').annotate(latest_date=Max('created_at')) # MySQL can't handle the subselect created by the ORM version -
.order_by('-latest_date').values('latest_date')[:edit_count]) # it fails with "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'"
last_edits = PageRevision.objects.filter(created_at__in=last_edits_dates).order_by('-created_at') last_edits = PageRevision.objects.raw(
"""
SELECT wp.* FROM
wagtailcore_pagerevision wp JOIN (
SELECT max(created_at) AS max_created_at, page_id FROM
wagtailcore_pagerevision WHERE user_id = %s GROUP BY page_id ORDER BY max_created_at DESC LIMIT %s
) AS max_rev ON max_rev.max_created_at = wp.created_at ORDER BY wp.created_at DESC
""", [
User._meta.pk.get_db_prep_value(self.request.user.pk, connection),
edit_count
]
)
else:
last_edits_dates = (PageRevision.objects.filter(user=self.request.user)
.values('page_id').annotate(latest_date=Max('created_at'))
.order_by('-latest_date').values('latest_date')[:edit_count])
last_edits = PageRevision.objects.filter(created_at__in=last_edits_dates).order_by('-created_at')
page_keys = [pr.page_id for pr in last_edits] page_keys = [pr.page_id for pr in last_edits]
pages = Page.objects.specific().in_bulk(page_keys) pages = Page.objects.specific().in_bulk(page_keys)
self.last_edits = [ self.last_edits = [