Skip to content
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
4 changes: 0 additions & 4 deletions collective/embedly/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
xmlns:zcml="http://namespaces.zope.org/zcml"
i18n_domain="collective.embedly">

<adapter factory=".controlpanel.EmbedlyControlPanelAdapter"
zcml:condition="installed Products.CMFDefault.formlib" />

<browser:resource
name="embedly.css"
file="embedly.css"
Expand All @@ -28,7 +25,6 @@
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
class=".controlpanel.EmbedlyControlPanel"
permission="cmf.ManagePortal"
zcml:condition="installed Products.CMFDefault.formlib"
/>

<browser:resourceDirectory
Expand Down
72 changes: 11 additions & 61 deletions collective/embedly/browser/controlpanel.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,17 @@
from zope.interface import implements
from zope.component import adapts
from zope.component import getUtility
from zope.formlib import form

from Products.CMFDefault.formlib.schema import SchemaAdapterBase
from Products.CMFPlone.interfaces import IPloneSiteRoot

from plone.registry.interfaces import IRegistry
from plone.app.controlpanel.form import ControlPanelForm

from collective.embedly import embedlyMessageFactory as _
from collective.embedly.interfaces import IEmbedlySettings
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper
from plone.app.registry.browser.controlpanel import RegistryEditForm
from plone.registry.interfaces import IRegistry
from plone.z3cform import layout
from zope.component import getUtility


class EmbedlyControlPanelAdapter(SchemaAdapterBase):

adapts(IPloneSiteRoot)
implements(IEmbedlySettings)

def __init__(self, context):
super(EmbedlyControlPanelAdapter, self).__init__(context)
registry = getUtility(IRegistry)
self.settings = registry.forInterface(IEmbedlySettings, False)

def getAPIKey(self):
return self.settings.api_key

def setAPIKey(self, value):
self.settings.api_key = value

def getUseServicesRegexp(self):
return self.settings.use_services_regexp

def setUseServicesRegexp(self, value):
self.settings.use_services_regexp = value

def getPersistentCache(self):
return self.settings.persistent_cache

def setPersistentCache(self, value):
self.settings.persistent_cache = value

def getCacheTimeout(self):
return self.settings.cache_timeout

def setCacheTimeout(self, value):
self.settings.cache_timeout = value

api_key = property(getAPIKey,
setAPIKey)

use_services_regexp = property(getUseServicesRegexp,
setUseServicesRegexp)

persistent_cache = property(getPersistentCache,
setPersistentCache)

cache_timeout = property(getCacheTimeout,
setCacheTimeout)


class EmbedlyControlPanel(ControlPanelForm):

class EmbedlyControlPanelForm(RegistryEditForm):
label = _("Embedly settings")
description = _("Lets you change the settings of collective.embedly")
form_fields = form.FormFields(IEmbedlySettings)
schema = IEmbedlySettings


EmbedlyControlPanel = layout.wrap_form(
EmbedlyControlPanelForm, ControlPanelFormWrapper)
4 changes: 2 additions & 2 deletions collective/embedly/setuphandlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from zope.interface import implements
from zope.interface import implementer
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.interfaces import INonInstallable
from zope.component import getUtility
Expand All @@ -16,8 +16,8 @@
})


@implementer(INonInstallable)
class HiddenProfiles(object):
implements(INonInstallable)

def getNonInstallableProfiles(self):
"""
Expand Down
24 changes: 16 additions & 8 deletions collective/embedly/tests/patch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import urllib2
import re
from urlparse import urlparse, parse_qsl
from collective.embedly.transform import get_services_regexp

try:
from urllib.request import urlopen
from urllib.parse import urlparse, parse_qsl
from urllib.error import HTTPError
except ImportError:
from urllib2 import urlopen
from urlparse import urlparse, parse_qsl
from urllib2 import HTTPError



embedly_data = '{"provider_url": "http://www.youtube.com/", "description": "HAYDAMAKY message", "title": "HAYDAMAKY message", "url": "http://www.youtube.com/watch?v=L1NPLlhFTVk", "author_name": "eastblok", "height": 360, "width": 640, "html": "<object width=\\"640\\" height=\\"360\\"><param name=\\"movie\\" value=\\"http://www.youtube.com/v/L1NPLlhFTVk?version=3\\"><param name=\\"allowFullScreen\\" value=\\"true\\"><param name=\\"allowscriptaccess\\" value=\\"always\\"><embed src=\\"http://www.youtube.com/v/L1NPLlhFTVk?version=3\\" type=\\"application/x-shockwave-flash\\" width=\\"640\\" height=\\"360\\" allowscriptaccess=\\"always\\" allowfullscreen=\\"true\\"></embed></object>", "thumbnail_width": 480, "version": "1.0", "provider_name": "YouTube", "thumbnail_url": "http://i1.ytimg.com/vi/L1NPLlhFTVk/hqdefault.jpg", "type": "video", "thumbnail_height": 360, "author_url": "http://www.youtube.com/user/eastblok"}'

Expand Down Expand Up @@ -39,8 +47,8 @@ def read(self):
service_url = p[1]

if key is not None and len(key) != 32:
from StringIO import StringIO
raise urllib2.HTTPError(self.url, 401, 'Unauthorized', None,
from six import StringIO
raise HTTPError(self.url, 401, 'Unauthorized', None,
StringIO('Invalid key or oauth_consumer_key provided: %s' % key))
if re.match(get_services_regexp(), service_url):
if service_url.endswith('.jpg'):
Expand All @@ -53,14 +61,14 @@ def read(self):
def close(self):
pass

original_urlopen = urllib2.urlopen
original_urlopen = urlopen


def patch_urlopen():
global original_urlopen
original_urlopen = urllib2.urlopen
urllib2.urlopen = dummy_urlopen
original_urlopen = urlopen
urlopen = dummy_urlopen


def unpatch_urlopen():
urllib2.urlopen = original_urlopen
urlopen = original_urlopen
8 changes: 6 additions & 2 deletions collective/embedly/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import urllib2
import json
import robotsuite
import unittest2 as unittest
Expand All @@ -24,6 +23,11 @@
from collective.embedly.tests.layer import EMBEDLY_ACCEPTANCE_TESTING
from collective.embedly.tests.patch import json_result

try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen


embedly_filter = lambda i: i['path'] == 'collective.embedly.transform.get_oembed'

Expand All @@ -41,7 +45,7 @@ def test_store_services(self):
view = portal.unrestrictedTraverse("@@update_embedly_services")
view.__of__(portal)()

res = urllib2.urlopen('http://api.embed.ly/1/services/python')
res = urlopen('http://api.embed.ly/1/services/python')
list_exp = []
for service in json.loads(res.read()):
list_exp.append('|'.join(service.get('regex', [])))
Expand Down
21 changes: 13 additions & 8 deletions collective/embedly/transform.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import urllib2
import json
import re
import logging
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import safe_unicode
from Products.PortalTransforms.interfaces import ITransform
from zope.interface import implements
from zope.interface import implementer
from zope.component import getUtility, queryUtility
from plone.registry.interfaces import IRegistry
from plone.memoize import ram
from plone.memoize.interfaces import ICacheChooser
from time import time
from urlparse import urlparse, parse_qsl
try:
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.parse import urlparse, parse_qsl, quote
except ImportError:
from urlparse import urlparse, parse_qsl
from urllib2 import urlopen, HTTPError, quote
try:
from zope.app.component.hooks import getSite
except ImportError:
Expand Down Expand Up @@ -48,7 +53,7 @@ def clear_cache():


def update_services():
resp = urllib2.urlopen('http://api.embed.ly/1/services/python')
resp = urlopen('http://api.embed.ly/1/services/python')
if resp.getcode() == 200:
list_exp = []
for service in json.loads(resp.read()):
Expand Down Expand Up @@ -152,12 +157,12 @@ def get_oembed(url, api_key=None, doc=None):
if embedly_query:
embedly_query = '&' + embedly_query
fetch_url = 'http://api.embed.ly/1/oembed?%surl=%s%s&format=json' % \
(api_key_string, urllib2.quote(curl.encode('utf-8')), embedly_query)
(api_key_string, quote(curl.encode('utf-8')), embedly_query)
logger.debug("HREF:%s URL:%s" % (url, fetch_url))
try:
result = urllib2.urlopen(fetch_url).read()
result = urlopen(fetch_url).read()
logger.debug("Response: %s" % result)
except urllib2.HTTPError, e:
except HTTPError as e:
if doc:
logger.error("Unexpected response from embedly API (%d: %s) while processing %s in %s" % (e.code, e.msg, url, doc))
else:
Expand Down Expand Up @@ -202,8 +207,8 @@ def replace(matchobj):
return link_url_re.sub(replace, text)


@implementer(ITransform)
class EmbedlyTransform:
implements(ITransform)

__name__ = "embedly_transform"

Expand Down
2 changes: 2 additions & 0 deletions docs/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog

dev
---
- Py3 compatibility
[sauzher]

- Don't attempt to decode unicode strings in transform.
[alecm]
Expand Down