def test_page_meta_gplus(self): """ Tests the Google+ schema data """ page, __ = self.get_pages() page_meta = models.PageMeta.objects.create(extended_object=page) for key, val in self.page_data.items(): setattr(page_meta, key, val) for key, val in self.gplus_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual(meta.gplus_author, 'https://plus.google.com/{0}'.format(self.gplus_data['gplus_author'])) self.assertEqual(meta.gplus_type, self.gplus_data['gplus_type']) new_data = copy(self.gplus_data) new_data['gplus_author'] = 'https://plus.google.com/+SomeUser' for key, val in new_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual(meta.gplus_author, new_data['gplus_author']) new_data = copy(self.gplus_data) new_data['gplus_author'] = '/SomePage' for key, val in new_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual(meta.gplus_author, 'https://plus.google.com{0}'.format(new_data['gplus_author']))
def test_none_page(self): meta = get_page_meta(None, 'en') self.assertIsNone(meta) request = self.get_page_request(SimpleLazyObject(lambda: None), self.user, '/') meta = get_page_meta(request.current_page, 'en') self.assertIsNone(meta)
def test_page_meta_og(self): """ Tests the OpenGraph meta tags """ page, page_2 = self.get_pages() page_meta = models.PageMeta.objects.create(extended_object=page) for key, val in self.page_data.items(): setattr(page_meta, key, val) for key, val in self.og_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual(meta.og_type, self.og_data['og_type']) self.assertEqual(meta.og_author_url, self.og_data['og_author_url']) self.assertEqual(meta.og_profile_id, self.og_data['og_author_fbid']) self.assertEqual(meta.og_publisher, self.og_data['og_publisher']) self.assertEqual(meta.og_app_id, self.og_data['og_app_id']) self.assertEqual(meta.published_time, page.publication_date.isoformat()) self.assertEqual(meta.modified_time, page.changed_date.isoformat()) if page.publication_end_date: self.assertEqual(meta.expiration_time, page.publication_end_date.isoformat()) else: self.assertFalse(hasattr(meta, 'expiration_time'))
def test_custom_extra(self): page1, __ = self.get_pages() page_meta = models.PageMeta.objects.create(extended_object=page1) page_meta.save() title_meta = models.TitleMeta.objects.create(extended_object=page1.get_title_obj('en')) title_meta.save() models.GenericMetaAttribute.objects.create( page=page_meta, attribute='custom', name='attr', value='foo' ) models.GenericMetaAttribute.objects.create( title=title_meta, attribute='custom', name='attr', value='bar' ) page1.reload() meta = get_page_meta(page1, 'en') self.assertEqual(meta.extra_custom_props, [('custom', 'attr', 'bar'), ('custom', 'attr', 'foo')]) meta = get_page_meta(page1, 'it') self.assertEqual(meta.extra_custom_props, [('custom', 'attr', 'foo')])
def test_page_meta_gplus(self): """ Tests the Google+ schema data """ page, __ = self.get_pages() page_meta = models.PageMeta.objects.create(extended_object=page) for key, val in self.page_data.items(): setattr(page_meta, key, val) for key, val in self.gplus_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual( meta.gplus_author, 'https://plus.google.com/{0}'.format( self.gplus_data['gplus_author'])) self.assertEqual(meta.gplus_type, self.gplus_data['gplus_type']) new_data = copy(self.gplus_data) new_data['gplus_author'] = 'https://plus.google.com/+SomeUser' for key, val in new_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual(meta.gplus_author, new_data['gplus_author']) new_data = copy(self.gplus_data) new_data['gplus_author'] = '/SomePage' for key, val in new_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual( meta.gplus_author, 'https://plus.google.com{0}'.format(new_data['gplus_author']))
def test_tags(self): tags1 = ('pagetag.1', 'pagetag.2') tags2 = ('titletag.1', 'titletag.2') try: from djangocms_page_tags.models import PageTags, TitleTags except ImportError: self.skipTest('djangocms_page_tags not installed') page1, page2 = self.get_pages() page_ext = PageTags.objects.create(extended_object=page1) page_ext.tags.add(*tags1) title_ext = TitleTags.objects.create( extended_object=page1.get_title_obj('en')) title_ext.tags.add(*tags2) title_ext = TitleTags.objects.create( extended_object=page2.get_title_obj('en')) title_ext.tags.add(*tags2) for page in (page1, page2): page_meta = models.PageMeta.objects.create(extended_object=page) for key, val in self.page_data.items(): setattr(page_meta, key, val) for key, val in self.og_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() for lang in self.languages: page1.publish(lang) page2.publish(lang) page1 = page1.get_public_object() page2 = page2.get_public_object() meta1 = get_page_meta(page1, 'en') meta2 = get_page_meta(page2, 'en') for tag in (tags2 + tags1): self.assertTrue(tag in meta1.tag) for tag in tags2: self.assertTrue(tag in meta2.tag)
def test_page_meta_no_meta(self): """ Tests the meta if no PageMeta is set """ meta_settings.GPLUS_AUTHOR = self.gplus_data['gplus_author'] page, __ = self.get_pages() meta = get_page_meta(page, 'en') self.assertEqual( meta.gplus_author, 'https://plus.google.com/{0}'.format(self.gplus_data['gplus_author']) ) self.assertEqual(meta.gplus_type, self.gplus_data['gplus_type']) self.assertEqual(meta.published_time, page.publication_date.isoformat()) self.assertEqual(meta.modified_time, page.changed_date.isoformat()) meta_settings.GPLUS_AUTHOR = ''
def test_tags(self): tags1 = ('pagetag.1', 'pagetag.2') tags2 = ('titletag.1', 'titletag.2') try: from djangocms_page_tags.models import PageTags, TitleTags except ImportError: self.skipTest('djangocms_page_tags not installed') page1, page2 = self.get_pages() page_ext = PageTags.objects.create(extended_object=page1) page_ext.tags.add(*tags1) title_ext = TitleTags.objects.create(extended_object=page1.get_title_obj('en')) title_ext.tags.add(*tags2) title_ext = TitleTags.objects.create(extended_object=page2.get_title_obj('en')) title_ext.tags.add(*tags2) for page in (page1, page2): page_meta = models.PageMeta.objects.create(extended_object=page) for key, val in self.page_data.items(): setattr(page_meta, key, val) for key, val in self.og_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() for lang in self.languages: page1.publish(lang) page2.publish(lang) page1 = page1.get_public_object() page2 = page2.get_public_object() meta1 = get_page_meta(page1, 'en') meta2 = get_page_meta(page2, 'en') for tag in (tags2 + tags1): self.assertTrue(tag in meta1.tag) for tag in tags2: self.assertTrue(tag in meta2.tag)
def test_page_meta_no_meta(self): """ Tests the meta if no PageMeta is set """ meta_settings.GPLUS_AUTHOR = self.gplus_data['gplus_author'] page, __ = self.get_pages() meta = get_page_meta(page, 'en') self.assertEqual( meta.gplus_author, 'https://plus.google.com/{0}'.format( self.gplus_data['gplus_author'])) self.assertEqual(meta.gplus_type, self.gplus_data['gplus_type']) self.assertEqual(meta.published_time, page.publication_date.isoformat()) self.assertEqual(meta.modified_time, page.changed_date.isoformat()) meta_settings.GPLUS_AUTHOR = ''
def test_page_meta_gplus(self): """ Tests the Google+ schema data """ page, page_2 = self.get_pages() page_meta = models.PageMeta.objects.create(extended_object=page) for key, val in self.page_data.items(): setattr(page_meta, key, val) for key, val in self.gplus_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual(meta.gplus_author, self.gplus_data['gplus_author']) self.assertEqual(meta.gplus_type, self.gplus_data['gplus_type'])
def test_page_meta_twitter(self): """ Tests the Twitter cards """ page, page_2 = self.get_pages() page_meta = models.PageMeta.objects.create(extended_object=page) for key, val in self.page_data.items(): setattr(page_meta, key, val) for key, val in self.twitter_data.items(): setattr(page_meta, key, val) page_meta.save() page.reload() meta = get_page_meta(page, 'en') self.assertEqual(meta.twitter_site, self.twitter_data['twitter_site']) self.assertEqual(meta.twitter_author, self.twitter_data['twitter_author']) self.assertEqual(meta.twitter_type, self.twitter_data['twitter_type']) self.assertEqual(meta.get_domain(), settings.META_SITE_DOMAIN)
def determine_meta_attributes(self, request, obj, language): page_meta = {} try: # If a Django CMS page meta_dict = get_page_meta(obj, language).__dict__ except AttributeError: try: # If a Model object that uses the ModelMeta-mixin meta_dict = obj.get_meta(request) except Exception: meta_dict = {} # <meta property="some" content="value"> --> {'some (property)': value} page_meta.update({ '{} ({})'.format(name, attr): value for attr, name, value in meta_dict.pop('extra_custom_props', []) }) for key, value in meta_dict.items(): if type(value) == list: page_meta[key] = ",".join(value) if type(value) == str and key not in META_FIELD_BLACKLIST: page_meta[key] = value return page_meta
def test_cache_cleanup_on_update_delete_meta(self): """ Meta caches are emptied when updating / deleting a meta """ page1, __ = self.get_pages() page_meta = models.PageMeta.objects.create(extended_object=page1) title_meta = models.TitleMeta.objects.create( extended_object=page1.get_title_obj("en")) # cache objects for language in page1.get_languages(): get_page_meta(page1, language) title_key = get_cache_key(title_meta.extended_object.page, title_meta.extended_object.language) self.assertTrue(cache.get(title_key)) # Title update check title_meta.description = "Something" title_meta.save() self.assertIsNone(cache.get(title_key)) # Refreshing cache get_page_meta(page1, title_meta.extended_object.language) self.assertTrue(cache.get(title_key)) # Page update check page_meta.og_author_url = "Something" page_meta.save() self.assertIsNone(cache.get(title_key)) # Refreshing cache get_page_meta(page1, title_meta.extended_object.language) self.assertTrue(cache.get(title_key)) # Check deleting objects title_meta.delete() self.assertIsNone(cache.get(title_key)) page_meta.delete() for language in page1.get_languages(): title_key = get_cache_key(page1, language) self.assertIsNone(cache.get(title_key))
def get_page_meta(page, language): """ Retrieves all the meta information for the page in the given language :param page: a Page instance :param lang: a language code :return: Meta instance :type: object """ from django.core.cache import cache from meta.views import Meta from .models import PageMeta, TitleMeta meta_key = get_cache_key(page, language) meta = cache.get(meta_key) if not meta: meta = Meta() title = page.get_title_obj(language) meta.title = page.get_title(language) try: titlemeta = title.titlemeta meta.description = titlemeta.description.strip() meta.keywords = titlemeta.keywords.strip().split(",") meta.locale = titlemeta.locale if titlemeta.image: meta.image = title.titlemeta.image.url except TitleMeta.DoesNotExist: # Skipping title-level metas pass try: pagemeta = page.pagemeta meta.object_type = pagemeta.og_type meta.og_type = pagemeta.og_type meta.og_app_id = pagemeta.og_app_id meta.og_profile_id = pagemeta.og_author_fbid meta.twitter_type = pagemeta.twitter_type meta.twitter_site = pagemeta.twitter_site meta.twitter_author = pagemeta.twitter_author meta.gplus_type = pagemeta.gplus_type meta.gplus_author = pagemeta.gplus_author meta.published_time = page.publication_date.isoformat() meta.modified_time = page.changed_date.isoformat() if page.publication_end_date: meta.expiration_time = page.publication_end_date.isoformat() if meta.og_type == 'article': meta.og_publisher = pagemeta.og_publisher if pagemeta.og_author_url: meta.og_author_url = pagemeta.og_author_url elif pagemeta.og_author: meta.og_author = { 'first_name': pagemeta.og_author.first_name, 'last_name': pagemeta.og_author.last_name } try: from djangocms_page_meta.utils import get_title_tags, get_page_meta tags = get_title_tags(page, language) tags += get_page_meta(page, language) meta.tag = ",".join(tags) except ImportError: # djangocms-page-meta not available pass if not meta.image and pagemeta.image: meta.image = pagemeta.image.url except PageMeta.DoesNotExist: # Skipping page-level metas pass if not meta.description and page.get_meta_description(language): meta.description = page.get_meta_description(language).strip() meta.url = page.get_absolute_url(language) return meta
def get_page_meta(page, language): """ Retrieves all the meta information for the page in the given language :param page: a Page instance :param lang: a language code :return: Meta instance :type: object """ from django.core.cache import cache from meta.views import Meta from .models import PageMeta, TitleMeta meta_key = get_cache_key(page, language) meta = cache.get(meta_key) if not meta: meta = Meta() title = page.get_title_obj(language) meta.title = page.get_page_title(language) if not meta.title: meta.title = page.get_title(language) try: titlemeta = title.titlemeta meta.description = titlemeta.description.strip() if titlemeta.keywords: meta.keywords = titlemeta.keywords.strip().split(",") meta.locale = titlemeta.locale meta.og_description = titlemeta.og_description.strip() meta.twitter_description = titlemeta.twitter_description.strip() meta.gplus_description = titlemeta.gplus_description.strip() if titlemeta.image: meta.image = title.titlemeta.image.url except TitleMeta.DoesNotExist: # Skipping title-level metas pass try: pagemeta = page.pagemeta meta.object_type = pagemeta.og_type meta.og_type = pagemeta.og_type meta.og_app_id = pagemeta.og_app_id meta.og_profile_id = pagemeta.og_author_fbid meta.twitter_type = pagemeta.twitter_type meta.twitter_site = pagemeta.twitter_site meta.twitter_author = pagemeta.twitter_author meta.gplus_type = pagemeta.gplus_type meta.gplus_author = pagemeta.gplus_author meta.published_time = page.publication_date.isoformat() meta.modified_time = page.changed_date.isoformat() if page.publication_end_date: meta.expiration_time = page.publication_end_date.isoformat() if meta.og_type == 'article': meta.og_publisher = pagemeta.og_publisher if pagemeta.og_author_url: meta.og_author_url = pagemeta.og_author_url try: from djangocms_page_meta.utils import get_title_tags, get_page_meta tags = get_title_tags(page, language) tags += get_page_meta(page, language) meta.tag = ",".join(tags) except ImportError: # djangocms-page-meta not available pass if not meta.image and pagemeta.image: meta.image = pagemeta.image.url except PageMeta.DoesNotExist: # Skipping page-level metas pass if not meta.description and page.get_meta_description(language): meta.description = page.get_meta_description(language).strip() meta.url = page.get_absolute_url(language) return meta