Skip to content

Commit

Permalink
Normalize twitter_card / twitter_type attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed Jul 7, 2020
1 parent f1b0e66 commit c441792
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
34 changes: 30 additions & 4 deletions meta/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import warnings

from django.core.exceptions import ImproperlyConfigured

from . import settings
Expand Down Expand Up @@ -33,7 +35,8 @@ def __init__(self, **kwargs):
self.site_name = kwargs.get('site_name', settings.SITE_NAME)
self.twitter_site = kwargs.get('twitter_site')
self.twitter_creator = kwargs.get('twitter_creator')
self.twitter_card = kwargs.get('twitter_card')
self.twitter_type = kwargs.get('twitter_type', kwargs.get('twitter_card', settings.TWITTER_TYPE))
self.twitter_card = self.twitter_type
self.facebook_app_id = kwargs.get('facebook_app_id')
self.locale = kwargs.get('locale')
self.use_og = kwargs.get('use_og', settings.USE_OG_PROPERTIES)
Expand Down Expand Up @@ -146,7 +149,7 @@ class MetadataMixin(object):
site_name = None
twitter_site = None
twitter_creator = None
twitter_card = None
twitter_type = None
facebook_app_id = None
locale = None
use_sites = False
Expand Down Expand Up @@ -216,8 +219,31 @@ def get_meta_twitter_site(self, context=None):
def get_meta_twitter_creator(self, context=None):
return self.twitter_creator

@property
def twitter_card(self):
warnings.warn(
"twitter_card attribute will be removed in version 3.0",
PendingDeprecationWarning
)
return self.twitter_type

@twitter_card.setter
def twitter_card(self, value):
warnings.warn(
"twitter_card attribute will be removed in version 3.0",
PendingDeprecationWarning
)
self.twitter_type = value

def get_meta_twitter_card(self, context=None):
return self.twitter_card
warnings.warn(
"get_meta_twitter_card attribute will be removed in version 3.0",
PendingDeprecationWarning
)
return self.twitter_type

def get_meta_twitter_type(self, context=None):
return self.twitter_type

def get_meta_facebook_app_id(self, context=None):
return self.facebook_app_id
Expand Down Expand Up @@ -254,7 +280,7 @@ def get_meta(self, context=None):
site_name=self.get_meta_site_name(context=context),
twitter_site=self.get_meta_twitter_site(context=context),
twitter_creator=self.get_meta_twitter_creator(context=context),
twitter_card=self.get_meta_twitter_card(context=context),
twitter_type=self.get_meta_twitter_type(context=context),
locale=self.get_meta_locale(context=context),
facebook_app_id=self.get_meta_facebook_app_id(context=context),
gplus_type=self.get_meta_gplus_type(context=context),
Expand Down
1 change: 0 additions & 1 deletion tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def test_defaults(self):
self.assertEqual(m.site_name, None)
self.assertEqual(m.twitter_site, None)
self.assertEqual(m.twitter_creator, None)
self.assertEqual(m.twitter_card, None)
self.assertEqual(m.locale, None)
self.assertEqual(m.facebook_app_id, None)
self.assertEqual(m.use_og, False)
Expand Down
50 changes: 39 additions & 11 deletions tests/test_metadata_mixin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import warnings

from django.test import TestCase

from meta import settings
Expand Down Expand Up @@ -239,17 +241,43 @@ def test_get_meta_twitter_creator(self):
)

def test_get_meta_twitter_card(self):
m = MetadataMixin()
self.assertEqual(
m.get_meta_twitter_card(),
None
)

m.twitter_card = 'summary'
self.assertEqual(
m.get_meta_twitter_card(),
'summary'
)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
m = MetadataMixin()
self.assertEqual(
m.get_meta_twitter_card(),
None
)
assert len(w) == 1
assert issubclass(w[-1].category, PendingDeprecationWarning)

m.twitter_card = 'summary'
assert len(w) == 2
assert issubclass(w[-1].category, PendingDeprecationWarning)

self.assertEqual(
m.get_meta_twitter_card(),
'summary'
)
assert len(w) == 3
assert issubclass(w[-1].category, PendingDeprecationWarning)

def test_get_meta_twitter_type(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
m = MetadataMixin()
self.assertEqual(
m.get_meta_twitter_type(),
None
)
assert len(w) == 0

m.twitter_type = 'summary'
self.assertEqual(
m.get_meta_twitter_type(),
'summary'
)
assert len(w) == 0

def test_get_meta_facebook_app_id(self):
m = MetadataMixin()
Expand Down

0 comments on commit c441792

Please sign in to comment.