def get_header_info(self, fields, request, context, from_addr=None, to_addr=None, subject=None): """Return header info header info is a dictionary Keyword arguments: request -- (optional) alternate request object to use """ portal = getToolByName(context, 'portal_url').getPortalObject() utils = getToolByName(context, 'plone_utils') (to, from_addr, reply) = self.get_addresses(fields, request, context) subject = self.get_subject(fields, request, context) headerinfo = OrderedDict() headerinfo['To'] = self.secure_header_line(to) headerinfo['From'] = self.secure_header_line(from_addr) if reply: headerinfo['Reply-To'] = self.secure_header_line(reply) # transform subject into mail header encoded string email_charset = portal.getProperty('email_charset', 'utf-8') if not isinstance(subject, unicode): site_charset = utils.getSiteEncoding() subject = unicode(subject, site_charset, 'replace') msgSubject = self.secure_header_line(subject).encode( email_charset, 'replace') msgSubject = str(Header(msgSubject, email_charset)) headerinfo['Subject'] = msgSubject # CC cc_recips = filter(None, self.cc_recipients) if hasattr(self, 'ccOverride') and self.ccOverride and get_expression( context, self.ccOverride): cc_recips = get_expression(context, self.ccOverride) if cc_recips: headerinfo['Cc'] = self._destFormat(cc_recips) # BCC bcc_recips = filter(None, self.bcc_recipients) if hasattr(self, 'bccOverride') and self.bccOverride and get_expression( context, self.bccOverride): bcc_recips = get_expression(context, self.bccOverride) if bcc_recips: headerinfo['Bcc'] = self._destFormat(bcc_recips) for key in getattr(self, 'xinfo_headers', []): headerinfo['X-{0}'.format(key)] = self.secure_header_line( request.get(key, 'MISSING')) return headerinfo
def get_addresses(self, fields, request, context, from_addr=None, to_addr=None): """Return addresses """ pprops = getToolByName(context, 'portal_properties') site_props = getToolByName(pprops, 'site_properties') portal = getToolByName(context, 'portal_url').getPortalObject() pms = getToolByName(context, 'portal_membership') # get Reply-To reply_addr = None if hasattr(self, 'replyto_field'): reply_addr = fields.get(self.replyto_field, None) # Get From address if hasattr(self, 'senderOverride') and self.senderOverride and get_expression(context, self.senderOverride): from_addr = get_expression(context, self.senderOverride).strip() else: from_addr = from_addr or site_props.getProperty('email_from_address') or \ portal.getProperty('email_from_address') # Get To address and full name if hasattr(self, 'recipientOverride') and self.recipientOverride and get_expression(context, self.recipientOverride): recip_email = get_expression(context, self.recipientOverride) else: recip_email = None if hasattr(self, 'to_field') and self.to_field: recip_email = fields.get(self.to_field, None) if not recip_email: recip_email = self.recipient_email recip_email = self._destFormat(recip_email) recip_name = self.recipient_name.encode('utf-8') # if no to_addr and no recip_email specified, use owner adress if possible. # if not, fall back to portal email_from_address. # if still no destination, raise an assertion exception. if not recip_email and not to_addr: ownerinfo = context.getOwner() ownerid = ownerinfo.getId() fullname = ownerid userdest = pms.getMemberById(ownerid) if userdest is not None: fullname = userdest.getProperty('fullname', ownerid) toemail = '' if userdest is not None: toemail = userdest.getProperty('email', '') if not toemail: toemail = portal.getProperty('email_from_address') assert toemail, """ Unable to mail form input because no recipient address has been specified. Please check the recipient settings of the EasyForm "Mailer" within the current form folder. """ to = formataddr((fullname, toemail)) else: to = to_addr or formataddr((recip_name, recip_email)) return (to, from_addr, reply_addr)
def get_header_info(self, fields, request, context, from_addr=None, to_addr=None, subject=None): """Return header info header info is a dictionary Keyword arguments: request -- (optional) alternate request object to use """ portal = getToolByName(context, 'portal_url').getPortalObject() utils = getToolByName(context, 'plone_utils') (to, from_addr, reply) = self.get_addresses(fields, request, context) subject = self.get_subject(fields, request, context) headerinfo = OrderedDict() headerinfo['To'] = self.secure_header_line(to) headerinfo['From'] = self.secure_header_line(from_addr) if reply: headerinfo['Reply-To'] = self.secure_header_line(reply) # transform subject into mail header encoded string email_charset = portal.getProperty('email_charset', 'utf-8') if not isinstance(subject, unicode): site_charset = utils.getSiteEncoding() subject = unicode(subject, site_charset, 'replace') msgSubject = self.secure_header_line( subject).encode(email_charset, 'replace') msgSubject = str(Header(msgSubject, email_charset)) headerinfo['Subject'] = msgSubject # CC cc_recips = filter(None, self.cc_recipients) if hasattr(self, 'ccOverride') and self.ccOverride: _cc = get_expression(context, self.ccOverride, fields=fields) if _cc: cc_recips = _cc if cc_recips: headerinfo['Cc'] = format_addresses(cc_recips) # BCC bcc_recips = filter(None, self.bcc_recipients) if hasattr(self, 'bccOverride') and self.bccOverride: _bcc = get_expression(context, self.bccOverride, fields=fields) if _bcc: bcc_recips = _bcc if bcc_recips: headerinfo['Bcc'] = format_addresses(bcc_recips) for key in getattr(self, 'xinfo_headers', []): headerinfo['X-{0}'.format(key)] = self.secure_header_line( request.get(key, 'MISSING')) return headerinfo
def get_addresses( self, fields, request, context, from_addr=None, to_addr=None ): """ Return addresses """ # get Reply-To reply_addr = None if hasattr(self, 'replyto_field'): reply_addr = fields.get(self.replyto_field, None) # Get From address portal_addr = self.get_portal_email_address(context) from_addr = from_addr or portal_addr if hasattr(self, 'senderOverride') and self.senderOverride: _from = get_expression(context, self.senderOverride, fields=fields) if _from: from_addr = _from # Get To address and full name recip_email = None if hasattr(self, 'to_field') and self.to_field: recip_email = fields.get(self.to_field, None) if not recip_email: if self.recipient_email != '': recip_email = self.recipient_email if hasattr(self, 'recipientOverride') and self.recipientOverride: _recip = get_expression( context, self.recipientOverride, fields=fields ) if _recip: recip_email = _recip to = None if to_addr: to = format_addresses(to_addr) elif recip_email: to = format_addresses( recip_email, self.recipient_name ) else: # Use owner adress or fall back to portal email_from_address. to = formataddr(self.get_owner_info(context)) assert(to) return (to, from_addr, reply_addr)
def get_header_info(self, fields, request, context, from_addr=None, to_addr=None, subject=None): """Return header info header info is a dictionary Keyword arguments: request -- (optional) alternate request object to use """ portal = getToolByName(context, "portal_url").getPortalObject() utils = getToolByName(context, "plone_utils") (to, from_addr, reply) = self.get_addresses(fields, request, context) subject = self.get_subject(fields, request, context) headerinfo = OrderedDict() headerinfo["To"] = self.secure_header_line(to) headerinfo["From"] = self.secure_header_line(from_addr) if reply: headerinfo["Reply-To"] = self.secure_header_line(reply) # transform subject into mail header encoded string email_charset = portal.getProperty("email_charset", "utf-8") if not isinstance(subject, unicode): site_charset = utils.getSiteEncoding() subject = unicode(subject, site_charset, "replace") msgSubject = self.secure_header_line(subject).encode(email_charset, "replace") msgSubject = str(Header(msgSubject, email_charset)) headerinfo["Subject"] = msgSubject # CC cc_recips = filter(None, self.cc_recipients) if hasattr(self, "ccOverride") and self.ccOverride: _cc = get_expression(context, self.ccOverride, fields=fields) if _cc: cc_recips = _cc if cc_recips: headerinfo["Cc"] = format_addresses(cc_recips) # BCC bcc_recips = filter(None, self.bcc_recipients) if hasattr(self, "bccOverride") and self.bccOverride: _bcc = get_expression(context, self.bccOverride, fields=fields) if _bcc: bcc_recips = _bcc if bcc_recips: headerinfo["Bcc"] = format_addresses(bcc_recips) for key in getattr(self, "xinfo_headers", []): headerinfo["X-{0}".format(key)] = self.secure_header_line(request.get(key, "MISSING")) return headerinfo
def get_addresses(self, fields, request, context, from_addr=None, to_addr=None): """Return addresses """ pprops = getToolByName(context, 'portal_properties') site_props = getToolByName(pprops, 'site_properties') portal = getToolByName(context, 'portal_url').getPortalObject() # get Reply-To reply_addr = None if hasattr(self, 'replyto_field'): reply_addr = fields.get(self.replyto_field, None) # Get From address from_addr = ( from_addr or site_props.getProperty('email_from_address') or portal.getProperty('email_from_address') ) if hasattr(self, 'senderOverride') and self.senderOverride: _from = get_expression(context, self.senderOverride, fields=fields) if _from: from_addr = _from # Get To address and full name recip_email = None if hasattr(self, 'to_field') and self.to_field: recip_email = fields.get(self.to_field, None) if not recip_email: recip_email = self.recipient_email if hasattr(self, 'recipientOverride') and self.recipientOverride: _recip = get_expression( context, self.recipientOverride, fields=fields ) if _recip: recip_email = _recip recip_email = self._destFormat(recip_email) recip_name = self.recipient_name.encode('utf-8') # if no to_addr and no recip_email specified, use owner adress if possible. # if not, fall back to portal email_from_address. # if still no destination, raise an assertion exception. if not recip_email and not to_addr: self.get_owner_info(context) to = formataddr(self.get_owner_info(context)) else: to = to_addr or formataddr((recip_name, recip_email)) return (to, from_addr, reply_addr)
def get_addresses(self, fields, request, context, from_addr=None, to_addr=None): """ Return addresses """ pprops = getToolByName(context, 'portal_properties') site_props = getToolByName(pprops, 'site_properties') portal = getToolByName(context, 'portal_url').getPortalObject() registry = getUtility(IRegistry) # get Reply-To reply_addr = None if hasattr(self, 'replyto_field'): reply_addr = fields.get(self.replyto_field, None) # Get From address from_addr = (from_addr or site_props.getProperty('email_from_address') or portal.getProperty('email_from_address') or registry.get('plone.email_from_address')) if hasattr(self, 'senderOverride') and self.senderOverride: _from = get_expression(context, self.senderOverride, fields=fields) if _from: from_addr = _from # Get To address and full name recip_email = None if hasattr(self, 'to_field') and self.to_field: recip_email = fields.get(self.to_field, None) if not recip_email: if self.recipient_email != '': recip_email = self.recipient_email if hasattr(self, 'recipientOverride') and self.recipientOverride: _recip = get_expression( context, self.recipientOverride, fields=fields) if _recip: recip_email = _recip to = None if to_addr: to = format_addresses(to_addr) elif recip_email: to = format_addresses(recip_email, self.recipient_name) else: # Use owner adress or fall back to portal email_from_address. to = formataddr(self.get_owner_info(context)) assert (to) return (to, from_addr, reply_addr)
def get_addresses(self, fields, request, context, from_addr=None, to_addr=None): """Return addresses """ pprops = getToolByName(context, 'portal_properties') site_props = getToolByName(pprops, 'site_properties') portal = getToolByName(context, 'portal_url').getPortalObject() # get Reply-To reply_addr = None if hasattr(self, 'replyto_field'): reply_addr = fields.get(self.replyto_field, None) # Get From address from_addr = (from_addr or site_props.getProperty('email_from_address') or portal.getProperty('email_from_address')) if hasattr(self, 'senderOverride') and self.senderOverride: _from = get_expression(context, self.senderOverride, fields=fields) if _from: from_addr = _from # Get To address and full name recip_email = None if hasattr(self, 'to_field') and self.to_field: recip_email = fields.get(self.to_field, None) if not recip_email: recip_email = self.recipient_email if hasattr(self, 'recipientOverride') and self.recipientOverride: _recip = get_expression(context, self.recipientOverride, fields=fields) if _recip: recip_email = _recip recip_email = self._destFormat(recip_email) recip_name = self.recipient_name.encode('utf-8') # if no to_addr and no recip_email specified, use owner adress if possible. # if not, fall back to portal email_from_address. # if still no destination, raise an assertion exception. if not recip_email and not to_addr: self.get_owner_info(context) to = formataddr(self.get_owner_info(context)) else: to = to_addr or formataddr((recip_name, recip_email)) return (to, from_addr, reply_addr)
def get_header_info(self, fields, request, context, from_addr=None, to_addr=None, subject=None): """Return header info. header info is a dictionary Keyword arguments: request -- (optional) alternate request object to use """ (to, from_addr, reply) = self.get_addresses(fields, request, context) headerinfo = OrderedDict() headerinfo["To"] = self.secure_header_line(to) headerinfo["From"] = self.secure_header_line(from_addr) if reply: headerinfo["Reply-To"] = self.secure_header_line(reply) headerinfo["Subject"] = self.get_subject(fields, request, context) # CC if isinstance(self.cc_recipients, six.string_types): cc_recips = self.cc_recipients else: cc_recips = [_f for _f in self.cc_recipients if _f] if hasattr(self, "ccOverride") and self.ccOverride: _cc = get_expression(context, self.ccOverride, fields=fields) if _cc: cc_recips = _cc if cc_recips: headerinfo["Cc"] = format_addresses(cc_recips) # BCC if isinstance(self.bcc_recipients, six.string_types): bcc_recips = self.bcc_recipients else: bcc_recips = [_f for _f in self.bcc_recipients if _f] if hasattr(self, "bccOverride") and self.bccOverride: _bcc = get_expression(context, self.bccOverride, fields=fields) if _bcc: bcc_recips = _bcc if bcc_recips: headerinfo["Bcc"] = format_addresses(bcc_recips) for key in getattr(self, "xinfo_headers", []): headerinfo["X-{0}".format(key)] = self.secure_header_line( request.get(key, "MISSING")) return headerinfo
def get_addresses(self, fields, request, context, from_addr=None, to_addr=None): """Return addresses """ pprops = getToolByName(context, 'portal_properties') site_props = getToolByName(pprops, 'site_properties') portal = getToolByName(context, 'portal_url').getPortalObject() # get Reply-To reply_addr = None if hasattr(self, 'replyto_field'): reply_addr = fields.get(self.replyto_field, None) # Get From address from_addr = (from_addr or site_props.getProperty('email_from_address') or portal.getProperty('email_from_address')) if hasattr(self, 'senderOverride') and self.senderOverride: _from = get_expression(context, self.senderOverride, fields=fields) if _from: from_addr = _from # Get To address and full name recip_email = None if hasattr(self, 'to_field') and self.to_field: recip_email = fields.get(self.to_field, None) if not recip_email: recip_email = self.recipient_email if hasattr(self, 'recipientOverride') and self.recipientOverride: _recip = get_expression(context, self.recipientOverride, fields=fields) if _recip: recip_email = _recip to = None if to_addr: to = format_addresses(to_addr) elif recip_email: to = format_addresses(recip_email, self.recipient_name) else: # Use owner adress or fall back to portal email_from_address. to = formataddr(self.get_owner_info(context)) assert (to) return (to, from_addr, reply_addr)
def get_header_info(self, fields, request, context, from_addr=None, to_addr=None, subject=None): """Return header info header info is a dictionary Keyword arguments: request -- (optional) alternate request object to use """ (to, from_addr, reply) = self.get_addresses(fields, request, context) headerinfo = OrderedDict() headerinfo['To'] = self.secure_header_line(to) headerinfo['From'] = self.secure_header_line(from_addr) if reply: headerinfo['Reply-To'] = self.secure_header_line(reply) headerinfo['Subject'] = self.get_subject(fields, request, context) # CC cc_recips = filter(None, self.cc_recipients) if hasattr(self, 'ccOverride') and self.ccOverride: _cc = get_expression(context, self.ccOverride, fields=fields) if _cc: cc_recips = _cc if cc_recips: headerinfo['Cc'] = format_addresses(cc_recips) # BCC bcc_recips = filter(None, self.bcc_recipients) if hasattr(self, 'bccOverride') and self.bccOverride: _bcc = get_expression(context, self.bccOverride, fields=fields) if _bcc: bcc_recips = _bcc if bcc_recips: headerinfo['Bcc'] = format_addresses(bcc_recips) for key in getattr(self, 'xinfo_headers', []): headerinfo['X-{0}'.format(key)] = self.secure_header_line( request.get(key, 'MISSING')) return headerinfo
def validate(self, value): """Validate field by TValidator """ # By default this will call SimpleFieldValidator.validator but allows for a fields # custom validation adaptor to also be called such as recaptcha _, _, view_interface, _, _ = self.__class__.__component_adapts__ validator = superAdapter( view_interface, self, (self.context, self.request, self.view, self.field, self.widget), ) if validator is not None: validator.validate(value) efield = IFieldExtender(self.field) validators = getattr(efield, "validators", []) if validators: for validator in validators: vmethod = queryUtility(IFieldValidator, name=validator) if not vmethod: continue res = vmethod(value) if res: raise Invalid(res) TValidator = getattr(efield, "TValidator", None) if TValidator: try: cerr = get_expression(self.context, TValidator, value=value) except Exception as e: raise Invalid(e) if cerr: raise Invalid(cerr)
def handleSubmit(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return data = self.updateServerSideData(data) errors = self.processActions(data) if errors: return self.setErrorsMessage(errors) thanksPageOverride = self.context.thanksPageOverride if thanksPageOverride: thanksPageOverrideAction = self.context.thanksPageOverrideAction thanksPage = get_expression(self.context, thanksPageOverride) if thanksPageOverrideAction == 'redirect_to': self.request.response.redirect(thanksPage) elif thanksPageOverrideAction == 'traverse_to': thanksPage = self.context.restrictedTraverse( thanksPage.encode('utf-8')) thanksPage = mapply( thanksPage, self.request.args, self.request).encode('utf-8') self.request.response.write(thanksPage) else: self.thanksPage = True replacer = DollarVarReplacer(data).sub self.thanksPrologue = self.context.thanksPrologue and replacer( self.context.thanksPrologue.output) self.thanksEpilogue = self.context.thanksEpilogue and replacer( self.context.thanksEpilogue.output) if not self.context.showAll: self.fields = self.setThanksFields(self.base_fields) for group in self.groups: group.fields = self.setThanksFields( self.base_groups.get(group.label)) self.setDisplayMode(DISPLAY_MODE) self.updateActions()
def handleSubmit(self, action): unsorted_data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return unsorted_data = self.updateServerSideData(unsorted_data) errors = self.processActions(unsorted_data) if errors: return self.setErrorsMessage(errors) data = OrderedDict([ x for x in getFieldsInOrder(self.schema) if x[0] in unsorted_data ]) data.update(unsorted_data) thanksPageOverride = self.context.thanksPageOverride if thanksPageOverride: thanksPageOverrideAction = self.context.thanksPageOverrideAction thanksPage = get_expression(self.context, thanksPageOverride) if thanksPageOverrideAction == 'redirect_to': self.request.response.redirect(thanksPage) elif thanksPageOverrideAction == 'traverse_to': thanksPage = self.context.restrictedTraverse( thanksPage.encode('utf-8')) thanksPage = mapply(thanksPage, self.request.args, self.request).encode('utf-8') self.request.response.write(thanksPage) else: # we come back to the form itself. # the thanks page is handled in the __call__ method pass
def header_injection(self): tal_expression = self.context.headerInjection header_to_inject = get_expression(self.context, tal_expression) if six.PY2 and isinstance(header_to_inject, six.text_type): header_to_inject = header_to_inject.encode("utf-8") return header_to_inject
def processActions(self, fields): # get a list of adapters with no duplicates, retaining order actions = getFieldsInOrder(get_actions(self.context)) for name, action in actions: if not action.required: continue # Now, see if we should execute it. # Check to see if execCondition exists and has contents execCondition = IActionExtender(action).execCondition if execCondition: doit = get_expression(self.context, execCondition) else: doit = True if doit and hasattr(action, "onSuccess"): if IRegistrantData.providedBy(action): result = action.onSuccess( fields, self.request, max_attendees=getattr(self.context, "max_attendees", 0), waiting_list_size=getattr(self.context, "waiting_list_size", 0), ) else: result = action.onSuccess(fields, self.request) if isinstance(result, dict) and len(result): return result
def handleSubmit(self, action): unsorted_data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return unsorted_data = self.updateServerSideData(unsorted_data) errors = self.processActions(unsorted_data) if errors: return self.setErrorsMessage(errors) data = OrderedDict( [x for x in getFieldsInOrder(self.schema) if x[0] in unsorted_data] ) data.update(unsorted_data) thanksPageOverride = self.context.thanksPageOverride if not thanksPageOverride: # we come back to the form itself. # the thanks page is handled in the __call__ method return thanksPageOverrideAction = self.context.thanksPageOverrideAction thanksPage = get_expression(self.context, thanksPageOverride) if six.PY2 and isinstance(thanksPage, six.text_type): thanksPage = thanksPage.encode("utf-8") if thanksPageOverrideAction == "redirect_to": self.request.response.redirect(thanksPage) return if thanksPageOverrideAction == "traverse_to": thanksPage = self.context.restrictedTraverse(thanksPage) thanksPage = mapply(thanksPage, self.request.args, self.request) self.request.response.write(safe_encode(thanksPage))
def handleSubmit(self, action): unsorted_data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return unsorted_data = self.updateServerSideData(unsorted_data) errors = self.processActions(unsorted_data) if errors: return self.setErrorsMessage(errors) data = OrderedDict( [x for x in getFieldsInOrder(self.schema) if x[0] in unsorted_data] ) data.update(unsorted_data) thanksPageOverride = self.context.thanksPageOverride if thanksPageOverride: thanksPageOverrideAction = self.context.thanksPageOverrideAction thanksPage = get_expression(self.context, thanksPageOverride) if thanksPageOverrideAction == 'redirect_to': self.request.response.redirect(thanksPage) elif thanksPageOverrideAction == 'traverse_to': thanksPage = self.context.restrictedTraverse( thanksPage.encode('utf-8')) thanksPage = mapply( thanksPage, self.request.args, self.request ).encode('utf-8') self.request.response.write(thanksPage) else: # we come back to the form itself. # the thanks page is handled in the __call__ method pass
def get_subject(self, fields, request, context): """Return subject """ # get subject header nosubject = u'(no subject)' # TODO: translate subject = None if hasattr(self, 'subjectOverride') and self.subjectOverride: # subject has a TALES override subject = get_expression(context, self.subjectOverride, fields=fields).strip() if not subject: subject = getattr(self, 'msg_subject', nosubject) subjectField = fields.get(self.subject_field, None) if subjectField is not None: subject = subjectField else: # we only do subject expansion if there's no field chosen subject = dollar_replacer(subject, fields) if isinstance(subject, basestring): subject = safe_unicode(subject) elif subject and isinstance(subject, (set, tuple, list)): subject = ', '.join([safe_unicode(s) for s in subject]) else: subject = nosubject # transform subject into mail header encoded string email_charset = 'utf-8' msgSubject = self.secure_header_line(subject).encode( email_charset, 'replace') msgSubject = str(Header(msgSubject, email_charset)) return msgSubject
def get_subject(self, fields, request, context): """Return subject """ # get subject header nosubject = '(no subject)' if hasattr(self, 'subjectOverride') and self.subjectOverride and get_expression(context, self.subjectOverride): # subject has a TALES override subject = get_expression(context, self.subjectOverride).strip() else: subject = getattr(self, 'msg_subject', nosubject) subjectField = fields.get(self.subject_field, None) if subjectField is not None: subject = subjectField else: # we only do subject expansion if there's no field chosen subject = DollarVarReplacer(fields).sub(subject) return subject
def updateActions(self): super(EasyFormForm, self).updateActions() if 'submit' in self.actions: if self.context.submitLabelOverride: self.actions['submit'].title = get_expression( self.context, self.context.submitLabelOverride) else: self.actions['submit'].title = self.context.submitLabel if 'reset' in self.actions: self.actions['reset'].title = self.context.resetLabel
def action(self): """ Redefine <form action=''> attribute. """ action = getattr(self.context, 'formActionOverride') if action: action = get_expression(self.context, action) if not action: action = self.context.absolute_url() if self.context.forceSSL: action = action.replace('http://', 'https://') return action
def get_subject(self, fields, request, context): """Return subject """ # get subject header nosubject = '(no subject)' if hasattr( self, 'subjectOverride') and self.subjectOverride and get_expression( context, self.subjectOverride): # subject has a TALES override subject = get_expression(context, self.subjectOverride).strip() else: subject = getattr(self, 'msg_subject', nosubject) subjectField = fields.get(self.subject_field, None) if subjectField is not None: subject = subjectField else: # we only do subject expansion if there's no field chosen subject = DollarVarReplacer(fields).sub(subject) return subject
def updateServerSideData(self, data): for fname in self.schema: field = self.schema[fname] efield = IFieldExtender(field) serverSide = getattr(efield, "serverSide", False) if not serverSide: continue fdefault = field.default TDefault = getattr(efield, "TDefault", None) value = get_expression(self.context, TDefault) if TDefault else fdefault data[fname] = value return data
def setOmitFields(self, fields): omit = [] for fname in fields: field = fields[fname].field efield = IFieldExtender(field) TEnabled = getattr(efield, 'TEnabled', None) serverSide = getattr(efield, 'serverSide', False) if TEnabled and not get_expression(self.context, TEnabled) or serverSide: omit.append(fname) if omit: fields = fields.omit(*omit) return fields
def updateServerSideData(self, data): for fname in self.schema: field = self.schema[fname] efield = IFieldExtender(field) serverSide = getattr(efield, 'serverSide', False) if not serverSide: continue fdefault = field.default TDefault = getattr(efield, 'TDefault', None) value = get_expression( self.context, TDefault) if TDefault else fdefault data[fname] = value return data
def processActions(self, data): # get a list of adapters with no duplicates, retaining order actions = getFieldsInOrder(get_actions(self.context)) for name, action in actions: if not action.required: continue # Now, see if we should execute it. # Check to see if execCondition exists and has contents execCondition = IActionExtender(action).execCondition if execCondition: doit = get_expression(self.context, execCondition) else: doit = True if doit and hasattr(action, 'onSuccess'): result = action.onSuccess(data, self.request) if isinstance(result, dict) and len(result): return result
def setOmitFields(self, fields): omit = [] new_fields = [] for fname, field in fields.items(): efield = IFieldExtender(field.field) TEnabled = getattr(efield, "TEnabled", None) serverSide = getattr(efield, "serverSide", False) if TEnabled and not get_expression(self.context, TEnabled) or serverSide: omit.append(fname) if getattr(efield, "THidden", False): field.mode = HIDDEN_MODE field.field.ignoreRequest = False new_fields.append(field) fields = fields.__class__(*new_fields) if omit: fields = fields.omit(*omit) return fields
def get(self): """ get default value of field from TDefault """ efield = IFieldExtender(self.field) TDefault = getattr(efield, "TDefault", None) if TDefault: return get_expression(self.context, TDefault) # see if there is another default adapter for this field instead _, _, view_interface, _, _ = self.__class__.__component_adapts__ adapter = superAdapter( view_interface, self, (self.context, self.request, self.view, self.field, self.widget), name="default", ) if adapter is not None: return adapter.get() else: # TODO: this should have already been done by z3c.form.widget.update() so shouldn't be needed return self.field.default
def validate(self, value): """ Validate field by TValidator """ super(FieldExtenderValidator, self).validate(value) efield = IFieldExtender(self.field) validators = getattr(efield, 'validators', []) if validators: for validator in validators: vmethod = queryUtility(IFieldValidator, name=validator) if not vmethod: continue res = vmethod(value) if res: raise Invalid(res) TValidator = getattr(efield, 'TValidator', None) if TValidator: try: cerr = get_expression(self.context, TValidator, value=value) except Exception as e: raise Invalid(e) if cerr: raise Invalid(cerr)
def get_subject(self, fields, request, context): """Return subject """ # get subject header nosubject = u'(no subject)' # TODO: translate subject = None if hasattr(self, 'subjectOverride') and self.subjectOverride: # subject has a TALES override subject = get_expression( context, self.subjectOverride, fields=fields ).strip() if not subject: subject = getattr(self, 'msg_subject', nosubject) subjectField = fields.get(self.subject_field, None) if subjectField is not None: subject = subjectField else: # we only do subject expansion if there's no field chosen subject = dollar_replacer(subject, fields) if isinstance(subject, basestring): subject = safe_unicode(subject) elif subject and isinstance(subject, (set, tuple, list)): subject = ', '.join([safe_unicode(s) for s in subject]) else: subject = nosubject # transform subject into mail header encoded string email_charset = 'utf-8' msgSubject = self.secure_header_line( subject).encode(email_charset, 'replace') msgSubject = str(Header(msgSubject, email_charset)) return msgSubject
def get_addresses(self, fields, request, context, from_addr=None, to_addr=None): """Return addresses """ pprops = getToolByName(context, 'portal_properties') site_props = getToolByName(pprops, 'site_properties') portal = getToolByName(context, 'portal_url').getPortalObject() pms = getToolByName(context, 'portal_membership') # get Reply-To reply_addr = None if hasattr(self, 'replyto_field'): reply_addr = fields.get(self.replyto_field, None) # Get From address if hasattr( self, 'senderOverride') and self.senderOverride and get_expression( context, self.senderOverride): from_addr = get_expression(context, self.senderOverride).strip() else: from_addr = from_addr or site_props.getProperty('email_from_address') or \ portal.getProperty('email_from_address') # Get To address and full name if hasattr(self, 'recipientOverride' ) and self.recipientOverride and get_expression( context, self.recipientOverride): recip_email = get_expression(context, self.recipientOverride) else: recip_email = None if hasattr(self, 'to_field') and self.to_field: recip_email = fields.get(self.to_field, None) if not recip_email: recip_email = self.recipient_email recip_email = self._destFormat(recip_email) recip_name = self.recipient_name.encode('utf-8') # if no to_addr and no recip_email specified, use owner adress if possible. # if not, fall back to portal email_from_address. # if still no destination, raise an assertion exception. if not recip_email and not to_addr: ownerinfo = context.getOwner() ownerid = ownerinfo.getId() fullname = ownerid userdest = pms.getMemberById(ownerid) if userdest is not None: fullname = userdest.getProperty('fullname', ownerid) toemail = '' if userdest is not None: toemail = userdest.getProperty('email', '') if not toemail: toemail = portal.getProperty('email_from_address') assert toemail, """ Unable to mail form input because no recipient address has been specified. Please check the recipient settings of the EasyForm "Mailer" within the current form folder. """ to = formataddr((fullname, toemail)) else: to = to_addr or formataddr((recip_name, recip_email)) return (to, from_addr, reply_addr)
def get(self): """ get default value of field from TDefault """ fdefault = self.field.default efield = IFieldExtender(self.field) TDefault = getattr(efield, 'TDefault', None) return get_expression(self.context, TDefault) if TDefault else fdefault