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

Make sure the hash key changes after cropping an image. #142

Merged
merged 5 commits into from
Sep 22, 2023
Merged
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
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ indent_size = 2
[*.{json,jsonl,js,jsx,ts,tsx,css,less,scss,html}] # Frontend development
# 2 space indentation
indent_size = 2
max_line_length = 80

[{Makefile,.gitmodules}]
# Tab indentation (no size specified, but view as 4 spaces)
Expand Down
14 changes: 10 additions & 4 deletions .github/workflows/meta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ on:
- main
workflow_dispatch:

##
# To set environment variables for all jobs, add in .meta.toml:
# [github]
# env = """
# debug: 1
# image-name: 'org/image'
# image-tag: 'latest'
# """
##

jobs:
qa:
uses: plone/meta/.github/workflows/qa.yml@main
test:
uses: plone/meta/.github/workflows/test.yml@main
coverage:
uses: plone/meta/.github/workflows/coverage.yml@main
dependencies:
uses: plone/meta/.github/workflows/dependencies.yml@main
release_ready:
uses: plone/meta/.github/workflows/release_ready.yml@main
circular:
uses: plone/meta/.github/workflows/circular.yml@main

##
# To modify the list of default jobs being created add in .meta.toml:
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
- "6.0-dev"
steps:
- uses: actions/checkout@v2
- uses: nanasess/setup-chromedriver@v1
- uses: nanasess/setup-chromedriver@v2

- name: Setup Plone ${{ matrix.plone }} with Python ${{ matrix.python }}
uses: plone/setup-plone@v1.0.0
uses: plone/setup-plone@v2.0.0
with:
python-version: ${{ matrix.python }}
plone-version: ${{ matrix.plone }}
Expand All @@ -46,11 +46,12 @@ jobs:
run: |
make VENV=off lint

- name: Run tests
- name: Start Browser
run: |
export DISPLAY=:99
chromedriver --url-base=/wd/hub &
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 &
sleep 2
export ROBOT_BROWSER=headlesschrome &
make VENV=off test-ignore-warnings
sudo Xvfb -ac :99 -screen 0 1920x1280x24 > /dev/null 2>&1 &

- name: Run tests
run: |
ROBOT_BROWSER=headlesschrome make VENV=off test-ignore-warnings
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*.pyc
*.pyo

# translation related
*.mo

# tools related
build/
.coverage
Expand Down
10 changes: 9 additions & 1 deletion .meta.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# See the inline comments on how to expand/tweak this configuration file
[meta]
template = "default"
commit-id = "c1677eef"
commit-id = "ea1b238f"

[pyproject]
codespell_skip = "*.js,*.min.js,*.min.js.map,*.css.map,yarn.lock,robot_*,test_*"
Expand All @@ -17,3 +17,11 @@ test_*
robot_*
forest.*
"""

[github]
jobs = [
"qa",
"coverage",
"dependencies",
"release_ready",
]
18 changes: 14 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ci:

repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.8.0
rev: v3.10.1
hooks:
- id: pyupgrade
args: [--py38-plus]
Expand All @@ -16,7 +16,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/collective/zpretty
Expand All @@ -32,7 +32,7 @@ repos:
# """
##
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8

Expand Down Expand Up @@ -71,10 +71,20 @@ repos:
- id: check-python-versions
args: ['--only', 'setup.py,pyproject.toml']
- repo: https://github.com/collective/i18ndude
rev: "6.0.0"
rev: "6.1.0"
hooks:
- id: i18ndude


##
# Add extra configuration options in .meta.toml:
# [pre_commit]
# i18ndude_extra_lines = """
# _your own configuration lines_
# """
##


##
# Add extra configuration options in .meta.toml:
# [pre_commit]
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Bug fixes:
[petschki] (#0)


- Make sure the hash key changes after cropping an image.
[mathias.leimgruber]


Internal:


Expand Down
37 changes: 0 additions & 37 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,42 +1,6 @@
# Generated from:
# https://github.com/plone/meta/tree/master/config/default
# See the inline comments on how to expand/tweak this configuration file
[tool.towncrier]
directory = "news/"
filename = "CHANGES.rst"
title_format = "{version} ({project_date})"
underlines = ["-", ""]

[[tool.towncrier.type]]
directory = "breaking"
name = "Breaking changes:"
showcontent = true

[[tool.towncrier.type]]
directory = "feature"
name = "New features:"
showcontent = true

[[tool.towncrier.type]]
directory = "bugfix"
name = "Bug fixes:"
showcontent = true

[[tool.towncrier.type]]
directory = "internal"
name = "Internal:"
showcontent = true

[[tool.towncrier.type]]
directory = "documentation"
name = "Documentation:"
showcontent = true

[[tool.towncrier.type]]
directory = "tests"
name = "Tests"
showcontent = true

##
# Add extra configuration options in .meta.toml:
# [pyproject]
Expand Down Expand Up @@ -126,7 +90,6 @@ Pillow = ['PIL']
# "gitpython = ['git']",
# "pygithub = ['github']",
# ]
# """
##

[tool.check-manifest]
Expand Down
7 changes: 7 additions & 0 deletions src/plone/app/imagecropping/storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from Acquisition import aq_base
from persistent.dict import PersistentDict
from plone.app.imagecropping import PAI_STORAGE_KEY
from plone.app.imagecropping.events import CroppingInfoChangedEvent
Expand Down Expand Up @@ -44,6 +45,12 @@ def store(self, fieldname, scale, box):
self.remove(fieldname, scale)
key = self._key(fieldname, scale)
self._storage[key] = box

context = aq_base(self.context)
field = getattr(context, fieldname, None)
if field is not None:
field._p_changed = True # Force a new hash key

notify(CroppingInfoChangedEvent(self.context))

def read(self, fieldname, scale):
Expand Down
28 changes: 28 additions & 0 deletions src/plone/app/imagecropping/tests/test_cropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from zope.annotation.interfaces import IAnnotations
from zope.lifecycleevent import ObjectModifiedEvent

import transaction
import unittest


Expand Down Expand Up @@ -84,6 +85,33 @@ def test_accessing_images(self):
"imagescaling does not return cropped image",
)

def test_create_new_key_hash_for_copped_images(self):
"""Even though the origunal image did not change,
the cache key needs to change, otherwise cache proxies and browser
don't about the 'new' cropped image.

Since the original image does not change, we need to force update
the modification (_p_mtime) on the field

Hint: the has key has the modification time included.
"""

view = self.img.restrictedTraverse("@@crop-image")
view._crop(fieldname="image", scale="thumb", box=(14, 14, 218, 218))
transaction.commit() # needed in order to have a _p_mtime on objects

# another use-case: call plone.app.imaging's ImageScaling view
thumb2 = self.img.restrictedTraverse("@@images")
tag_thumb2 = thumb2.tag(scale="thumb")

view._crop(fieldname="image", scale="thumb", box=(14, 14, 100, 100))
transaction.commit()

thumb3 = self.img.restrictedTraverse("@@images")
tag_thumb3 = thumb3.tag(scale="thumb")

self.assertNotEqual(tag_thumb2, tag_thumb3)

def test_image_formats(self):
"""make sure the scales have the same format as the original image"""

Expand Down
24 changes: 19 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ commands =
echo "Unrecognized environment name {envname}"
false

[testenv:init]
description = Prepare environment
skip_install = true
commands =
echo "Initial setup complete"


[testenv:format]
description = automatically reformat code
skip_install = true
Expand Down Expand Up @@ -86,11 +93,20 @@ set_env =
# test_environment_variables = """
# PIP_EXTRA_INDEX_URL=https://my-pypi.my-server.com/
# """
#
# Set constrain_package_deps .meta.toml:
# [tox]
# constrain_package_deps = "false"
##
deps =
zope.testrunner
-c https://dist.plone.org/release/6.0-dev/constraints.txt

##
# Specify additional deps in .meta.toml:
# [tox]
# test_deps_additional = "-esources/plonegovbr.portal_base[test]"
#
# Specify a custom constraints file in .meta.toml:
# [tox]
# constraints_file = "https://my-server.com/constraints.txt"
Expand Down Expand Up @@ -128,6 +144,7 @@ deps =
coverage
zope.testrunner
-c https://dist.plone.org/release/6.0-dev/constraints.txt

commands =
coverage run --branch --source plone.app.imagecropping {envbindir}/zope-testrunner --quiet --all --test-path={toxinidir}/src -s plone.app.imagecropping {posargs}
coverage report -m --format markdown
Expand All @@ -142,13 +159,9 @@ skip_install = true
deps =
twine
build
towncrier
-c https://dist.plone.org/release/6.0-dev/constraints.txt

commands =
# fake version to not have to install the package
# we build the change log as news entries might break
# the README that is displayed on PyPI
towncrier build --version=100.0.0 --yes
python -m build --sdist --no-isolation
twine check dist/*

Expand All @@ -171,6 +184,7 @@ deps =
pipdeptree
pipforester
-c https://dist.plone.org/release/6.0-dev/constraints.txt

commands =
# Generate the full dependency tree
sh -c 'pipdeptree -j > forest.json'
Expand Down
Loading