Skip to content

Commit 74951a7

Browse files
committed
fix: metadata into the "ModelMeta"-subclass PostContent
- _metadata-attribute on Post itself was wrong -> the meta didn't work there because it was fetched from PostContent => so most data was None / not set
1 parent 83175f4 commit 74951a7

File tree

3 files changed

+163
-58
lines changed

3 files changed

+163
-58
lines changed

djangocms_blog/models.py

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -330,35 +330,6 @@ class Post(KnockerModel, models.Model):
330330

331331
related = SortedManyToManyField("self", verbose_name=_("Related Posts"), blank=True, symmetrical=False)
332332

333-
_metadata = {
334-
"title": "get_title",
335-
"description": "get_description",
336-
"keywords": "get_keywords",
337-
"og_description": "get_description",
338-
"twitter_description": "get_description",
339-
"schemaorg_description": "get_description",
340-
"locale": "get_locale",
341-
"image": "get_image_full_url",
342-
"image_width": "get_image_width",
343-
"image_height": "get_image_height",
344-
"object_type": "get_meta_attribute",
345-
"og_type": "get_meta_attribute",
346-
"og_app_id": "get_meta_attribute",
347-
"og_profile_id": "get_meta_attribute",
348-
"og_publisher": "get_meta_attribute",
349-
"og_author_url": "get_meta_attribute",
350-
"og_author": "get_meta_attribute",
351-
"twitter_type": "get_meta_attribute",
352-
"twitter_site": "get_meta_attribute",
353-
"twitter_author": "get_meta_attribute",
354-
"schemaorg_type": "get_meta_attribute",
355-
"published_time": "date_published",
356-
"modified_time": "date_modified",
357-
"expiration_time": "date_published_end",
358-
"tag": "get_tags",
359-
"url": "get_absolute_url",
360-
}
361-
362333
class Meta:
363334
verbose_name = _("post")
364335
verbose_name_plural = _("posts")
@@ -602,6 +573,35 @@ class Meta:
602573
objects = GenericDateTaggedManager()
603574
admin_manager = AdminDateTaggedManager()
604575

576+
_metadata = {
577+
"title": "get_title",
578+
"description": "get_description",
579+
"keywords": "get_keywords",
580+
"og_description": "get_description",
581+
"twitter_description": "get_description",
582+
"schemaorg_description": "get_description",
583+
"locale": "get_locale",
584+
"image": "get_image_full_url",
585+
"image_width": "get_image_width",
586+
"image_height": "get_image_height",
587+
"object_type": "get_meta_attribute",
588+
"og_type": "get_meta_attribute",
589+
"og_app_id": "get_meta_attribute",
590+
"og_profile_id": "get_meta_attribute",
591+
"og_publisher": "get_meta_attribute",
592+
"og_author_url": "get_meta_attribute",
593+
"og_author": "get_meta_attribute",
594+
"twitter_type": "get_meta_attribute",
595+
"twitter_site": "get_meta_attribute",
596+
"twitter_author": "get_meta_attribute",
597+
"schemaorg_type": "get_meta_attribute",
598+
"published_time": "date_published",
599+
"modified_time": "date_modified",
600+
"expiration_time": "date_published_end",
601+
"tag": "get_tags",
602+
"url": "get_absolute_url",
603+
}
604+
605605
@property
606606
def author(self):
607607
return self.post.author
@@ -651,6 +651,54 @@ def get_template(self):
651651
return "djangocms_blog/no_post_structure.html"
652652
return "djangocms_blog/post_structure.html"
653653

654+
def get_title(self):
655+
title = self.meta_title
656+
if not title:
657+
title = self.title or _("No title")
658+
return title.strip()
659+
660+
def get_keywords(self):
661+
"""
662+
Returns the list of keywords (as python list)
663+
:return: list
664+
"""
665+
return self.meta_keywords.strip().split(",")
666+
667+
def get_description(self):
668+
description = self.meta_description
669+
if not description:
670+
description = self.abstract
671+
return strip_tags(description).strip()
672+
673+
def get_image_full_url(self):
674+
if self.post.main_image:
675+
if thumbnail_options := get_setting("META_IMAGE_SIZE"):
676+
thumbnail_url = get_thumbnailer(self.post.main_image).get_thumbnail(thumbnail_options).url
677+
return self.build_absolute_uri(thumbnail_url)
678+
return self.build_absolute_uri(self.post.main_image.url)
679+
return ""
680+
681+
def get_image_width(self):
682+
if self.post.main_image:
683+
thumbnail_options = get_setting("META_IMAGE_SIZE")
684+
if thumbnail_options:
685+
return get_thumbnailer(self.post.main_image).get_thumbnail(thumbnail_options).width
686+
return self.post.main_image.width
687+
688+
def get_image_height(self):
689+
if self.post.main_image:
690+
thumbnail_options = get_setting("META_IMAGE_SIZE")
691+
if thumbnail_options:
692+
return get_thumbnailer(self.post.main_image).get_thumbnail(thumbnail_options).height
693+
return self.post.main_image.height
694+
695+
def get_tags(self):
696+
"""
697+
Returns the list of object tags as comma separated list
698+
"""
699+
taglist = [tag.name for tag in self.tags.all()]
700+
return ",".join(taglist)
701+
654702
def __str__(self):
655703
return self.title or _("Untitled")
656704

djangocms_stories/models.py

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -332,35 +332,6 @@ class Post(models.Model):
332332

333333
objects = GenericDateTaggedManager()
334334

335-
_metadata = {
336-
"title": "get_title",
337-
"description": "get_description",
338-
"keywords": "get_keywords",
339-
"og_description": "get_description",
340-
"twitter_description": "get_description",
341-
"schemaorg_description": "get_description",
342-
"locale": "get_locale",
343-
"image": "get_image_full_url",
344-
"image_width": "get_image_width",
345-
"image_height": "get_image_height",
346-
"object_type": "get_meta_attribute",
347-
"og_type": "get_meta_attribute",
348-
"og_app_id": "get_meta_attribute",
349-
"og_profile_id": "get_meta_attribute",
350-
"og_publisher": "get_meta_attribute",
351-
"og_author_url": "get_meta_attribute",
352-
"og_author": "get_meta_attribute",
353-
"twitter_type": "get_meta_attribute",
354-
"twitter_site": "get_meta_attribute",
355-
"twitter_author": "get_meta_attribute",
356-
"schemaorg_type": "get_meta_attribute",
357-
"published_time": "date_published",
358-
"modified_time": "date_modified",
359-
"expiration_time": "date_published_end",
360-
"tag": "get_tags",
361-
"url": "get_absolute_url",
362-
}
363-
364335
class Meta:
365336
verbose_name = _("post")
366337
verbose_name_plural = _("posts")
@@ -590,6 +561,35 @@ class Meta:
590561
# objects = GenericDateTaggedManager()
591562
# admin_manager = AdminDateTaggedManager()
592563

564+
_metadata = {
565+
"title": "get_title",
566+
"description": "get_description",
567+
"keywords": "get_keywords",
568+
"og_description": "get_description",
569+
"twitter_description": "get_description",
570+
"schemaorg_description": "get_description",
571+
"locale": "get_locale",
572+
"image": "get_image_full_url",
573+
"image_width": "get_image_width",
574+
"image_height": "get_image_height",
575+
"object_type": "get_meta_attribute",
576+
"og_type": "get_meta_attribute",
577+
"og_app_id": "get_meta_attribute",
578+
"og_profile_id": "get_meta_attribute",
579+
"og_publisher": "get_meta_attribute",
580+
"og_author_url": "get_meta_attribute",
581+
"og_author": "get_meta_attribute",
582+
"twitter_type": "get_meta_attribute",
583+
"twitter_site": "get_meta_attribute",
584+
"twitter_author": "get_meta_attribute",
585+
"schemaorg_type": "get_meta_attribute",
586+
"published_time": "date_published",
587+
"modified_time": "date_modified",
588+
"expiration_time": "date_published_end",
589+
"tag": "get_tags",
590+
"url": "get_absolute_url",
591+
}
592+
593593
@property
594594
def author(self):
595595
return self.post.author
@@ -602,6 +602,10 @@ def date_published(self):
602602
def date_published_end(self):
603603
return self.post.date_published_end
604604

605+
@property
606+
def date_modified(self):
607+
return self.post.date_modified
608+
605609
@property
606610
def app_config(self):
607611
return self.post.app_config
@@ -641,6 +645,57 @@ def get_template(self):
641645
else:
642646
return f"{folder}/{self.no_structure_template}"
643647

648+
def get_title(self):
649+
title = self.meta_title
650+
if not title:
651+
title = self.title or _("No title")
652+
return title.strip()
653+
654+
def get_keywords(self):
655+
"""
656+
Returns the list of keywords (as python list)
657+
:return: list
658+
"""
659+
keywords = self.meta_keywords.strip()
660+
if "".join(keywords) == "":
661+
return []
662+
return [keyword.strip() for keyword in keywords.split(",")]
663+
664+
def get_description(self):
665+
description = self.meta_description
666+
if not description:
667+
description = self.abstract
668+
return strip_tags(description).strip()
669+
670+
def get_image_full_url(self):
671+
if self.post.main_image:
672+
if thumbnail_options := get_setting("META_IMAGE_SIZE"):
673+
thumbnail_url = get_thumbnailer(self.post.main_image).get_thumbnail(thumbnail_options).url
674+
return self.build_absolute_uri(thumbnail_url)
675+
return self.build_absolute_uri(self.post.main_image.url)
676+
return ""
677+
678+
def get_image_width(self):
679+
if self.post.main_image:
680+
thumbnail_options = get_setting("META_IMAGE_SIZE")
681+
if thumbnail_options:
682+
return get_thumbnailer(self.post.main_image).get_thumbnail(thumbnail_options).width
683+
return self.post.main_image.width
684+
685+
def get_image_height(self):
686+
if self.post.main_image:
687+
thumbnail_options = get_setting("META_IMAGE_SIZE")
688+
if thumbnail_options:
689+
return get_thumbnailer(self.post.main_image).get_thumbnail(thumbnail_options).height
690+
return self.post.main_image.height
691+
692+
def get_tags(self):
693+
"""
694+
Returns the list of object tags as comma separated list
695+
"""
696+
taglist = [tag.name for tag in self.tags.all()]
697+
return ",".join(taglist)
698+
644699
def __str__(self):
645700
return self.title or _("Untitled")
646701

tests/test_views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def test_post_detail_view(client, admin_user, post_content, assert_html_in_respo
2525
assert_html_in_response(
2626
f"<h4>{related_post.subtitle}</h4>", response
2727
) # Subtitle appears in the related posts section
28+
# meta:
29+
assert_html_in_response(f'<meta name="description" content="{post_content.meta_description}">', response)
2830

2931

3032
@pytest.mark.django_db

0 commit comments

Comments
 (0)