Skip to content

Commit

Permalink
WEB-4122 : Override ILeadImageBehavior to limit image formats availab…
Browse files Browse the repository at this point in the history
…le for upload
  • Loading branch information
boulch committed Jul 3, 2024
1 parent 280823b commit ebfd629
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
1.2.18 (unreleased)
-------------------

- Nothing changed yet.
- WEB-4122 : Override ILeadImageBehavior to limit image formats available for upload


1.2.17 (2024-06-06)
Expand Down
1 change: 1 addition & 0 deletions src/imio/smartweb/common/behaviors/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# -*- coding: utf-8 -*-
from .behaviors import ICustomLeadImage # NOQA
45 changes: 45 additions & 0 deletions src/imio/smartweb/common/behaviors/behaviors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from plone.app.contenttypes import _
from plone.autoform.interfaces import IFormFieldProvider
from plone.namedfile.interfaces import INamedImage
from plone.namedfile.field import NamedBlobImage
from plone.app.contenttypes.behaviors.leadimage import ILeadImage
from plone.app.contenttypes.behaviors.leadimage import ILeadImageBehavior
from zope.interface import alsoProvides
from zope.schema import ValidationError
from zope.interface import implementer
from zope.interface import provider


class ICustomLeadImage(ILeadImage):
pass


class InvalidImageFormat(ValidationError):
__doc__ = "Unsupported image format."


def validate_image_format(value):
import pdb

pdb.set_trace()
allowed_extensions = [".gif", ".jpg", ".jpeg", ".png", ".svg", ".webp"]
if value:
filename = value.filename.lower()
if not any(filename.endswith(ext) for ext in allowed_extensions):
raise InvalidImageFormat
return True


@implementer(ILeadImage)
@provider(IFormFieldProvider)
class ICustomLeadImageBehavior(ILeadImageBehavior):

image = NamedBlobImage(
title=_("label_leadimage", default="Lead Image"),
description="",
required=False,
constraint=validate_image_format,
)


alsoProvides(ICustomLeadImageBehavior, INamedImage)
11 changes: 11 additions & 0 deletions src/imio/smartweb/common/behaviors/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@
provides=".iam.IAm"
/>

<plone:behavior
name="imio.smartweb.leadimage"
title="Lead Image with Custom Validation"
description="A lead image field with custom format validation."
factory="plone.app.contenttypes.behaviors.leadimage.LeadImage"
provides=".behaviors.ICustomLeadImageBehavior"
for="plone.dexterity.interfaces.IDexterityContent"
marker="plone.app.contenttypes.behaviors.leadimage.ILeadImage"
former_dotted_names="imio.smartweb.common.behaviors.ICustomLeadImage"
/>

</configure>

0 comments on commit ebfd629

Please sign in to comment.