dnscms: prefetch images and related objects that GraphQL listings resolve per item
This commit is contained in:
6 files changed
+40
-7
No files matched your search
@@ -18,7 +18,9 @@ from dnscms.wordpress.models import DeferWPFieldsManagerMixin, WPImportedPageMix
|
|||||||
|
|
||||||
|
|
||||||
class AssociationPageManager(DeferWPFieldsManagerMixin, PageManager):
|
class AssociationPageManager(DeferWPFieldsManagerMixin, PageManager):
|
||||||
pass
|
def get_queryset(self):
|
||||||
|
# GraphQL lists load these through here; saves a query per item
|
||||||
|
return super().get_queryset().prefetch_related("logo")
|
||||||
|
|
||||||
|
|
||||||
@register_singular_query_field("associationIndex")
|
@register_singular_query_field("associationIndex")
|
||||||
|
|||||||
@@ -42,6 +42,12 @@ class ContactIndex(HeadlessMixin, Page):
|
|||||||
search_fields = Page.search_fields + [index.SearchField("body")]
|
search_fields = Page.search_fields + [index.SearchField("body")]
|
||||||
|
|
||||||
|
|
||||||
|
class ContactEntityManager(models.Manager):
|
||||||
|
def get_queryset(self):
|
||||||
|
# GraphQL lists load these through here; saves a query per item
|
||||||
|
return super().get_queryset().prefetch_related("image")
|
||||||
|
|
||||||
|
|
||||||
@register_snippet
|
@register_snippet
|
||||||
@register_query_field("contactEntity", "contactEntities")
|
@register_query_field("contactEntity", "contactEntities")
|
||||||
class ContactEntity(models.Model):
|
class ContactEntity(models.Model):
|
||||||
@@ -73,6 +79,8 @@ class ContactEntity(models.Model):
|
|||||||
related_name="+",
|
related_name="+",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
objects = ContactEntityManager()
|
||||||
|
|
||||||
title_widget = forms.TextInput(attrs={"placeholder": "Navn*"})
|
title_widget = forms.TextInput(attrs={"placeholder": "Navn*"})
|
||||||
|
|
||||||
panels = [
|
panels = [
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import graphene
|
import graphene
|
||||||
|
|
||||||
|
from django.db.models import prefetch_related_objects
|
||||||
from django.templatetags.static import static
|
from django.templatetags.static import static
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
from grapple.registry import registry as grapple_registry
|
from grapple.registry import registry as grapple_registry
|
||||||
@@ -10,6 +11,7 @@ from wagtail.models import Page
|
|||||||
from wagtail.search.backends import get_search_backend
|
from wagtail.search.backends import get_search_backend
|
||||||
|
|
||||||
from dnscms.rich_text import register_button_link_feature
|
from dnscms.rich_text import register_button_link_feature
|
||||||
|
from events.models import EventPage
|
||||||
|
|
||||||
|
|
||||||
@hooks.register("register_rich_text_features")
|
@hooks.register("register_rich_text_features")
|
||||||
@@ -65,7 +67,10 @@ def override_search_field(query_mixins):
|
|||||||
return []
|
return []
|
||||||
ids = [p.id for p in ranked]
|
ids = [p.id for p in ranked]
|
||||||
specific_map = {p.id: p for p in Page.objects.filter(id__in=ids).specific()}
|
specific_map = {p.id: p for p in Page.objects.filter(id__in=ids).specific()}
|
||||||
return [specific_map[i] for i in ids if i in specific_map]
|
results = [specific_map[i] for i in ids if i in specific_map]
|
||||||
|
events = [p for p in results if isinstance(p, EventPage)]
|
||||||
|
prefetch_related_objects(events, "occurrences")
|
||||||
|
return results
|
||||||
|
|
||||||
query_mixins.insert(0, SearchOverrideMixin)
|
query_mixins.insert(0, SearchOverrideMixin)
|
||||||
|
|
||||||
|
|||||||
+17
-3
@@ -164,10 +164,16 @@ class EventOrganizerLink(Orderable):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class EventOrganizerManager(WPAwareManager):
|
||||||
|
def get_queryset(self):
|
||||||
|
# GraphQL lists load these through here; saves a query per item
|
||||||
|
return super().get_queryset().prefetch_related("association")
|
||||||
|
|
||||||
|
|
||||||
# registered as a snippet via EventOrganizerSnippetViewSet in wagtail_hooks.py
|
# registered as a snippet via EventOrganizerSnippetViewSet in wagtail_hooks.py
|
||||||
@register_query_field("eventOrganizer", "eventOrganizers")
|
@register_query_field("eventOrganizer", "eventOrganizers")
|
||||||
class EventOrganizer(index.Indexed, ClusterableModel):
|
class EventOrganizer(index.Indexed, ClusterableModel):
|
||||||
objects = WPAwareManager()
|
objects = EventOrganizerManager()
|
||||||
|
|
||||||
name = models.CharField(
|
name = models.CharField(
|
||||||
max_length=100,
|
max_length=100,
|
||||||
@@ -250,7 +256,9 @@ class EventPageManager(
|
|||||||
DeferWPFieldsManagerMixin,
|
DeferWPFieldsManagerMixin,
|
||||||
PageManager.from_queryset(EventPageQuerySet),
|
PageManager.from_queryset(EventPageQuerySet),
|
||||||
):
|
):
|
||||||
pass
|
def get_queryset(self):
|
||||||
|
# GraphQL lists load these through here; saves a query per item
|
||||||
|
return super().get_queryset().prefetch_related("featured_image")
|
||||||
|
|
||||||
|
|
||||||
class EventPage(HeadlessMixin, WPImportedPageMixin, Page):
|
class EventPage(HeadlessMixin, WPImportedPageMixin, Page):
|
||||||
@@ -450,8 +458,14 @@ class EventPage(HeadlessMixin, WPImportedPageMixin, Page):
|
|||||||
self.price_member = ""
|
self.price_member = ""
|
||||||
|
|
||||||
|
|
||||||
|
class EventOccurrenceManager(WPAwareManager):
|
||||||
|
def get_queryset(self):
|
||||||
|
# GraphQL lists load these through here; saves a query per item
|
||||||
|
return super().get_queryset().prefetch_related("venue")
|
||||||
|
|
||||||
|
|
||||||
class EventOccurrence(Orderable):
|
class EventOccurrence(Orderable):
|
||||||
objects = WPAwareManager()
|
objects = EventOccurrenceManager()
|
||||||
|
|
||||||
event = ParentalKey(EventPage, on_delete=models.CASCADE, related_name="occurrences")
|
event = ParentalKey(EventPage, on_delete=models.CASCADE, related_name="occurrences")
|
||||||
start = models.DateTimeField()
|
start = models.DateTimeField()
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ from dnscms.wordpress.models import DeferWPFieldsManagerMixin, WPImportedPageMix
|
|||||||
|
|
||||||
|
|
||||||
class NewsPageManager(DeferWPFieldsManagerMixin, PageManager):
|
class NewsPageManager(DeferWPFieldsManagerMixin, PageManager):
|
||||||
pass
|
def get_queryset(self):
|
||||||
|
# GraphQL lists load these through here; saves a query per item
|
||||||
|
return super().get_queryset().prefetch_related("featured_image")
|
||||||
|
|
||||||
|
|
||||||
@register_singular_query_field("newsIndex")
|
@register_singular_query_field("newsIndex")
|
||||||
|
|||||||
@@ -20,7 +20,9 @@ from dnscms.wordpress.models import DeferWPFieldsManagerMixin, WPImportedPageMix
|
|||||||
|
|
||||||
|
|
||||||
class VenuePageManager(DeferWPFieldsManagerMixin, PageManager):
|
class VenuePageManager(DeferWPFieldsManagerMixin, PageManager):
|
||||||
pass
|
def get_queryset(self):
|
||||||
|
# GraphQL lists load these through here; saves a query per item
|
||||||
|
return super().get_queryset().prefetch_related("featured_image")
|
||||||
|
|
||||||
|
|
||||||
@register_singular_query_field("venueIndex")
|
@register_singular_query_field("venueIndex")
|
||||||
|
|||||||
Reference in new issue
Block a user