Skip to content

Commit

Permalink
Merge pull request #194 from plone/changes-for-plone.app.z3cform
Browse files Browse the repository at this point in the history
get_ajaxselect_options is no longer used in plone.app.z3cform
  • Loading branch information
pbauer committed Apr 11, 2019
2 parents 7deb1c0 + 0cb689d commit 917edc6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 138 deletions.
7 changes: 7 additions & 0 deletions news/194.breaking
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Deprecate ``get_ajaxselect_options`` (no longer used).
``IWidgetsLayer`` and ``IWidgetsView`` are no longer used, remove them.
Deprecated ``IFileFactory`` import, use ``zope.filerepresentation`` instead.
Hard depend on ``plone.app.event``, it is meanwhile a dependenciy of Plone core.
Move ``IFieldPermissionChecker`` and ``Zope2FileUploadStorable`` to ``plone.app.z3cform`` in order to slowly fade out this package.
Use util ``first_weekday`` from ``plone.app.event`` and do not duplicate here; deprecated import placed.
[jensens]
6 changes: 0 additions & 6 deletions plone/app/widgets/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,4 @@

<include package="mockup" />

<utility
name="ZPublisher.HTTPRequest.FileUpload"
provides="plone.namedfile.interfaces.IStorage"
factory=".factories.Zope2FileUploadStorable"
/>

</configure>
26 changes: 9 additions & 17 deletions plone/app/widgets/factories.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
from zope.interface import implementer
from plone.namedfile.storages import MAXCHUNKSIZE
from plone.namedfile.interfaces import IStorage


@implementer(IStorage)
class Zope2FileUploadStorable(object):

def store(self, data, blob):
data.seek(0)

fp = blob.open('w')
block = data.read(MAXCHUNKSIZE)
while block:
fp.write(block)
block = data.read(MAXCHUNKSIZE)
fp.close()
# -*- coding: utf-8 -*-
import zope.deferredimport

# can be removed in Plone 6
zope.deferredimport.initialize()
zope.deferredimport.deprecated(
'Import Zope2FileUploadStorable from plone.app.z3cform.factories instead',
Zope2FileUploadStorable='plone.app.z3cform.factories:Zope2FileUploadStorable',
)
48 changes: 14 additions & 34 deletions plone/app/widgets/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
from zope.interface import Interface
from plone.app.z3cform.interfaces import IPloneFormLayer
from zope.filerepresentation.interfaces import IFileFactory


class IWidgetsLayer(IPloneFormLayer):
"""Browser layer used to indicate that plone.app.widgets is installed
"""


class IWidgetsView(Interface):
"""A view that gives access to various widget related functions.
"""

def getVocabulary():
"""Returns vocabulary
"""

def bodyDataOptions():
"""Returns the data attributes to be used on the body tag.
"""


class IFieldPermissionChecker(Interface):
"""Adapter factory for checking whether a user has permission to
edit a specific field on a content object.
"""

def validate(field_name, vocabulary_name=None):
"""Returns True if the current user has permission to edit the
`field_name` field. Returns False if the user does not have
permission. Raises and AttributeError if the field cannot be
found.
"""
# -*- coding: utf-8 -*-
import zope.deferredimport

# FileFactory only needed by ATContentTypes
zope.deferredimport.initialize()
zope.deferredimport.deprecated(
'Import IFileFactory from zope.filerepresentation.interfaces instead',
IFileFactory='zope.filerepresentation.interfaces:IFileFactory',
)

zope.deferredimport.deprecated(
'Import IFieldPermissionChecker from plone.app.z3cform.interfaces instead',
IFieldPermissionChecker='plone.app.z3cform.interfaces:IFieldPermissionChecker', # noqa
)
44 changes: 0 additions & 44 deletions plone/app/widgets/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,15 @@
# -*- coding: utf-8 -*-
from mock import Mock
from mock import patch
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.app.widgets.testing import PLONEAPPWIDGETS_INTEGRATION_TESTING
from plone.app.widgets.utils import get_querystring_options
from plone.app.widgets.utils import get_relateditems_options
from plone.app.widgets.utils import get_tinymce_options
from six.moves import reload_module

import unittest


class MockTool(Mock):
firstweekday = 6


class UtilsTests(unittest.TestCase):

def test__first_weekday(self):
# make sure, plone.app.event is available and mock it.
mock = Mock()
modules = {
'plone': mock,
'plone.app': mock.module,
'plone.app.event': mock.module.module,
'plone.app.event.base': mock.module.module.module,
}
with patch('Products.CMFCore.utils.getToolByName', new=MockTool), \
patch.dict('sys.modules', modules):
# test for plone.app.event installed
from plone.app.event import base
base.first_weekday = lambda: 0
base.wkday_to_mon1 = lambda x: x
from plone.app.widgets import utils
# reload utils, so that plone.app.event mock import
# works, even if it was imported before.
reload_module(utils)
orig_HAS_PAE = utils.HAS_PAE
utils.HAS_PAE = True
self.assertEqual(utils.first_weekday(), 0)
base.first_weekday = lambda: 1
self.assertEqual(utils.first_weekday(), 1)
base.first_weekday = lambda: 5
self.assertEqual(utils.first_weekday(), 1)

# test without plone.app.event installed
utils.HAS_PAE = False
self.assertEqual(utils.first_weekday(), 0)

# restore original state
utils.HAS_PAE = orig_HAS_PAE
reload_module(utils)


class TestQueryStringOptions(unittest.TestCase):
layer = PLONEAPPWIDGETS_INTEGRATION_TESTING

Expand Down
66 changes: 30 additions & 36 deletions plone/app/widgets/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-

from Acquisition import aq_base
from Acquisition import aq_parent
from datetime import datetime
Expand All @@ -15,37 +14,23 @@
from zope.component import providedBy
from zope.component import queryUtility
from zope.component.hooks import getSite
from zope.deprecation import deprecate
from zope.globalrequest import getRequest
from zope.i18n import translate
from zope.i18nmessageid import MessageFactory
from zope.schema.interfaces import IVocabularyFactory

import json
import zope.deferredimport


_ = MessageFactory('plone')


try:
from plone.app.event import base as pae_base
HAS_PAE = True
except ImportError:
HAS_PAE = False


def first_weekday():
if HAS_PAE:
wkday = pae_base.wkday_to_mon1(pae_base.first_weekday())
if wkday > 1:
return 1 # Default to Monday
return wkday
else:
cal = getToolByName(getSite(), 'portal_calendar', None)
if cal:
wkday = cal.firstweekday
if wkday == 6: # portal_calendar's Sunday is 6
return 0 # Sunday
return 1 # other cases: Monday
zope.deferredimport.deprecated(
'Import first_weekday from plone.app.event.base instead',
first_weekday='plone.app.event.base:first_weekday',
)


class NotImplemented(Exception):
Expand Down Expand Up @@ -91,8 +76,10 @@ def get_datetime_options(request):
return options


@deprecate("features were moved into the AjaxSelectWidget, remove in Plone 6")
def get_ajaxselect_options(context, value, separator, vocabulary_name,
vocabulary_view, field_name=None):
# code now part of the widget, let it in here for BBB and remove in Plone 6
options = {'separator': separator}
if vocabulary_name:
options['vocabularyUrl'] = '{}/{}?name={}'.format(
Expand Down Expand Up @@ -132,14 +119,28 @@ def get_relateditems_options(context, value, separator, vocabulary_name,

request = getRequest()
site = get_top_site_from_url(context, request)
options = get_ajaxselect_options(
site,
value,
separator,
vocabulary_name,
vocabulary_view,
field_name
options = {
'separator': separator,
}
if not vocabulary_name:
# we need a vocabulary!
raise ValueError('RelatedItems needs a vocabulary')
options['vocabularyUrl'] = '{0}/{1}?name={2}'.format(
get_context_url(site), vocabulary_view, vocabulary_name,
)
if field_name:
options['vocabularyUrl'] += '&field={0}'.format(field_name)
if value:
options['initialValues'] = {}
catalog = False
if vocabulary_name == 'plone.app.vocabularies.Catalog':
catalog = getToolByName(getSite(), 'portal_catalog')
for value in value.split(separator):
title = value
if catalog:
result = catalog(UID=value)
title = result[0].Title if result else value
options['initialValues'][value] = title

nav_root = getNavigationRootObject(context, site)

Expand Down Expand Up @@ -199,14 +200,7 @@ def get_querystring_options(context, querystring_view):
'previewURL': '%s/@@querybuilder_html_results' % base_url,
'previewCountURL': '%s/@@querybuildernumberofresults' % base_url,
'patternDateOptions': get_date_options(getRequest()),
'patternAjaxSelectOptions': get_ajaxselect_options(
context,
None,
';',
None,
None,
None
),
'patternAjaxSelectOptions': {'separator': ';'},
'patternRelateditemsOptions': get_relateditems_options(
context,
None,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '2.4.2.dev0'
version = '3.0.0.dev0'

setup(
name='plone.app.widgets',
Expand Down

0 comments on commit 917edc6

Please sign in to comment.