Ejemplo n.º 1
0
    class LeadImageBlobExtender(LeadImageExtender):

        fields = [
            LeadimageBlobImageField(
                IMAGE_FIELD_NAME,
                required=False,
                storage=AnnotationStorage(migrate=True),
                languageIndependent=True,
                max_size=zconf.ATNewsItem.max_image_dimension,
                swallowResizeExceptions=zconf.swallowImageResizeExceptions.
                enable,
                pil_quality=zconf.pil_config.quality,
                pil_resize_algo=zconf.pil_config.resize_algo,
                validators=(('isNonEmptyFile', V_REQUIRED),
                            ('checkNewsImageMaxSize', V_REQUIRED)),
                widget=ImageWidget(
                    label=_(u"Lead image"),
                    description=_(u"You can upload lead image. This image "
                                  u"will be displayed above the content. "
                                  u"Uploaded image will be automatically "
                                  u"scaled to size specified in the leadimage "
                                  u"control panel."),
                    show_content_type=False,
                ),
            ),
            captionField,
        ]
    def handle_scales_action(self, action, data):
        CheckAuthenticator(self.request)
        number = 0
        ctool = getToolByName(self.context, 'portal_catalog')
        items = ctool(hasContentLeadImage=True)
        for i in items:
            obj = i.getObject()
            if obj is None:
                continue
            if not ILeadImageable.providedBy(obj):
                continue

            try:
                state = obj._p_changed
            except (ConflictError, KeyboardInterrupt):
                raise
            except:
                state = 0

            field = obj.getField(config.IMAGE_FIELD_NAME)
            if field is not None:
                field.removeScales(obj)
                field.createScales(obj)
                number = number + 1

            if state is None:
                obj._p_deactivate()

        self.status = _(u"text_scales_recreated", default=u"${number} scales recreated.", mapping={'number':number})
    def handle_scales_action(self, action, data):
        CheckAuthenticator(self.request)
        number = 0
        ctool = getToolByName(self.context, 'portal_catalog')
        items = ctool(hasContentLeadImage=True)
        for i in items:
            obj = i.getObject()
            if obj is None:
                continue
            if not ILeadImageable.providedBy(obj):
                continue

            try:
                state = obj._p_changed
            except (ConflictError, KeyboardInterrupt):
                raise
            except:
                state = 0

            field = obj.getField(config.IMAGE_FIELD_NAME)
            if field is not None:
                field.removeScales(obj)
                field.createScales(obj)
                number = number + 1

            if state is None:
                obj._p_deactivate()

        self.status = _(u"text_scales_recreated",
                        default=u"${number} scales recreated.",
                        mapping={'number': number})
if HAS_BLOB:
    class LeadimageBlobImageField(ExtensionField, BlobImageField):
        """Image Field with blob support that uses sizes defined in plone.app.imaging
        """

        def getFilename(self, context):
            return IMAGE_FIELD_NAME

captionField = LeadimageCaptionField(IMAGE_CAPTION_FIELD_NAME,
        required=False,
        searchable=False,
        storage = AnnotationStorage(),
        languageIndependent = False,
        widget = StringWidget(
                    label=_(u"Lead image caption"),
                    description=_(u"You may enter lead image caption text"),
                    maxlength=512,
                    size=60,
                ),
    )

show_leadimage = _ExtensionBooleanField(
            "show_leadimage_context",
            required=False,
            default=True,
            schemata="settings",
            widget=BooleanWidget(
                label=u"Show Lead Image on this item",
                description=u"This will show the lead image on the object display.",
            ),
class ILeadImagePrefsForm(Interface):
    """ The view for LeadImage  prefs form. """

    allowed_types = schema.Tuple(
        title=_(u'Portal types'),
        description=_(u'Portal types lead image may be attached to.'),
        missing_value=tuple(),
        value_type=schema.Choice(
            vocabulary="plone.app.vocabularies.ReallyUserFriendlyTypes"),
        required=False)

    image_width = schema.Int(
        title=_(u'Width'),
        description=_(u'Lead image scale width. Value specified in this field '
                      u"is used for generating 'leadimage' scale"),
        default=67,
        required=True)

    image_height = schema.Int(
        title=_(u'Height'),
        description=_(
            u'Lead image scale height. Value specified in this field '
            u"is used for generating 'leadimage' scale"),
        default=81,
        required=True)

    desc_scale_name = schema.Choice(
        title=_(u"'Description' image scale"),
        description=_(
            u'Please select scale which will be used next to Description field.'
        ),
        required=True,
        default='thumb',
        vocabulary=u"collective.contentleadimage.scales_vocabulary",
    )

    body_scale_name = schema.Choice(
        title=_(u"'Body' image scale"),
        description=_(u'Please select scale which will be used in the body.'),
        required=True,
        default='mini',
        vocabulary=u"collective.contentleadimage.scales_vocabulary",
    )

    viewlet_description = schema.Bool(
        title=_(u'Show image next to Description field'),
        default=True,
    )

    viewlet_body = schema.Bool(
        title=_(u'Show image in body area'),
        default=False,
    )
class LeadImagePrefsForm(ControlPanelForm):
    """ The view class for the lead image preferences form. """

    implements(ILeadImagePrefsForm)
    form_fields = form.FormFields(ILeadImagePrefsForm)

    label = _(u'Content Lead Image Settings Form')
    description = _(u'Select properties for Content Lead Image')
    form_name = _(u'Content Lead Image Settings')

    # handle_edit_action and handle_cancel_action are copied from
    # ControlPanelForm because they are overriden by my handle_scales_action
    @form.action(_p(u'label_save', default=u'Save'), name=u'save')
    def handle_edit_action(self, action, data):
        CheckAuthenticator(self.request)
        if form.applyChanges(self.context, self.form_fields, data,
                             self.adapters):
            self.status = _p("Changes saved.")
            notify(ConfigurationChangedEvent(self, data))
            self._on_save(data)
        else:
            self.status = _p("No changes made.")

    @form.action(_p(u'label_cancel', default=u'Cancel'),
                 validator=null_validator,
                 name=u'cancel')
    def handle_cancel_action(self, action, data):
        IStatusMessage(self.request).addStatusMessage(_p("Changes canceled."),
                                                      type="info")
        url = getMultiAdapter((self.context, self.request),
                              name='absolute_url')()
        self.request.response.redirect(url + '/plone_control_panel')
        return ''

    @form.action(_(u'label_recreate_scales', default=u'Recreate scales'),
                 name=u'scales',
                 validator=null_validator)
    def handle_scales_action(self, action, data):
        CheckAuthenticator(self.request)
        number = 0
        ctool = getToolByName(self.context, 'portal_catalog')
        items = ctool(hasContentLeadImage=True)
        for i in items:
            obj = i.getObject()
            if obj is None:
                continue
            if not ILeadImageable.providedBy(obj):
                continue

            try:
                state = obj._p_changed
            except (ConflictError, KeyboardInterrupt):
                raise
            except:
                state = 0

            field = obj.getField(config.IMAGE_FIELD_NAME)
            if field is not None:
                field.removeScales(obj)
                field.createScales(obj)
                number = number + 1

            if state is None:
                obj._p_deactivate()

        self.status = _(u"text_scales_recreated",
                        default=u"${number} scales recreated.",
                        mapping={'number': number})
Ejemplo n.º 7
0
class LeadImageExtender(object):
    adapts(ILeadImageable)
    implements(IOrderableSchemaExtender, IBrowserLayerAwareExtender)

    layer = ILeadImageSpecific

    fields = [
        LeadimageImageField(
            IMAGE_FIELD_NAME,
            required=False,
            storage=AnnotationStorage(migrate=True),
            languageIndependent=True,
            max_size=zconf.ATNewsItem.max_image_dimension,
            swallowResizeExceptions=zconf.swallowImageResizeExceptions.enable,
            pil_quality=zconf.pil_config.quality,
            pil_resize_algo=zconf.pil_config.resize_algo,
            validators=(('isNonEmptyFile', V_REQUIRED),
                        ('checkNewsImageMaxSize', V_REQUIRED)),
            widget=ImageWidget(
                label=_(u"Lead image"),
                description=_(u"You can upload lead image. This image "
                              u"will be displayed above the content. "
                              u"Uploaded image will be automatically "
                              u"scaled to size specified in the leadimage "
                              u"control panel."),
                show_content_type=False,
            ),
        ),
        captionField,
    ]

    def __init__(self, context):
        self.context = context

    def getFields(self):
        portal = getUtility(IPloneSiteRoot)
        cli_prefs = ILeadImagePrefsForm(portal)
        if cli_prefs.cli_props is not None:
            portal_type = getattr(self.context, 'portal_type', None)
            if portal_type in cli_prefs.allowed_types:
                return self.fields
        return []

    def getOrder(self, original):
        """
        'original' is a dictionary where the keys are the names of
        schemata and the values are lists of field names, in order.

        Move leadImage field just after the Description
        """
        default = original.get('default', None)
        if default:
            desc_index = 0
            # if there is no title nor description field, do nothing
            if 'description' in default:
                desc_index = default.index('description')
            elif 'title' in default:
                desc_index = default.index('title')
            if desc_index >= 0 and (IMAGE_FIELD_NAME in default):
                default.remove(IMAGE_FIELD_NAME)
                default.insert(desc_index + 1, IMAGE_FIELD_NAME)
                if IMAGE_CAPTION_FIELD_NAME in default:
                    default.remove(IMAGE_CAPTION_FIELD_NAME)
                    default.insert(desc_index + 2, IMAGE_CAPTION_FIELD_NAME)
        return original
Ejemplo n.º 8
0
if HAS_BLOB:

    class LeadimageBlobImageField(ExtensionField, BlobImageField):
        """Image Field with blob support that uses sizes defined in plone.app.imaging
        """
        pass


captionField = LeadimageCaptionField(
    IMAGE_CAPTION_FIELD_NAME,
    required=False,
    searchable=False,
    storage=AnnotationStorage(),
    languageIndependent=False,
    widget=StringWidget(
        label=_(u"Lead image caption"),
        description=_(u"You may enter lead image caption text"),
    ),
)


class LeadImageExtender(object):
    adapts(ILeadImageable)
    implements(IOrderableSchemaExtender, IBrowserLayerAwareExtender)

    layer = ILeadImageSpecific

    fields = [
        LeadimageImageField(
            IMAGE_FIELD_NAME,
            required=False,