This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added block type 'SocialFeedBlock'. closes #4
- Loading branch information
Showing
7 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
test_blocks | ||
---------------------------------- | ||
Tests for `wagtailsocialfeed.blocks`. | ||
""" | ||
from __future__ import unicode_literals | ||
|
||
from django.test import RequestFactory, TestCase | ||
|
||
from bs4 import BeautifulSoup | ||
from wagtailsocialfeed.blocks import SocialFeedBlock | ||
|
||
from . import feed_response | ||
from .factories import SocialFeedConfigurationFactory, SocialFeedPageFactory | ||
|
||
|
||
class TestSocialFeedBlock(TestCase): | ||
def setUp(self): | ||
self.factory = RequestFactory() | ||
|
||
self.feedconfig = SocialFeedConfigurationFactory( | ||
source='twitter', username='wagtailcms') | ||
self.page = SocialFeedPageFactory.create(feedconfig=self.feedconfig) | ||
|
||
@feed_response('twitter') | ||
def test_render(self, tweets): | ||
block = SocialFeedBlock() | ||
value = { | ||
'feedconfig': self.feedconfig, | ||
'limit': 3 | ||
} | ||
context = block.get_context(value) | ||
html = block.render(value) | ||
|
||
self.assertEqual(len(context['feed']), 3) | ||
self.assertIn("Please try out 1.6 on your staging sites ahead of the", | ||
html) | ||
self.assertIn("Congratulations to Wagtail users @britishswimming!", | ||
html) | ||
self.assertIn("When are *you* going to write a blog post for us?", | ||
html) | ||
|
||
def test_to_python(self): | ||
block = SocialFeedBlock() | ||
value = { | ||
'feedconfig': self.feedconfig.pk, | ||
'limit': 3 | ||
} | ||
result = block.to_python(value) | ||
self.assertEqual(result['feedconfig'], self.feedconfig) | ||
self.assertEqual(result['limit'], 3) | ||
|
||
def test_render_form(self): | ||
block = SocialFeedBlock() | ||
value = { | ||
'feedconfig': self.feedconfig, | ||
'limit': 3 | ||
} | ||
html = block.render_form(value, prefix='test') | ||
soup = BeautifulSoup(html, 'html.parser') | ||
|
||
self.assertEqual( | ||
soup.find(id='test-feedconfig').find_all('option')[1].text, | ||
'twitter (@wagtailcms)') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django import forms | ||
|
||
from wagtail.wagtailcore import blocks | ||
|
||
from .models import SocialFeedConfiguration | ||
from .utils import get_feed | ||
|
||
|
||
class FeedChooserBlock(blocks.ChooserBlock): | ||
target_model = SocialFeedConfiguration | ||
widget = forms.Select | ||
|
||
def value_for_form(self, value): | ||
return value.pk | ||
|
||
def to_python(self, value): | ||
return self.target_model.objects.get(pk=value) | ||
|
||
|
||
class SocialFeedBlock(blocks.StructBlock): | ||
feedconfig = FeedChooserBlock() | ||
limit = blocks.IntegerBlock(required=False, min_value=0) | ||
|
||
class Meta: | ||
icon = 'icon icon-fa-rss' | ||
template = 'wagtailsocialfeed/social_feed_block.html' | ||
|
||
def get_context(self, value): | ||
context = super(SocialFeedBlock, self).get_context(value) | ||
|
||
feedconfig = value['feedconfig'] | ||
context['feed'] = get_feed(feedconfig, limit=value['limit']) | ||
|
||
return context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
wagtailsocialfeed/templates/wagtailsocialfeed/social_feed_block.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% for item in feed %} | ||
{% if item.moderated %} | ||
{% include 'wagtailsocialfeed/includes/feed_item.html' with item=item.get_content %} | ||
{% else %} | ||
{% include 'wagtailsocialfeed/includes/feed_item.html' %} | ||
{% endif %} | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from __future__ import unicode_literals | ||
|
||
from .feed.factory import FeedFactory | ||
|
||
|
||
def get_feed(feedconfig, limit=0): | ||
if feedconfig.moderated: | ||
qs = feedconfig.moderated_items.all() | ||
if limit: | ||
return qs[:limit] | ||
return qs | ||
|
||
stream = FeedFactory.create(feedconfig.source) | ||
return stream.get_feed(config=feedconfig, limit=limit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters