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

Commit

Permalink
fixed Python 3 compatibility and fixed lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tleguijt committed Aug 31, 2016
1 parent 7ebdab1 commit 394a97d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 37 deletions.
3 changes: 2 additions & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.encoding import force_text

from bs4 import BeautifulSoup
from wagtailsocialfeed.models import ModeratedItem
Expand Down Expand Up @@ -127,7 +128,7 @@ def test_post(self):
# Test with missing data
resp = self.client.post(self.url)
self.assertEqual(resp.status_code, 400)
json_resp = json.loads(resp.content)
json_resp = json.loads(force_text(resp.content))
self.assertEqual(
json_resp['message'],
'The original social feed post was not found in the POST data')
Expand Down
13 changes: 10 additions & 3 deletions wagtailsocialfeed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
from .utils.feed.factory import FeedFactory
from .managers import ModeratedItemManager


@python_2_unicode_compatible
class SocialFeedConfiguration(models.Model):
FEED_CHOICES = (
('twitter', _('Twitter')),
('instagram', _('Instagram')),
)

source = models.CharField(_('Feed source'), max_length=100, choices=FEED_CHOICES, blank=False)
username = models.CharField(_('User to track'), max_length=255, blank=False)
source = models.CharField(_('Feed source'),
max_length=100,
choices=FEED_CHOICES,
blank=False)
username = models.CharField(_('User to track'),
max_length=255,
blank=False)
moderated = models.BooleanField(default=False)

def __str__(self):
Expand Down Expand Up @@ -61,7 +67,8 @@ class SocialFeedPage(Page):
]

def get_context(self, request, *args, **kwargs):
context = super(SocialFeedPage, self).get_context(request, *args, **kwargs)
context = super(SocialFeedPage,
self).get_context(request, *args, **kwargs)

if self.feedconfig.moderated:
feed = self.feedconfig.moderated_items.all()
Expand Down
12 changes: 9 additions & 3 deletions wagtailsocialfeed/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
from . import views

urlpatterns = [
url(r'^moderate/(?P<pk>\d+)/$', views.ModerateView.as_view(), name='moderate'),
url(r'^moderate/(?P<pk>\d+)/(?P<post_id>.+)/allow/$', views.ModerateAllowView.as_view(), name='allow'),
url(r'^moderate/(?P<pk>\d+)/(?P<post_id>.+)/remove/$', views.ModerateRemoveView.as_view(), name='remove'),
url(r'^moderate/(?P<pk>\d+)/$',
views.ModerateView.as_view(),
name='moderate'),
url(r'^moderate/(?P<pk>\d+)/(?P<post_id>.+)/allow/$',
views.ModerateAllowView.as_view(),
name='allow'),
url(r'^moderate/(?P<pk>\d+)/(?P<post_id>.+)/remove/$',
views.ModerateRemoveView.as_view(),
name='remove'),
]
5 changes: 3 additions & 2 deletions wagtailsocialfeed/utils/feed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

logger = logging.getLogger('wagtailsocialfeed')


class FeedError(Exception):
pass

Expand All @@ -22,10 +23,10 @@ def get_feed(self, config, *args, **kwargs):
logger.debug("Fetching data online")
data = self.fetch_online(config=config, *args, **kwargs)
logger.debug("Storing data in cache ({})".format(cache_key))
cache.set(cache_key, data, get_socialfeed_setting('CACHE_DURATION'))
cache.set(cache_key, data,
get_socialfeed_setting('CACHE_DURATION'))
return data


def fetch_online(self, *args, **kwargs):
raise NotImplementedError(
"fetch_online() is not implemented yet by {}".format(
Expand Down
3 changes: 2 additions & 1 deletion wagtailsocialfeed/utils/feed/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ def create(cls, source):
return TwitterFeed()
if source == 'instagram':
return InstagramFeed()
raise NotImplementedError("Feed class for type '{}' not available".format(source))
raise NotImplementedError(
"Feed class for type '{}' not available".format(source))
2 changes: 1 addition & 1 deletion wagtailsocialfeed/utils/feed/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ def fetch_online(self, config, limit=None):
except KeyError as e:
raise FeedError("No items could be found in the response")

return map(prepare_item, items)
return list(map(prepare_item, items))

raise FeedError(resp.reason)
27 changes: 14 additions & 13 deletions wagtailsocialfeed/utils/feed/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@


def process_images(media_dict):
images = {}
if not media_dict:
return images
base_url = media_dict[0]['media_url_https']

# TODO: see if we can provide the width and height attributes as well
images['small'] = dict(url=base_url + ":small")
images['thumb'] = dict(url=base_url + ":thumb")
images['medium'] = dict(url=base_url + ":medium")
images['large'] = dict(url=base_url + ":large")
images = {}
if not media_dict:
return images
base_url = media_dict[0]['media_url_https']

# TODO: see if we can provide the width and height attributes as well
images['small'] = dict(url=base_url + ":small")
images['thumb'] = dict(url=base_url + ":thumb")
images['medium'] = dict(url=base_url + ":medium")
images['large'] = dict(url=base_url + ":large")
return images


def prepare_item(item):
Expand All @@ -30,8 +30,9 @@ def prepare_item(item):
item['image'] = image

# Use the dateutil parser because on some platforms
# python's own strptime doesn't support the %z directive
item['date'] = dateparser.parse(item.get('created_at')) # '%a %b %d %H:%M:%S %z %Y'
# python's own strptime doesn't support the %z directive.
# Format: '%a %b %d %H:%M:%S %z %Y'
item['date'] = dateparser.parse(item.get('created_at'))

return item

Expand Down Expand Up @@ -59,4 +60,4 @@ def fetch_online(self, config, limit=None):
trim_user=True,
contributor_details=False,
include_rts=False)
return map(prepare_item, tweets)
return list(map(prepare_item, tweets))
2 changes: 1 addition & 1 deletion wagtailsocialfeed/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json

from django.http import JsonResponse
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View
from django.views.generic.detail import DetailView
Expand Down
28 changes: 16 additions & 12 deletions wagtailsocialfeed/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
from __future__ import unicode_literals

from django.conf.urls import url, include
from django.conf.urls import include, url
from django.core.urlresolvers import reverse
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail.contrib.modeladmin.menus import SubMenu

from wagtail.wagtailadmin.menu import Menu, MenuItem, SubmenuMenuItem
from wagtail.contrib.modeladmin.menus import SubMenu
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail.wagtailadmin.menu import MenuItem, SubmenuMenuItem
from wagtail.wagtailcore import hooks

from . import urls
from .models import SocialFeedConfiguration

from . import urls

class SocialFeedConfigurationAdmin(ModelAdmin):
model = SocialFeedConfiguration
menu_label = 'Social feeds' # ditch this to use verbose_name_plural from model
menu_icon = 'icon icon-fa-rss' # change as required
menu_order = 400 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = True # or True to add your model to the Settings sub-menu
menu_label = 'Social feeds' # TODO ditch this to use verbose_name_plural from model # NOQA
menu_icon = 'icon icon-fa-rss'
menu_order = 400
add_to_settings_menu = True
list_display = ('source', 'username')
list_filter = ('source', )

modeladmin_register(SocialFeedConfigurationAdmin)


@hooks.register('register_admin_urls')
def register_admin_urls():
return [
url(r'^socialfeed/', include(urls, app_name='wagtailsocialfeed', namespace='wagtailsocialfeed')),
url(r'^socialfeed/', include(urls, app_name='wagtailsocialfeed',
namespace='wagtailsocialfeed')),
]


Expand All @@ -39,9 +41,11 @@ def register_socialfeed_menu():
for config in config_qs:
url = reverse('wagtailsocialfeed:moderate', kwargs={'pk': config.pk})
config_menu_items.append(
MenuItem(six.text_type(config), url, classnames='icon icon-folder-inverse')
MenuItem(six.text_type(config), url,
classnames='icon icon-folder-inverse')
)

socialfeed_menu = SubMenu(config_menu_items)
return SubmenuMenuItem(
_('Social feed'), socialfeed_menu, classnames='icon icon-fa-rss', order=800)
_('Social feed'), socialfeed_menu, classnames='icon icon-fa-rss',
order=800)

0 comments on commit 394a97d

Please sign in to comment.