-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WEB-4122 : Override ILeadImageBehavior to limit image formats availab…
…le for upload
- Loading branch information
Showing
4 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# -*- coding: utf-8 -*- | ||
from .behaviors import ICustomLeadImage # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters