Skip to content

Commit

Permalink
[fc] Repository: plone.rest
Browse files Browse the repository at this point in the history
Branch: refs/heads/master
Date: 2022-03-24T16:57:37+01:00
Author: Mauro Amico (mamico) <mauro.amico@gmail.com>
Commit: plone/plone.rest@4891ad1

redirect with view (#132)

* redirect with view

* black

* black

* changes

Files changed:
A news/132.fix
M plone-5.2.x.cfg
M src/plone/rest/errors.py
M src/plone/rest/tests/test_redirects.py
  • Loading branch information
tisto committed Mar 24, 2022
1 parent 1b736b7 commit 3e09e52
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions last_commit.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
Repository: plone.app.upgrade
Repository: plone.rest


Branch: refs/heads/master
Date: 2022-03-17T11:54:16+01:00
Author: Maurits van Rees (mauritsvanrees) <maurits@vanrees.org>
Commit: https://github.com/plone/plone.app.upgrade/commit/4473b399fd2b38064c0fe6231207b3fb24e73974
Date: 2022-03-24T16:57:37+01:00
Author: Mauro Amico (mamico) <mauro.amico@gmail.com>
Commit: https://github.com/plone/plone.rest/commit/4891ad1ad61f0c9541c07539c2708bc82054fbea

Add new image scales.
redirect with view (#132)

Files changed:
A news/3279.bugfix
A plone/app/upgrade/v60/tests.py
M plone/app/upgrade/v60/alphas.py
M plone/app/upgrade/v60/configure.zcml

b'diff --git a/news/3279.bugfix b/news/3279.bugfix\nnew file mode 100644\nindex 00000000..047e2b54\n--- /dev/null\n+++ b/news/3279.bugfix\n@@ -0,0 +1,2 @@\n+Add new image scales.\n+[maurits]\ndiff --git a/plone/app/upgrade/v60/alphas.py b/plone/app/upgrade/v60/alphas.py\nindex d0adb75f..7cefe6a3 100644\n--- a/plone/app/upgrade/v60/alphas.py\n+++ b/plone/app/upgrade/v60/alphas.py\n@@ -1,11 +1,13 @@\n # -*- coding: utf-8 -*-\n from plone.app.upgrade.utils import loadMigrationProfile\n from plone.dexterity.fti import DexterityFTI\n+from plone.registry.interfaces import IRegistry\n from plone.uuid.interfaces import ATTRIBUTE_NAME\n from plone.uuid.interfaces import IUUIDGenerator\n from Products.CMFCore.utils import getToolByName\n from Products.CMFPlone.utils import safe_unicode\n from ZODB.broken import Broken\n+from zope.component import getUtility\n from zope.component import queryUtility\n from zope.component.hooks import getSite\n \n@@ -278,3 +280,62 @@ def fix_unicode_properties(context):\n portal = getSite()\n portal.reindexObject()\n portal.ZopeFindAndApply(portal, search_sub=1, apply_func=fix_properties)\n+\n+\n+def add_new_image_scales(context):\n+ """Add new image scales.\n+\n+ See PLIP 3279, which adds and updates a few scales, and especially my\n+ comment on how we should handle upgrades:\n+ https://github.com/plone/Products.CMFPlone/issues/3279#issuecomment-1064970253\n+\n+ Summary: we want an upgrade step in plone.app.upgrade that adds the\n+ completely new scales, without changing existing scales.\n+ """\n+ registry = getUtility(IRegistry)\n+ record = registry.records["plone.allowed_sizes"]\n+ new_scales = [\n+ "huge 1600:65536",\n+ "great 1200:65536",\n+ "larger 1000:65536",\n+ "teaser 600:65536",\n+ ]\n+ changed = False\n+ # Get the old/current value, without empty lines.\n+ old_value = [line for line in record.value if line.strip()]\n+ for line in new_scales:\n+ found = False\n+ new_name, new_dimensions = line.split()\n+ for old_line in old_value:\n+ try:\n+ old_name, old_dimensions = old_line.split()\n+ except (ValueError, KeyError, TypeError):\n+ continue\n+ if old_name == new_name:\n+ # A scale with this name is already defined. Keep it.\n+ found = True\n+ break\n+ if found:\n+ continue\n+ old_value.append(line)\n+ logger.info("Added image scale: %s", line)\n+ changed = True\n+\n+ if not changed:\n+ return\n+\n+ def sorter(value):\n+ try:\n+ dimensions = value.strip().split()[-1]\n+ width, height = dimensions.split(":")\n+ width = int(width)\n+ height = int(height)\n+ except (ValueError, KeyError, TypeError):\n+ return (0, 0)\n+ return width, height\n+\n+ # Sort the lines.\n+ new_value = sorted(old_value, key=sorter, reverse=True)\n+\n+ # Explicitly save the record.\n+ record.value = new_value\ndiff --git a/plone/app/upgrade/v60/configure.zcml b/plone/app/upgrade/v60/configure.zcml\nindex 7858c180..167d5799 100644\n--- a/plone/app/upgrade/v60/configure.zcml\n+++ b/plone/app/upgrade/v60/configure.zcml\n@@ -87,6 +87,11 @@\n import_profile="plone.app.upgrade.v60:to6004"\n />\n \n+ <gs:upgradeStep\n+ title="Add new image scales."\n+ handler=".alphas.add_new_image_scales"\n+ />\n+\n </gs:upgradeSteps>\n \n </configure>\ndiff --git a/plone/app/upgrade/v60/tests.py b/plone/app/upgrade/v60/tests.py\nnew file mode 100644\nindex 00000000..a54e4c20\n--- /dev/null\n+++ b/plone/app/upgrade/v60/tests.py\n@@ -0,0 +1,95 @@\n+# -*- coding: utf-8 -*-\n+from plone.app.testing import PLONE_INTEGRATION_TESTING\n+from plone.registry.interfaces import IRegistry\n+from Products.CMFCore.utils import getToolByName\n+from zope.component import getUtility\n+\n+import unittest\n+\n+\n+class Various60Test(unittest.TestCase):\n+ layer = PLONE_INTEGRATION_TESTING\n+\n+ def test_add_new_image_scales(self):\n+ from plone.app.upgrade.v60.alphas import add_new_image_scales\n+\n+ # These new scales should get added:\n+ new_scales = [\n+ "teaser 600:65536",\n+ "larger 1000:65536",\n+ "great 1200:65536",\n+ "huge 1600:65536",\n+ ]\n+ portal = self.layer["portal"]\n+ setup = getToolByName(portal, "portal_setup")\n+ registry = getUtility(IRegistry)\n+\n+ # Call the upgrade step.\n+ add_new_image_scales(setup)\n+ record = registry.records["plone.allowed_sizes"]\n+ for scale in new_scales:\n+ self.assertIn(scale, record.value)\n+\n+ # If scales with the given name already exist, do not change them.\n+ record.value = [\n+ "mini 200:200",\n+ "teaser 42:42",\n+ "maxi 200:400",\n+ ]\n+ add_new_image_scales(setup)\n+ self.assertIn("mini 200:200", record.value)\n+ self.assertIn("teaser 42:42", record.value)\n+ self.assertNotIn("teaser 600:65536", record.value)\n+ self.assertIn("huge 1600:65536", record.value)\n+\n+ # If we make a change, we sort by width, then height.\n+ self.assertEqual(\n+ record.value,\n+ [\n+ "huge 1600:65536",\n+ "great 1200:65536",\n+ "larger 1000:65536",\n+ "maxi 200:400",\n+ "mini 200:200",\n+ "teaser 42:42",\n+ ],\n+ )\n+\n+ # If we do not make a change, we also do not change the sort order.\n+ # As value, store the new scales alphabetically ordered.\n+ record.value = sorted(new_scales)\n+ add_new_image_scales(setup)\n+ self.assertEqual(record.value, sorted(new_scales))\n+\n+ # Check that the upgrade does not break easily.\n+ record.value = [\n+ "too many spaces 100:100",\n+ "not_enough_spaces200:200",\n+ "too_many_colons 300:300:400",\n+ "not_enough_many_colons 500",\n+ "good 600:600",\n+ " space 700:700 ",\n+ "# just a comment",\n+ "or an empty line:",\n+ "",\n+ ]\n+ # The biggest is is that this does not throw an error:\n+ add_new_image_scales(setup)\n+ # The bad scales will be at the end.\n+ self.assertEqual(\n+ record.value,\n+ [\n+ "huge 1600:65536",\n+ "great 1200:65536",\n+ "larger 1000:65536",\n+ " space 700:700 ",\n+ "teaser 600:65536",\n+ "good 600:600",\n+ "too many spaces 100:100",\n+ "not_enough_spaces200:200",\n+ "too_many_colons 300:300:400",\n+ "not_enough_many_colons 500",\n+ "# just a comment",\n+ "or an empty line:",\n+ ],\n+ )\n'

Repository: plone.app.upgrade


Branch: refs/heads/master
Date: 2022-03-24T15:51:03+01:00
Author: Maurits van Rees (mauritsvanrees) <maurits@vanrees.org>
Commit: https://github.com/plone/plone.app.upgrade/commit/ce871bef1f2d1e8aa79ed1dade250ba81b46678d

Merge branch 'maurits-add-scales'
* redirect with view

* black

* black

* changes

Files changed:
A news/3279.bugfix
A plone/app/upgrade/v60/tests.py
M plone/app/upgrade/v60/alphas.py
M plone/app/upgrade/v60/configure.zcml
A news/132.fix
M plone-5.2.x.cfg
M src/plone/rest/errors.py
M src/plone/rest/tests/test_redirects.py

b'diff --git a/news/3279.bugfix b/news/3279.bugfix\nnew file mode 100644\nindex 00000000..047e2b54\n--- /dev/null\n+++ b/news/3279.bugfix\n@@ -0,0 +1,2 @@\n+Add new image scales.\n+[maurits]\ndiff --git a/plone/app/upgrade/v60/alphas.py b/plone/app/upgrade/v60/alphas.py\nindex c0c53b9d..3291d554 100644\n--- a/plone/app/upgrade/v60/alphas.py\n+++ b/plone/app/upgrade/v60/alphas.py\n@@ -381,3 +381,62 @@ def cleanup_resources_and_bundles_in_registry(context=None):\n loadMigrationProfile(context, "profile-plone.app.caching:default", steps=["controlpanel"])\n if installer.is_profile_installed("Products.CMFPlacefulWorkflow:base"):\n loadMigrationProfile(context, "profile-Products.CMFPlacefulWorkflow:base", steps=["controlpanel"])\n+\n+\n+def add_new_image_scales(context):\n+ """Add new image scales.\n+\n+ See PLIP 3279, which adds and updates a few scales, and especially my\n+ comment on how we should handle upgrades:\n+ https://github.com/plone/Products.CMFPlone/issues/3279#issuecomment-1064970253\n+\n+ Summary: we want an upgrade step in plone.app.upgrade that adds the\n+ completely new scales, without changing existing scales.\n+ """\n+ registry = getUtility(IRegistry)\n+ record = registry.records["plone.allowed_sizes"]\n+ new_scales = [\n+ "huge 1600:65536",\n+ "great 1200:65536",\n+ "larger 1000:65536",\n+ "teaser 600:65536",\n+ ]\n+ changed = False\n+ # Get the old/current value, without empty lines.\n+ old_value = [line for line in record.value if line.strip()]\n+ for line in new_scales:\n+ found = False\n+ new_name, new_dimensions = line.split()\n+ for old_line in old_value:\n+ try:\n+ old_name, old_dimensions = old_line.split()\n+ except (ValueError, KeyError, TypeError):\n+ continue\n+ if old_name == new_name:\n+ # A scale with this name is already defined. Keep it.\n+ found = True\n+ break\n+ if found:\n+ continue\n+ old_value.append(line)\n+ logger.info("Added image scale: %s", line)\n+ changed = True\n+\n+ if not changed:\n+ return\n+\n+ def sorter(value):\n+ try:\n+ dimensions = value.strip().split()[-1]\n+ width, height = dimensions.split(":")\n+ width = int(width)\n+ height = int(height)\n+ except (ValueError, KeyError, TypeError):\n+ return (0, 0)\n+ return width, height\n+\n+ # Sort the lines.\n+ new_value = sorted(old_value, key=sorter, reverse=True)\n+\n+ # Explicitly save the record.\n+ record.value = new_value\ndiff --git a/plone/app/upgrade/v60/configure.zcml b/plone/app/upgrade/v60/configure.zcml\nindex c30aedf5..80a99d78 100644\n--- a/plone/app/upgrade/v60/configure.zcml\n+++ b/plone/app/upgrade/v60/configure.zcml\n@@ -92,6 +92,11 @@\n handler=".alphas.cleanup_resources_and_bundles_in_registry"\n />\n \n+ <gs:upgradeStep\n+ title="Add new image scales."\n+ handler=".alphas.add_new_image_scales"\n+ />\n+\n </gs:upgradeSteps>\n \n </configure>\ndiff --git a/plone/app/upgrade/v60/tests.py b/plone/app/upgrade/v60/tests.py\nnew file mode 100644\nindex 00000000..a54e4c20\n--- /dev/null\n+++ b/plone/app/upgrade/v60/tests.py\n@@ -0,0 +1,95 @@\n+# -*- coding: utf-8 -*-\n+from plone.app.testing import PLONE_INTEGRATION_TESTING\n+from plone.registry.interfaces import IRegistry\n+from Products.CMFCore.utils import getToolByName\n+from zope.component import getUtility\n+\n+import unittest\n+\n+\n+class Various60Test(unittest.TestCase):\n+ layer = PLONE_INTEGRATION_TESTING\n+\n+ def test_add_new_image_scales(self):\n+ from plone.app.upgrade.v60.alphas import add_new_image_scales\n+\n+ # These new scales should get added:\n+ new_scales = [\n+ "teaser 600:65536",\n+ "larger 1000:65536",\n+ "great 1200:65536",\n+ "huge 1600:65536",\n+ ]\n+ portal = self.layer["portal"]\n+ setup = getToolByName(portal, "portal_setup")\n+ registry = getUtility(IRegistry)\n+\n+ # Call the upgrade step.\n+ add_new_image_scales(setup)\n+ record = registry.records["plone.allowed_sizes"]\n+ for scale in new_scales:\n+ self.assertIn(scale, record.value)\n+\n+ # If scales with the given name already exist, do not change them.\n+ record.value = [\n+ "mini 200:200",\n+ "teaser 42:42",\n+ "maxi 200:400",\n+ ]\n+ add_new_image_scales(setup)\n+ self.assertIn("mini 200:200", record.value)\n+ self.assertIn("teaser 42:42", record.value)\n+ self.assertNotIn("teaser 600:65536", record.value)\n+ self.assertIn("huge 1600:65536", record.value)\n+\n+ # If we make a change, we sort by width, then height.\n+ self.assertEqual(\n+ record.value,\n+ [\n+ "huge 1600:65536",\n+ "great 1200:65536",\n+ "larger 1000:65536",\n+ "maxi 200:400",\n+ "mini 200:200",\n+ "teaser 42:42",\n+ ],\n+ )\n+\n+ # If we do not make a change, we also do not change the sort order.\n+ # As value, store the new scales alphabetically ordered.\n+ record.value = sorted(new_scales)\n+ add_new_image_scales(setup)\n+ self.assertEqual(record.value, sorted(new_scales))\n+\n+ # Check that the upgrade does not break easily.\n+ record.value = [\n+ "too many spaces 100:100",\n+ "not_enough_spaces200:200",\n+ "too_many_colons 300:300:400",\n+ "not_enough_many_colons 500",\n+ "good 600:600",\n+ " space 700:700 ",\n+ "# just a comment",\n+ "or an empty line:",\n+ "",\n+ ]\n+ # The biggest is is that this does not throw an error:\n+ add_new_image_scales(setup)\n+ # The bad scales will be at the end.\n+ self.assertEqual(\n+ record.value,\n+ [\n+ "huge 1600:65536",\n+ "great 1200:65536",\n+ "larger 1000:65536",\n+ " space 700:700 ",\n+ "teaser 600:65536",\n+ "good 600:600",\n+ "too many spaces 100:100",\n+ "not_enough_spaces200:200",\n+ "too_many_colons 300:300:400",\n+ "not_enough_many_colons 500",\n+ "# just a comment",\n+ "or an empty line:",\n+ ],\n+ )\n'
b'diff --git a/news/132.fix b/news/132.fix\nnew file mode 100644\nindex 0000000..e212318\n--- /dev/null\n+++ b/news/132.fix\n@@ -0,0 +1 @@\n+++api++ traverser should be kept on 30x redirections [mamico]\ndiff --git a/plone-5.2.x.cfg b/plone-5.2.x.cfg\nindex cfd8f76..82f35ea 100644\n--- a/plone-5.2.x.cfg\n+++ b/plone-5.2.x.cfg\n@@ -1,11 +1,12 @@\n [buildout]\n extends =\n base.cfg\n- https://dist.plone.org/release/5.2.4/versions.cfg\n+ https://dist.plone.org/release/5.2.7/versions.cfg\n find-links += https://dist.plone.org/thirdparty/\n versions=versions\n \n [versions]\n+plone.rest =\n black = 21.12b0\n \n # Error: The requirement (\'virtualenv>=20.0.35\') is not allowed by your [versions] constraint (20.0.26)\ndiff --git a/src/plone/rest/errors.py b/src/plone/rest/errors.py\nindex ecfe8a3..ecc6607 100644\n--- a/src/plone/rest/errors.py\n+++ b/src/plone/rest/errors.py\n@@ -180,7 +180,12 @@ def attempt_redirect(self):\n return False\n \n # remove ++api++ traverser\n- old_path = "/".join(filter("++api++".__ne__, old_path_elements))\n+ if "++api++" in old_path_elements:\n+ api_traverser_pos = old_path_elements.index("++api++")\n+ old_path_elements = [el for el in old_path_elements if el != "++api++"]\n+ else:\n+ api_traverser_pos = None\n+ old_path = "/".join(old_path_elements)\n \n # First lets try with query string in cases or content migration\n \n@@ -214,9 +219,9 @@ def attempt_redirect(self):\n url = urllib.parse.SplitResult(*(url[:2] + (url_path,) + url[3:])).geturl()\n else:\n # reinsert ++api++ traverser\n- if "++api++" in old_path_elements:\n+ if api_traverser_pos is not None:\n new_path_elements = new_path.split("/")\n- new_path_elements.insert(old_path_elements.index("++api++"), "++api++")\n+ new_path_elements.insert(api_traverser_pos, "++api++")\n new_path = "/".join(new_path_elements)\n url = self.request.physicalPathToURL(new_path)\n \ndiff --git a/src/plone/rest/tests/test_redirects.py b/src/plone/rest/tests/test_redirects.py\nindex 39b159b..73e7ac7 100644\n--- a/src/plone/rest/tests/test_redirects.py\n+++ b/src/plone/rest/tests/test_redirects.py\n@@ -59,6 +59,19 @@ def test_get_to_moved_item_causes_301_redirect_with_api_traverser(self):\n self.assertEqual("application/json", response.headers["Content-type"])\n self.assertEqual({"id": "folder-new", "method": "GET"}, response.json())\n \n+ def test_get_to_moved_item_causes_301_redirect_with_rest_view(self):\n+ response = requests.get(\n+ self.portal_url + "/++api++/folder-old/@actions",\n+ auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD),\n+ allow_redirects=False,\n+ )\n+ self.assertEqual(301, response.status_code)\n+ self.assertEqual(\n+ self.portal_url + "/++api++/folder-new/@actions",\n+ response.headers["Location"],\n+ )\n+ self.assertEqual(b"", response.raw.read())\n+\n def test_post_to_moved_item_causes_308_redirect(self):\n response = requests.post(\n self.portal_url + "/folder-old",\n'

0 comments on commit 3e09e52

Please sign in to comment.