def handle_edit_action(self, action, data):
     CheckAuthenticator(self.request)
     if form.applyChanges(self.context, self.form_fields, data,
                          self.adapters):
         status = _("Changes saved, you can now configure the back-end.")
         self.status = status
         notify(ConfigurationChangedEvent(self, data))
         self._on_save(data)
     else:
         self.status = _("No changes made.")
    def setPhoto_ofs(self, data):
        photo_data = data["photo"]
        content_type = self.widgets["photo"].value.headers["content-type"]
        if content_type != "image/jpeg":
            error = interface.Invalid(_(u"Is not a JPEG photo"))
            raise form.interfaces.WidgetActionExecutionError("photo", error)
        photo_file = StringIO()
        photo_file.write(photo_data)
        photo_id = str(data["uid"])
        ofs_image = Image(photo_id, "Photo for " + data["uid"], photo_file, content_type="image/jpeg")
        portal = getToolByName(self.context, "portal_url").getPortalObject()
        config = self.settings
        try:
            path = config["photo_ofs_directory"]
            container = portal.unrestrictedTraverse(path)
        except AttributeError:
            msg = (
                "No valid Plone folder provided for"
                "collective.portlet.contact > LDAP backend & "
                "OFS backend for the photo"
            )
            self.context.plone_log(msg)
            container = None
        except KeyError:
            msg = (
                "No valid Plone folder provided for "
                "collective.portlet.contact > LDAP backend &"
                "OFS backend for the photo"
            )
            self.context.plone_log(msg)
            container = None

        if container:
            container = portal.portlet_contact_photo
            if photo_id in container:
                del container[photo_id]
            container[photo_id] = ofs_image
            msg = _(u"The photo has been well uploaded")
            IStatusMessage(self.request).addStatusMessage(msg)
        else:
            msg = _(u"The photo can't be uploaded")
            IStatusMessage(self.request).addStatusMessage(msg)

        url = self.context.absolute_url() + "/view"
        self.request.response.redirect(url)
    def setPhoto_ldap(self, data):
        photo = data['photo']
        #check the content type of the uploaded file
        content_type = self.widgets['photo'].value.headers['content-type']
        if content_type != 'image/jpeg':
            error = interface.Invalid(_(u'Is not a JPEG photo'))
            raise form.interfaces.WidgetActionExecutionError('photo', error)
        #try to set the photo to the contact
        try:
            _setPhoto_ldap(self.context, data['uid'], photo)
        except ldap.SERVER_DOWN:
            error = interface.Invalid(_(u"The LDAP server is unreacheable"))
            raise form.interfaces.ActionExecutionError(error)
        except ldap.INVALID_CREDENTIALS:
            msg = _("The LDAP authentication credentials are invalid")
            error = interface.Invalid(msg)
            raise form.interfaces.ActionExecutionError()
        except ldap.NO_SUCH_OBJECT:
            msg = _("The entry you have specified is not in the LDAP")
            error = interface.Invalid(msg)
            raise form.interfaces.ActionExecutionError(error)
        except ldap.INSUFFICIENT_ACCESS:
            msg = _(u"The LDAP credentials provided has not the modify\
                permission.")
            error = interface.Invalid(msg)
            raise form.interfaces.ActionExecutionError(error)

        msg = _(u"The photo has been well uploaded")
        IStatusMessage(self.request).addStatusMessage(msg)
        self.request.response.redirect(self.context.absolute_url() + '/view')
 def label(self):
     mapping = {"cn": self.request.get("cn", "")}
     return _(u"Contact's photo form ${cn}", mapping=mapping)
 def handle_cancel_action(self, action, data):
     IStatusMessage(self.request).addStatusMessage(_("Changes canceled."),
                                                   type="info")
     url = getMultiAdapter((self.context, self.request),
                           name='absolute_url')()
     self.request.response.redirect(url + '/plone_control_panel')