Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Add Facebook image and description #258

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions bulbs/content/migrations/0013_auto_20160725_1304.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import djbetty.fields


class Migration(migrations.Migration):

dependencies = [
('content', '0012_auto_20160615_1605'),
]

operations = [
migrations.AddField(
model_name='content',
name='_facebook_description',
field=models.TextField(max_length=1024, null=True, db_column='facebook_description', blank=True),
),
migrations.AddField(
model_name='content',
name='facebook_image',
field=djbetty.fields.ImageField(default=None, null=True, blank=True),
),
]
18 changes: 18 additions & 0 deletions bulbs/content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ class Content(PolymorphicModel, Indexable):
template_choice = models.IntegerField(default=0, choices=TEMPLATE_CHOICES)
# Facebook Instant Article ID
instant_article_id = models.BigIntegerField(blank=True, null=True, default=None)
_facebook_description = models.TextField(
max_length=1024,
blank=True,
null=True,
db_column="facebook_description"
)
facebook_image = ImageField(null=True, blank=True)

# custom ES manager
search_objects = ContentManager()
Expand All @@ -238,6 +245,7 @@ class Mapping:
slug = field.String(index="not_analyzed")
status = field.String(index="not_analyzed")
thumbnail_override = ElasticsearchImageField()
facebook_image = ElasticsearchImageField()

def __unicode__(self):
"""unicode friendly name
Expand Down Expand Up @@ -288,6 +296,16 @@ def first_image(self):
# no non-none images, return None
return None

@property
def facebook_description(self):
if self._facebook_description:
return self._facebook_description
return self.description

@facebook_description.setter
def facebook_description(self, value):
self._facebook_description = value

def get_absolute_url(self):
"""produces a url to link directly to this instance, given the URL config

Expand Down
7 changes: 7 additions & 0 deletions bulbs/content/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,15 @@ class ContentSerializer(serializers.ModelSerializer):
thumbnail = ImageFieldSerializer(allow_null=True, read_only=True)
first_image = ImageFieldSerializer(allow_null=True, read_only=True)
thumbnail_override = ImageFieldSerializer(allow_null=True, required=False)
facebook_image = ImageFieldSerializer(allow_null=True, required=False)
absolute_url = serializers.ReadOnlyField(source="get_absolute_url")
status = serializers.ReadOnlyField(source="get_status")
facebook_description = serializers.CharField(
source="_facebook_description",
allow_null=True,
required=False
)

template_type = serializers.SlugRelatedField(
slug_field="slug",
allow_null=True,
Expand Down
4 changes: 4 additions & 0 deletions bulbs/super_features/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class BaseSuperFeatureSerializer(ContentSerializer):
class Meta:
model = BaseSuperFeature

def to_representation(self, obj):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just adding this for documentation purposes, since this serializer isn't being used

self.superfeature_type = getattr(obj, 'superfeature_type')
return super(BaseSuperFeatureSerializer, self).to_representation(obj)


class BaseSuperFeaturePartialSerializer(ContentSerializer):

Expand Down
2 changes: 2 additions & 0 deletions tests/api/test_content_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def test_create_article(self):
"description": "Testing out things with an article.",
"foo": "Fighters",
"feature_type": "Some Super Long String Probably",
"facebook_description": "this is the facebook description",
"facebook_image": {"id": 1},
"authors": [{
"id": author.id,
"username": author.username,
Expand Down
15 changes: 15 additions & 0 deletions tests/content/test_content_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ def test_get_targeting(self):
targeting = obj.get_targeting()
self.assertEqual(obj.published.isoformat(), targeting.get("dfp_publishdate"))

def test_facebook_description_fallback(self):
obj = TestContentObj.objects.create(**self.default_data)
obj.description = "This is a description"
obj.save()
TestContentObj.search_objects.refresh()

self.assertEqual(obj.facebook_description, obj.description)

obj.facebook_description = "This is the facebook description"
obj.save()
TestContentObj.search_objects.refresh()

self.assertNotEqual(obj.facebook_description, obj.description)
self.assertEqual(obj.facebook_description, "This is the facebook description")


class SerializerTestCase(BaseIndexableTestCase):
def test_content_status(self):
Expand Down