Skip to content

Commit

Permalink
Merge pull request #1643 from freedomofpress/page-metadata-performance
Browse files Browse the repository at this point in the history
Cache social/SEO values on metadata mixin
  • Loading branch information
SaptakS authored May 15, 2023
2 parents b505a6d + b597bc8 commit 8ffa43e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions common/models/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.db import models
from django.http import Http404
from django.shortcuts import redirect
from django.utils.functional import cached_property
from django.utils.html import strip_tags
from django.utils.module_loading import import_string
from django.template.defaultfilters import truncatewords
Expand Down Expand Up @@ -39,6 +40,7 @@
InfoTableBlock,
)
from common.choices import CATEGORY_SYMBOL_CHOICES, CATEGORY_CHART_CHOICES
from common.models.settings import SocialSharingSEOSettings
from common.utils import (
DEFAULT_PAGE_KEY,
paginate,
Expand Down Expand Up @@ -94,12 +96,12 @@ class Meta:
class MetadataPageMixin(OriginalMetadataPageMixin):
"Provide defaults for metadate for pages in this application"

def _get_ssssettings(self):
# Imported here to avoid circular dependency
from common.models.settings import SocialSharingSEOSettings
@cached_property
def social_sharing_seo_settings(self):
return SocialSharingSEOSettings.for_site(self.get_site())

def get_meta_description(self):
@cached_property
def meta_description(self):
"""
Return either the search_description set on the page or the
default description set for the site
Expand All @@ -111,7 +113,8 @@ def get_meta_description(self):
ssssettings = self._get_ssssettings()
return ssssettings.default_description

def get_meta_image(self):
@cached_property
def meta_image(self):
"""
Return either the search_image set on the page or the
default image set for the site
Expand All @@ -123,6 +126,15 @@ def get_meta_image(self):
ssssettings = self._get_ssssettings()
return ssssettings.default_image

def _get_ssssettings(self):
return self.social_sharing_seo_settings

def get_meta_description(self):
return self.meta_description

def get_meta_image(self):
return self.meta_image

class Meta:
abstract = True

Expand Down

0 comments on commit 8ffa43e

Please sign in to comment.