Пример #1
0
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

    try:
        meta_key = get_cache_key(page, language)
    except AttributeError:
        return None
    titlemeta = None
    meta = cache.get(meta_key)
    if not meta:
        meta = Meta()
        title = page.get_title_obj(language)
        meta.extra_custom_props = []

        meta.title = page.get_page_title(language)
        if not meta.title:
            meta.title = page.get_title(language)

        if title.meta_description:
            meta.description = title.meta_description.strip()
        try:
            titlemeta = title.titlemeta
            if titlemeta.description:
                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()
            if not meta.twitter_description:
                meta.twitter_description = meta.description
            if titlemeta.image:
                meta.image = title.titlemeta.image.canonical_url or title.titlemeta.image.url
                meta.schemaorg_image = meta.image
            meta.schemaorg_description = titlemeta.schemaorg_description.strip(
            )
            if not meta.schemaorg_description:
                meta.schemaorg_description = meta.description
            meta.schemaorg_name = titlemeta.schemaorg_name
            if not meta.schemaorg_name:
                meta.schemaorg_name = meta.title
            for item in titlemeta.extra.all():
                attribute = item.attribute
                if not attribute:
                    attribute = item.DEFAULT_ATTRIBUTE
                meta.extra_custom_props.append(
                    (attribute, item.name, item.value))
        except (TitleMeta.DoesNotExist, AttributeError):
            # Skipping title-level metas
            if meta.description:
                meta.og_description = meta.description
                meta.schemaorg_description = meta.description
                meta.twitter_description = meta.description
        defaults = {
            "object_type":
            meta_settings.FB_TYPE,
            "og_type":
            meta_settings.FB_TYPE,
            "og_app_id":
            meta_settings.FB_APPID,
            "fb_pages":
            meta_settings.FB_PAGES,
            "og_profile_id":
            meta_settings.FB_PROFILE_ID,
            "og_publisher":
            meta_settings.FB_PUBLISHER,
            "og_author_url":
            meta_settings.FB_AUTHOR_URL,
            "twitter_type":
            meta_settings.TWITTER_TYPE,
            "twitter_site":
            meta_settings.TWITTER_SITE,
            "twitter_author":
            meta_settings.TWITTER_AUTHOR,
            "schemaorg_type":
            meta_settings.SCHEMAORG_TYPE,
            "schemaorg_datePublished":
            page.publication_date.isoformat()
            if page.publication_date else None,
            "schemaorg_dateModified":
            page.changed_date.isoformat() if page.changed_date else None,
        }
        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.fb_pages = pagemeta.fb_pages
            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.schemaorg_type = pagemeta.schemaorg_type
            if page.publication_date:
                meta.published_time = page.publication_date.isoformat()
            if page.changed_date:
                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
                meta.og_author_url = pagemeta.og_author_url
                try:
                    from djangocms_page_tags.utils import get_page_tags, get_title_tags

                    tags = list(get_title_tags(page, language))
                    tags += list(get_page_tags(page))
                    meta.tag = ",".join([tag.name for tag in tags])
                except ImportError:
                    # djangocms-page-tags not available
                    pass
            if not meta.image and pagemeta.image:
                meta.image = pagemeta.image.canonical_url or pagemeta.image.url
            # added page level fields
            if pagemeta.title:
                meta.title = pagemeta.title
            # we can override the meta description defined on the page
            # otherwise we only update if there isn't a title description already
            if pagemeta.description and (not titlemeta or
                                         (titlemeta
                                          and not titlemeta.description)):
                meta.description = pagemeta.description
            if pagemeta.og_description and (
                    not getattr(meta, 'og_description', '') or
                (titlemeta and not titlemeta.og_description)):
                meta.og_description = pagemeta.og_description
            if pagemeta.twitter_description and (
                    not getattr(meta, 'twitter_description', '') or
                (titlemeta and not titlemeta.twitter_description)):
                meta.twitter_description = pagemeta.twitter_description
            if pagemeta.gplus_description and (
                    not getattr(meta, 'gplus_description', '') or
                (titlemeta and not titlemeta.gplus_description)):
                meta.gplus_description = pagemeta.gplus_description
            if not meta.keywords and pagemeta.keywords:
                meta.keywords = pagemeta.keywords.strip().split(',')

            for item in pagemeta.extra.all():
                attribute = item.attribute
                if not attribute:
                    attribute = item.DEFAULT_ATTRIBUTE
                meta.extra_custom_props.append(
                    (attribute, item.name, item.value))
        except PageMeta.DoesNotExist:
            pass

        # override social descriptions with the general
        # descriptions if nothing more specific was found
        if meta.description:
            if not getattr(meta, 'og_description', ''):
                meta.og_description = meta.description
            if not getattr(meta, 'twitter_description', ''):
                meta.twitter_description = meta.description
            if not getattr(meta, 'gplus_description', ''):
                meta.gplus_description = meta.description

        for attr, val in defaults.items():
            if not getattr(meta, attr, "") and val:
                setattr(meta, attr, val)
        meta.url = page.get_absolute_url(language)
        meta.schemaorg_url = meta.url
        cache.set(meta_key, meta)
    return meta
Пример #2
0
 def test_set_namespaces(self):
     settings.OG_NAMESPACES = ['foo', 'bar']
     m = Meta()
     self.assertEqual(m.custom_namespace[0], 'foo')
     self.assertEqual(m.custom_namespace[1], 'bar')
     settings.OG_NAMESPACES = None
Пример #3
0
 def test_set_image_with_full_url(self):
     m = Meta(image='http://meta.example.com/image.gif')
     self.assertEqual(m.image, 'http://meta.example.com/image.gif')
Пример #4
0
 def test_set_image_with_image_url(self):
     meta.settings.SITE_PROTOCOL = 'https'
     meta.settings.SITE_DOMAIN = 'foo.com'
     meta.settings.IMAGE_URL = '/thumb/'
     m = Meta(image='img/image.gif')
     self.assertEqual(m.image, 'https://foo.com/thumb/img/image.gif')
Пример #5
0
 def test_get_full_url_with_domain_and_protocol(self):
     settings.SITE_PROTOCOL = 'https'
     settings.SITE_DOMAIN = 'foo.com'
     m = Meta()
     self.assertEqual(m.get_full_url('foo/bar'), 'https://foo.com/foo/bar')
Пример #6
0
 def test_get_full_url_with_absolute_path(self):
     settings.SITE_PROTOCOL = 'https'
     settings.SITE_DOMAIN = 'foo.com'
     m = Meta()
     self.assertEqual(m.get_full_url('/foo/bar'), 'https://foo.com/foo/bar')
Пример #7
0
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

    try:
        meta_key = get_cache_key(page, language)
    except AttributeError:
        return None
    gplus_server = 'https://plus.google.com'
    meta = cache.get(meta_key)
    if not meta:
        meta = Meta()
        title = page.get_title_obj(language)
        meta.extra_custom_props = []

        meta.title = page.get_page_title(language)
        if not meta.title:
            meta.title = page.get_title(language)

        if title.meta_description:
            meta.description = title.meta_description.strip()
        try:
            titlemeta = title.titlemeta
            if titlemeta.description:
                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()
            if not meta.og_description:
                meta.og_description = meta.description
            meta.twitter_description = titlemeta.twitter_description.strip()
            if not meta.twitter_description:
                meta.twitter_description = meta.description
            meta.gplus_description = titlemeta.gplus_description.strip()
            if not meta.gplus_description:
                meta.gplus_description = meta.description
            if titlemeta.image:
                meta.image = title.titlemeta.image.canonical_url or title.titlemeta.image.url
            for item in titlemeta.extra.all():
                attribute = item.attribute
                if not attribute:
                    attribute = item.DEFAULT_ATTRIBUTE
                meta.extra_custom_props.append(
                    (attribute, item.name, item.value))
        except (TitleMeta.DoesNotExist, AttributeError):
            # Skipping title-level metas
            if meta.description:
                meta.og_description = meta.description
                meta.twitter_description = meta.description
                meta.gplus_description = meta.description
        defaults = {
            'object_type': meta_settings.FB_TYPE,
            'og_type': meta_settings.FB_TYPE,
            'og_app_id': meta_settings.FB_APPID,
            'fb_pages': meta_settings.FB_PAGES,
            'og_profile_id': meta_settings.FB_PROFILE_ID,
            'og_publisher': meta_settings.FB_PUBLISHER,
            'og_author_url': meta_settings.FB_AUTHOR_URL,
            'twitter_type': meta_settings.TWITTER_TYPE,
            'twitter_site': meta_settings.TWITTER_SITE,
            'twitter_author': meta_settings.TWITTER_AUTHOR,
            'gplus_type': meta_settings.GPLUS_TYPE,
            'gplus_author': meta_settings.GPLUS_AUTHOR,
            'extra_props': meta_settings.EXTRA_PROPS,
            'extra_custom_props': meta_settings.EXTRA_CUSTOM_PROPS
        }
        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.fb_pages = pagemeta.fb_pages
            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
            if meta.og_type == 'article':
                meta.og_publisher = pagemeta.og_publisher
                meta.og_author_url = pagemeta.og_author_url
                try:
                    from djangocms_page_tags.utils import get_title_tags, get_page_tags
                    tags = list(get_title_tags(page, language))
                    tags += list(get_page_tags(page))
                    meta.tag = ','.join([tag.name for tag in tags])
                except ImportError:
                    # djangocms-page-tags not available
                    pass
            if not meta.image and pagemeta.image:
                meta.image = pagemeta.image.canonical_url or pagemeta.image.url
            for item in pagemeta.extra.all():
                attribute = item.attribute
                if not attribute:
                    attribute = item.DEFAULT_ATTRIBUTE
                meta.extra_custom_props.append(
                    (attribute, item.name, item.value))
        except PageMeta.DoesNotExist:
            pass
        if meta.gplus_author and not meta.gplus_author.startswith('http'):
            if not meta.gplus_author.startswith('/'):
                meta.gplus_author = '{0}/{1}'.format(gplus_server,
                                                     meta.gplus_author)
            else:
                meta.gplus_author = '{0}{1}'.format(gplus_server,
                                                    meta.gplus_author)
        if page.publication_date:
            meta.published_time = page.publication_date.isoformat()
        if page.changed_date:
            meta.modified_time = page.changed_date.isoformat()
        if page.publication_end_date:
            meta.expiration_time = page.publication_end_date.isoformat()
        for attr, val in defaults.items():
            if not getattr(meta, attr, '') and val:
                setattr(meta, attr, val)
        meta.url = page.get_absolute_url(language)
    return meta
Пример #8
0
 def test_get_full_url_without_protocol_without_schema_will_raise(self):
     m = Meta()
     with self.assertRaises(ImproperlyConfigured):
         m.get_full_url('//foo.com/foo/bar')
Пример #9
0
 def test_set_keywords_with_defaults(self):
     meta.settings.DEFAULT_KEYWORDS = ['foo', 'bar']
     m = Meta()
     self.assertEqual(m.keywords[0], 'foo')
     self.assertEqual(m.keywords[1], 'bar')
Пример #10
0
}

common_meta_tags_extra_props = [
    ('http-equiv', 'Content-Type', 'text/html; charset=UTF-8'),
    ('name', 'robots', 'all'),
    get_prop('fb:profile_id', settings.SEO_FACEBOOK_ID + '/about/'),
    get_prop('og:locale', settings.SEO_LOCALE),
    get_prop('og:see_also', settings.SEO_INSTAGRAM_ID),
    get_prop('og:see_also', settings.SEO_LINKEDIN_ID),
    get_prop('og:see_also', settings.SEO_FACEBOOK_ID),
    get_prop('og:see_also', settings.SEO_TWITTER_ID),
]

home_meta_tags = Meta(
    **common_meta_tags,
    extra_custom_props=common_meta_tags_extra_props + [],
    title=settings.SEO_SITE_TITLE,
    description=settings.SEO_SITE_MAIN_DESCRIPTION,
)

SEO_PAGE_TITLE = 'SEO Toronto | SEO Company Toronto | SEO Services Toronto'
SEO_PAGE_DESCRIPTION = 'We are a leading SEO company in Toronto helping brands increase their ' \
                       'organic search engine traffic & revenue. Start your SEO campaign today!'
seo_meta_tags = Meta(
    **common_meta_tags,
    extra_custom_props=common_meta_tags_extra_props + [
        get_prop('og:url', reverse_lazy('seo')),
    ],
    title=SEO_PAGE_TITLE,
    description=SEO_PAGE_DESCRIPTION,
)
Пример #11
0
 def test_set_keywords_no_duplicate(self):
     m = Meta(keywords=['foo', 'foo', 'foo'])
     self.assertEqual(m.keywords[0], 'foo')
     self.assertEqual(len(m.keywords), 1)
Пример #12
0
 def test_set_keywords_with_include(self):
     meta.settings.INCLUDE_KEYWORDS = ['baz']
     m = Meta(keywords=['foo', 'bar'])
     self.assertEqual(m.keywords[0], 'foo')
     self.assertEqual(m.keywords[1], 'bar')
     self.assertEqual(m.keywords[2], 'baz')
Пример #13
0
 def test_set_keywords(self):
     m = Meta(keywords=['foo', 'bar'])
     self.assertEqual(m.keywords[0], 'foo')
     self.assertEqual(m.keywords[1], 'bar')
Пример #14
0
 def test_get_full_url_with_None(self):
     m = Meta()
     self.assertEqual(m.get_full_url(None), None)
Пример #15
0
 def test_meta_namespaces_default(self):
     context = {'meta': Meta()}
     result = meta_namespaces(context)
     expected = ' prefix="og: http://ogp.me/ns#"'
     self.assertEqual(result, expected)
Пример #16
0
 def test_get_full_url_with_full_url(self):
     m = Meta()
     self.assertEqual(m.get_full_url('http://example.com/foo'),
                      'http://example.com/foo')
Пример #17
0
 def test_meta_namespaces_facebook(self):
     context = {'meta': Meta(use_facebook=True)}
     result = meta_namespaces(context)
     expected = ' prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#"'
     self.assertEqual(result, expected)
Пример #18
0
 def test_get_full_url_without_domain_will_raise(self):
     settings.SITE_PROTOCOL = 'http'
     m = Meta()
     with self.assertRaises(ImproperlyConfigured):
         m.get_full_url('foo/bar')
Пример #19
0
 def test_meta_namespaces_custom(self):
     context = {'meta': Meta(custom_namespace='my-website')}
     result = meta_namespaces(context)
     expected = ' prefix="og: http://ogp.me/ns# my-website: http://ogp.me/ns/my-website#"'
     self.assertEqual(result, expected)
Пример #20
0
 def test_get_full_url_without_schema(self):
     settings.SITE_PROTOCOL = 'https'
     m = Meta()
     self.assertEqual(m.get_full_url('//foo.com/foo/bar'),
                      'https://foo.com/foo/bar')
Пример #21
0
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

    try:
        meta_key = get_cache_key(page, language)
    except AttributeError:
        return None
    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)

        if title.meta_description:
            meta.description = title.meta_description.strip()
        try:
            titlemeta = title.titlemeta
            if titlemeta.description:
                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()
            if not meta.og_description:
                meta.og_description = meta.description
            meta.twitter_description = titlemeta.twitter_description.strip()
            if not meta.twitter_description:
                meta.twitter_description = meta.description
            meta.gplus_description = titlemeta.gplus_description.strip()
            if not meta.gplus_description:
                meta.gplus_description = meta.description
            if titlemeta.image:
                meta.image = title.titlemeta.image.url
        except TitleMeta.DoesNotExist:
            if meta.description:
                meta.og_description = meta.description
                meta.twitter_description = meta.description
                meta.gplus_description = meta.description
            # 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
            if page.publication_date:
                meta.published_time = page.publication_date.isoformat()
            if page.changed_date:
                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_tags.utils import get_title_tags, get_page_tags
                    tags = list(get_title_tags(page, language))
                    tags += list(get_page_tags(page))
                    meta.tag = ','.join([tag.name for tag in tags])
                except ImportError:
                    # djangocms-page-tags not available
                    pass
            if not meta.image and pagemeta.image:
                meta.image = pagemeta.image.url
        except PageMeta.DoesNotExist:
            # Skipping page-level metas
            pass
        meta.url = page.get_absolute_url(language)
    return meta
Пример #22
0
 def test_set_url(self):
     settings.SITE_PROTOCOL = 'https'
     settings.SITE_DOMAIN = 'foo.com'
     m = Meta(url='foo/bar')
     self.assertEqual(m.url, 'https://foo.com/foo/bar')
Пример #23
0
from meta.views import Meta
from taggit.models import Tag

from ideas.forms import AnonymousIdeaCreateForm, NonAnonymousIdeaCreateForm
from ideas.models import Idea
from ideas.utils import process_idea_form
from subscribers.models import Subscriber
from utils import validators

global paginate_by
paginate_by = 15

global meta_home
meta_home = Meta(
    title='IdeaFare | Let us make the world a better place!',
    description=
    'Read, share and discuss about the ideas that you think can change the world.',
    keywords=['idea', 'share', 'innovate', 'change', 'discuss', 'curiousity'])


@require_http_methods(['POST'])
def subscribe(request):
    """
    adds emails to the model Subscribers after verifying legit emails only on POST requests.
    Returns Jsonresponse with properties response and status.
    """
    data = {'msg': '', 'email': '', 'status': -1}
    if request.method == 'POST' and request.is_ajax():
        email = request.POST.get('email', None)
        if email:
            data['email'] = email
Пример #24
0
 def test_set_image_with_relative_path(self):
     settings.SITE_PROTOCOL = 'https'
     settings.SITE_DOMAIN = 'foo.com'
     m = Meta(image='img/image.gif')
     self.assertEqual(m.image, 'https://foo.com/static/img/image.gif')
Пример #25
0
 def test_set_image_with_absolute_path(self):
     meta.settings.SITE_PROTOCOL = 'https'
     meta.settings.SITE_DOMAIN = 'foo.com'
     m = Meta(image='/img/image.gif')
     self.assertEqual(m.image, 'https://foo.com/img/image.gif')