Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call new pre_scale and get_or_generate from plone.scale. #113

Merged
merged 46 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
691114d
Call new pre_scale and get_or_generate from plone.scale.
mauritsvanrees May 15, 2022
7de488e
Use 'pre' at the right places.
mauritsvanrees May 16, 2022
a5f1338
Do not include old-style srcset when requesting a scale via a url.
mauritsvanrees May 17, 2022
340c411
Merge branch 'master' into pre-scale
mauritsvanrees May 17, 2022
ce07e8d
Add images-test view, only for Editors.
mauritsvanrees May 17, 2022
ce1cf5f
From Products.CMFCore only load permissions.zcml.
mauritsvanrees May 17, 2022
de6903d
mxdev: use the pre-scale branch of plone.scale.
mauritsvanrees May 17, 2022
3fda001
Start fixing tests.
mauritsvanrees May 17, 2022
4e3d3b9
Merge branch 'master' into pre-scale
mauritsvanrees May 17, 2022
4a262a3
We need to pass include_srcset=True in tests.
mauritsvanrees May 17, 2022
ec3cd65
Fixed the tests for srcsets.
mauritsvanrees May 18, 2022
6a2f451
Added news snippets
mauritsvanrees May 18, 2022
6b90fa0
images-test: sort the scales nicely
mauritsvanrees May 18, 2022
291b85e
images-test: add link to open image scale in new tab.
mauritsvanrees May 18, 2022
50aa9b4
prevent unknown keyword parameter modified
MrTango May 20, 2022
e0ce960
Catch TypeError when getting IPrimaryFieldInfo.
mauritsvanrees May 23, 2022
25d0814
Do call IPrimaryFieldInfo with self.context and None, like before.
mauritsvanrees May 23, 2022
af46d75
get_original_value: return IPrimaryFieldInfo.value
mauritsvanrees May 23, 2022
070740f
calculate_srcset: if scale_src is None, do not treat it as dictionary.
mauritsvanrees May 23, 2022
06a0a5c
Register adapter for primary field info in tests.
mauritsvanrees May 23, 2022
22f5a8a
Fix undefined names in primary field code.
mauritsvanrees May 23, 2022
9ef547e
Merge branch 'master' into pre-scale
mauritsvanrees May 24, 2022
369d468
Accept fieldname in get_original_value.
mauritsvanrees May 24, 2022
163ae34
black formating
MrTango Jun 1, 2022
0b2dd7b
Add picture method to ImageScaling and include it in test.pt
MrTango Jun 1, 2022
e085b96
Set width and height in picture method, update examples in test.pt
MrTango Jun 2, 2022
d75880a
Add title and alt attributes, add changelog entry
MrTango Jun 2, 2022
430b595
move utils.Img2PictureTag from outputfilter to namedfile.picture
MrTango Jun 3, 2022
cc1036c
use PloneSandboxLayer for testing
MrTango Jun 3, 2022
8764913
use resolve_urls and uid params for create_picture_tag
MrTango Jun 3, 2022
3bd293c
Tests: add UID to DummyContent and patch uuidToObject.
mauritsvanrees Jun 3, 2022
fe0850c
Revert "use PloneSandboxLayer for testing"
MrTango Jun 7, 2022
8d0567e
rename image_srcsets to picture_variants, fix captioning
MrTango Jun 7, 2022
bddd88a
refacture picture.py and fix tests
MrTango Jun 8, 2022
aafadf8
black fixes
MrTango Jun 8, 2022
112b0e3
Fixed import for Plone 5.2.
mauritsvanrees Jun 8, 2022
1c2ce5e
black picture
mauritsvanrees Jun 8, 2022
b181852
Let picture method return an img tag if the picture variant is not fo…
mauritsvanrees Jun 8, 2022
b64457c
use plone.app.uuid.utils.uuidToObject
MrTango Jun 8, 2022
0833aba
Merge branch 'pre-scale-picture-tag' of https://github.com/plone/plon…
MrTango Jun 8, 2022
5983331
Add dependency on plone.app.uuid
mauritsvanrees Jun 8, 2022
c4c68a6
cleanup print statements
MrTango Jun 8, 2022
74ea17c
Merge branch 'pre-scale-picture-tag' of https://github.com/plone/plon…
MrTango Jun 8, 2022
8613ff2
Update test.pt
mauritsvanrees Jun 8, 2022
8660d4e
Update news snippet.
mauritsvanrees Jun 8, 2022
26ce3a8
Merge branch 'master' into pre-scale
mauritsvanrees Jun 8, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ include
lib
parts
requirements-*-mxdev.txt
sources/
2 changes: 2 additions & 0 deletions news/113.feature.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Creating a tag no longer generates the actual scale.
[maurits]
5 changes: 5 additions & 0 deletions news/113.feature.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Add ``@@images-test`` page for Editors.
This shows various variants from the image field of the current context.
It shows a list of stored scales.
It allows purging the stored scales.
[maurits]
5 changes: 5 additions & 0 deletions news/113.feature.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Add picture method to to ImageScaling and include it in @@image-test.
Picture tags only work on Plone 6, with several other branches for picture variants merged.
See `plip-image-srcsets.cfg <https://github.com/plone/buildout.coredev/blob/6.0/plips/plip-image-srcsets.cfg>`_.
If not available (like on Plone 5.2), an ordinary image tag is created.
[MrTango]
143 changes: 143 additions & 0 deletions plone/namedfile/picture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import logging
import re

from plone.namedfile.interfaces import IAvailableSizes
from plone.app.uuid.utils import uuidToObject
from plone.registry.interfaces import IRegistry
from zope.component import getUtility
from zope.component import queryUtility
from bs4 import BeautifulSoup

try:
from plone.base.interfaces import IImagingSchema
except ImportError:
from Products.CMFPlone.interfaces.controlpanel import IImagingSchema


logger = logging.getLogger("plone.outputfilter.picture_variant")
appendix_re = re.compile("^(.*)([?#].*)$")
resolveuid_re = re.compile("^[./]*resolve[Uu]id/([^/]*)/?(.*)$")


def get_allowed_scales():
sizes_util = queryUtility(IAvailableSizes)
if sizes_util is None:
return {}
sizes = sizes_util()
if sizes is None:
return {}
return sizes


def get_picture_variants():
registry = getUtility(IRegistry)
settings = registry.forInterface(IImagingSchema, prefix="plone", check=False)
return getattr(settings, "picture_variants", {})


class Img2PictureTag(object):
def get_scale_name(self, scale_line):
parts = scale_line.split(" ")
return parts and parts[0] or ""

def get_scale_width(self, scale):
"""get width from allowed_scales line
large 800:65536
"""
allowed_scales = get_allowed_scales()
scale_info = allowed_scales.get(scale)
return scale_info[0]

def create_picture_tag(
self, sourceset, attributes, uid=None, fieldname=None, resolve_urls=False
):
"""Converts the img tag to a picture tag with picture_variant definition"""
width = None
height = None
src = attributes.get("src")
if not uid and not src:
raise TypeError("Either uid or attributes['src'] need to be given.")
soup = BeautifulSoup("", "html.parser")
allowed_scales = get_allowed_scales()
if uid:
obj = uuidToObject(uid)
else:
obj = self.resolve_uid_url(src)
picture_tag = soup.new_tag("picture")
css_classes = attributes.get("class", [])
if "captioned" in css_classes:
picture_tag["class"] = "captioned"
for i, source in enumerate(sourceset):
target_scale = source["scale"]
media = source.get("media")

additional_scales = source.get("additionalScales", None)
if additional_scales is None:
additional_scales = [
self.get_scale_name(s) for s in allowed_scales if s != target_scale
]
source_scales = [target_scale] + additional_scales
source_srcset = []
for scale in source_scales:
if resolve_urls and obj:
scale_view = obj.unrestrictedTraverse("@@images", None)
scale_obj = scale_view.scale(fieldname, scale, pre=True)
scale_url = scale_obj.url
else:
# obj = self.resolve_uid_url(src)
# scale_view = obj.unrestrictedTraverse("@@images", None)
# scale_obj = scale_view.scale(fieldname, scale, pre=True)
# scale_url = scale_obj.url
scale_url = self.update_src_scale(src=src, scale=scale)
scale_width = self.get_scale_width(scale)
source_srcset.append("{0} {1}w".format(scale_url, scale_width))
source_tag = soup.new_tag("source", srcset=",\n".join(source_srcset))
if media:
source_tag["media"] = media
picture_tag.append(source_tag)
if i == len(sourceset) - 1:
if resolve_urls and obj:
scale_view = obj.unrestrictedTraverse("@@images", None)
scale_obj = scale_view.scale(fieldname, target_scale, pre=True)
scale_url = scale_obj.url
width = scale_obj.width
height = scale_obj.height
else:
# obj = self.resolve_uid_url(src)
# scale_view = obj.unrestrictedTraverse("@@images", None)
# scale_obj = scale_view.scale(fieldname, target_scale, pre=True)
# scale_url = scale_obj.url
# width = scale_obj.width
# height = scale_obj.height
scale_url = self.update_src_scale(src=src, scale=target_scale)
img_tag = soup.new_tag("img", src=scale_url)
for k, attr in attributes.items():
if k in ["src", "srcset"]:
continue
img_tag.attrs[k] = attr
img_tag["loading"] = "lazy"
if width:
img_tag["width"] = width
if height:
img_tag["height"] = height
picture_tag.append(img_tag)
return picture_tag

def resolve_uid_url(self, href):
obj = None
subpath = href
match = resolveuid_re.match(subpath)
if match is not None:
uid, _subpath = match.groups()
obj = uuidToObject(uid)
return obj

def update_src_scale(self, src, scale):
parts = src.split("/")
if "." in parts[-1]:
field_name = parts[-1].split("-")[0]
src_scale = "/".join(parts[:-1]) + "/{0}/{1}".format(field_name, scale)
src_scale
else:
src_scale = "/".join(parts[:-1]) + "/{}".format(scale)
return src_scale
Loading