Detect type in Dexterity Validator on add item

classic Classic list List threaded Threaded
2 messages Options
Daniel Jowett Daniel Jowett
Reply | Threaded
Open this post in threaded view
|

Detect type in Dexterity Validator on add item

Hi all,

I wrote a image dimension validator for plone.app.contenttypes, as included below
(inspired by this article by Daniel Widerin).

Anyway, I wanted to exclude News Items from the validation, which it does on object Edit, but not on object Creation.
How can I get it working on both?


# -*- coding: utf-8 -*-
from plone.namedfile.interfaces import INamedBlobImageField
from plone.app.contenttypes.behaviors.leadimage import ILeadImage
from plone.app.contenttypes.interfaces import INewsItem

from zope.interface import Invalid
from z3c.form import validator
from five import grok

# Strict Dimensions check
REQUIRED_HEIGHT = 320
REQUIRED_WIDTH = 2000

# Filesize limit
MAXSIZE_KB = 200


class ImageDimensionsValidator(validator.FileUploadValidator):

    def validate(self, value):
        super(ImageDimensionsValidator, self).validate(value)

        if INewsItem.providedBy(self.context):
            return None


        if value:
            # See: plone.namedfile.file.NamedBlobImage
            width, height = value.getImageSize()
           
            if REQUIRED_HEIGHT and REQUIRED_WIDTH:
                if (width != REQUIRED_WIDTH or
                    height != REQUIRED_HEIGHT):
                    raise Invalid("Image has wrong dimensions - it should be %d x %d pixels (h x w)"
                                  ", but is %d x %d" %
                                  (REQUIRED_HEIGHT, REQUIRED_WIDTH, height, width))
            elif REQUIRED_HEIGHT:
                if (height != REQUIRED_HEIGHT):
                    raise Invalid("Image is the wrong height - it should be %d pixels" %
                                  (REQUIRED_HEIGHT))
            elif REQUIRED_WIDTH:
                if (width != REQUIRED_WIDTH):
                    raise Invalid("Image is the wrong width - it should be %d pixels" %
                                  (REQUIRED_WIDTH))

            # Lastly check filesize
            if value.getSize() > (MAXSIZE_KB * 1024):
                raise Invalid("Image filesize is too large. Maximum permitted: %dKB " % MAXSIZE_KB)
           


validator.WidgetValidatorDiscriminators(ImageDimensionsValidator,
                                        context=ILeadImage,
                                        field=ILeadImage['image'])
grok.global_adapter(ImageDimensionsValidator)



Cheers,
Daniel

--
Daniel Jowett

Jowett Enterprises Ltd
Tel: 0333 3 553 773
Mobile: 07870 667 126
[hidden email]
www.jowettenterprises.com

------------------------------------------------------------------------------
Shape the Mobile Experience: Free Subscription
Software experts and developers: Be at the forefront of tech innovation.
Intel(R) Software Adrenaline delivers strategic insight and game-changing
conversations that shape the rapidly evolving mobile landscape. Sign up now.
http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
David Glick (Plone) David Glick (Plone)
Reply | Threaded
Open this post in threaded view
|

Re: Detect type in Dexterity Validator on add item

On 11/22/13, 4:05 AM, Daniel Jowett wrote:
Hi all,

I wrote a image dimension validator for plone.app.contenttypes, as included below
(inspired by this article by Daniel Widerin).

Anyway, I wanted to exclude News Items from the validation, which it does on object Edit, but not on object Creation.
How can I get it working on both?


# -*- coding: utf-8 -*-
from plone.namedfile.interfaces import INamedBlobImageField
from plone.app.contenttypes.behaviors.leadimage import ILeadImage
from plone.app.contenttypes.interfaces import INewsItem

from zope.interface import Invalid
from z3c.form import validator
from five import grok

# Strict Dimensions check
REQUIRED_HEIGHT = 320
REQUIRED_WIDTH = 2000

# Filesize limit
MAXSIZE_KB = 200


class ImageDimensionsValidator(validator.FileUploadValidator):

    def validate(self, value):
        super(ImageDimensionsValidator, self).validate(value)

        if INewsItem.providedBy(self.context):
            return None


        if value:
            # See: plone.namedfile.file.NamedBlobImage
            width, height = value.getImageSize()
           
            if REQUIRED_HEIGHT and REQUIRED_WIDTH:
                if (width != REQUIRED_WIDTH or
                    height != REQUIRED_HEIGHT):
                    raise Invalid("Image has wrong dimensions - it should be %d x %d pixels (h x w)"
                                  ", but is %d x %d" %
                                  (REQUIRED_HEIGHT, REQUIRED_WIDTH, height, width))
            elif REQUIRED_HEIGHT:
                if (height != REQUIRED_HEIGHT):
                    raise Invalid("Image is the wrong height - it should be %d pixels" %
                                  (REQUIRED_HEIGHT))
            elif REQUIRED_WIDTH:
                if (width != REQUIRED_WIDTH):
                    raise Invalid("Image is the wrong width - it should be %d pixels" %
                                  (REQUIRED_WIDTH))

            # Lastly check filesize
            if value.getSize() > (MAXSIZE_KB * 1024):
                raise Invalid("Image filesize is too large. Maximum permitted: %dKB " % MAXSIZE_KB)
           


validator.WidgetValidatorDiscriminators(ImageDimensionsValidator,
                                        context=ILeadImage,
                                        field=ILeadImage['image'])
grok.global_adapter(ImageDimensionsValidator)



You could do something like this:

if INewsItem.providedBy(self.context) or (IAddForm.providedBy(self.view) and self.view.portal_type == 'News Item'):


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users