Ejemplo n.º 1
0
class ContactFormSchema(ContentSchema):
    recipient = colander.SchemaNode(colander.String())
    body = colander.SchemaNode(
        colander.String(),
        widget=RichTextWidget(theme='advanced', width=790, height=500),
        missing=u"",
    )
    show_attachment = colander.SchemaNode(
        colander.Boolean(),
        title=_(u"Show attachment"),
        description=_(u"If activated, the user can upload an attachment."),
        default=True,
        missing=True,
    )
Ejemplo n.º 2
0
class ContactForm(Content):

    __tablename__ = 'contact_forms'
    __mapper_args__ = dict(polymorphic_identity='contact_form')

    implements(IDefaultWorkflow)

    id = Column('id', Integer, ForeignKey('contents.id'), primary_key=True)
    recipient = Column(String(255), nullable=False)
    body = Column(Text)
    show_attachment = Column(Boolean(), nullable=False)

    type_info = Content.type_info.copy(
        name=u'ContactForm',
        title=_(u'Contact form'),
        add_view=u'add_contactform',
        addable_to=[u'Document'],
        selectable_default_views=[
            ('view-1-col', u'1 column layout'),
        ],
    )

    def __init__(self,
                 recipient=u"",
                 body=u"",
                 show_attachment=True,
                 **kwargs):

        super(ContactForm, self).__init__(**kwargs)
        self.recipient = recipient
        self.body = body
        self.show_attachment = show_attachment
Ejemplo n.º 3
0
 def file_size_limit(node, value):
     value['fp'].seek(0, 2)
     size = value['fp'].tell()
     value['fp'].seek(0)
     max_size = 10
     if size > max_size * 1024 * 1024:
         msg = _('Maximum file size: ${size}MB', mapping={'size': max_size})
         raise colander.Invalid(node, msg)
Ejemplo n.º 4
0
 def file_size_limit(node, value):
     value['fp'].seek(0, 2)
     size = value['fp'].tell()
     value['fp'].seek(0)
     max_size = 10
     if size > max_size * 1024 * 1024:
         msg = _('Maximum file size: ${size}MB', mapping={'size': max_size})
         raise colander.Invalid(node, msg)
Ejemplo n.º 5
0
    def deserialize(self, field, pstruct):

        from kotti_settings.util import get_setting

        if pstruct is null:
            return null

        challenge = pstruct.get('recaptcha_challenge_field') or ''
        if not challenge:
            raise Invalid(field.schema, 'Missing challenge')

        response = pstruct.get('recaptcha_response_field') or ''
        if not response:
            raise Invalid(field.schema, 'No input')

        privatekey = get_setting('private_key')
        remoteip = self.request.remote_addr
        data = urlencode(dict(privatekey=privatekey,
                              remoteip=remoteip,
                              challenge=challenge,
                              response=response))
        h = httplib2.Http(timeout=10)
        try:
            resp, content = h.request(self.url, "POST", headers=self.headers,
                                      body=data)
        except AttributeError as e:
            if e == "'NoneType' object has no attribute 'makefile'":
                ## XXX: catch a possible httplib regression in 2.7 where
                ## XXX: there is no connextion made to the socker so
                ## XXX sock is still None when makefile is called.
                raise Invalid(field.schema,
                              _("Could not connect to the captcha service."))

        if not resp['status'] == '200':
            raise Invalid(field.schema,
                          "There was an error talking to the recaptcha \
                          server{0}".format(resp['status']))

        valid, reason = content.split('\n')

        if not valid == 'true':
            if reason == 'incorrect-captcha-sol':
                reason = _(u"Incorrect solution")
            raise Invalid(field.schema, reason.replace('\\n', ' ').strip("'"))

        return reason
Ejemplo n.º 6
0
 def file_size_limit(node, value):
     value["fp"].seek(0, 2)
     size = value["fp"].tell()
     value["fp"].seek(0)
     max_size = 10
     if size > max_size * 1024 * 1024:
         msg = _("Maximum file size: ${size}MB", mapping={"size": max_size})
         raise colander.Invalid(node, msg)
Ejemplo n.º 7
0
def view_contactform(context, request):

    locale_name = get_locale_name(request)

    tmpstore = FileUploadTempStore(request)

    def file_size_limit(node, value):
        value['fp'].seek(0, 2)
        size = value['fp'].tell()
        value['fp'].seek(0)
        max_size = 10
        if size > max_size * 1024 * 1024:
            msg = _('Maximum file size: ${size}MB', mapping={'size': max_size})
            raise colander.Invalid(node, msg)

    def maybe_show_attachment(node, kw):
        if kw.get('maybe_show_attachment', True) is False:
            del node['attachment']

    class SubmissionSchema(colander.MappingSchema):

        name = colander.SchemaNode(colander.String(),
                                   title=_("Full Name"))
        sender = colander.SchemaNode(colander.String(),
                                     validator=colander.Email(),
                                     title=_("E-Mail Address"))
        subject = colander.SchemaNode(colander.String(), title=_("Subject"))
        content = colander.SchemaNode(
            colander.String(),
            widget=TextAreaWidget(cols=40, rows=5),
            title=_("Your message")
        )
        attachment = colander.SchemaNode(
            FileData(),
            title=_('Attachment'),
            widget=FileUploadWidget(tmpstore),
            validator=file_size_limit,
            missing=None,
        )
        _LOCALE_ = colander.SchemaNode(
            colander.String(),
            widget=HiddenWidget(),
            default=locale_name)

    schema = SubmissionSchema(after_bind=maybe_show_attachment)
    schema = schema.bind(maybe_show_attachment=context.show_attachment)
    form = Form(schema, buttons=[Button('submit', _('Submit'))])
    appstruct = None
    rendered_form = None
    if 'submit' in request.POST:
        controls = request.POST.items()
        try:
            appstruct = form.validate(controls)
            mail_submission(context, request, appstruct)
        except ValidationFailure, e:
            appstruct = None
            rendered_form = e.render()
Ejemplo n.º 8
0
 class SubmissionSchema(colander.MappingSchema):
     name = colander.SchemaNode(colander.String(), title=_("Full Name"))
     sender = colander.SchemaNode(colander.String(),
                                  validator=colander.Email(),
                                  title=_("E-Mail Address"))
     subject = colander.SchemaNode(colander.String(), title=_("Subject"))
     content = colander.SchemaNode(colander.String(),
                                   widget=TextAreaWidget(cols=40, rows=5),
                                   title=_("Your message"))
     attachment = colander.SchemaNode(
         FileData(),
         title=_('Attachment'),
         widget=FileUploadWidget(tmpstore),
         validator=file_size_limit,
         missing=None,
     )
     _LOCALE_ = colander.SchemaNode(colander.String(),
                                    widget=HiddenWidget(),
                                    default=locale_name)
Ejemplo n.º 9
0
def view_contactform(context, request):

    locale_name = get_locale_name(request)

    tmpstore = FileUploadTempStore(request)

    def file_size_limit(node, value):
        value['fp'].seek(0, 2)
        size = value['fp'].tell()
        value['fp'].seek(0)
        max_size = 10
        if size > max_size * 1024 * 1024:
            msg = _('Maximum file size: ${size}MB', mapping={'size': max_size})
            raise colander.Invalid(node, msg)

    def maybe_show_attachment(node, kw):
        if kw.get('maybe_show_attachment', True) is False:
            del node['attachment']

    class SubmissionSchema(colander.MappingSchema):

        name = colander.SchemaNode(colander.String(), title=_("Full Name"))
        sender = colander.SchemaNode(colander.String(),
                                     validator=colander.Email(),
                                     title=_("E-Mail Address"))
        subject = colander.SchemaNode(colander.String(), title=_("Subject"))
        content = colander.SchemaNode(colander.String(),
                                      widget=TextAreaWidget(cols=40, rows=5),
                                      title=_("Your message"))
        attachment = colander.SchemaNode(
            FileData(),
            title=_('Attachment'),
            widget=FileUploadWidget(tmpstore),
            validator=file_size_limit,
            missing=None,
        )
        _LOCALE_ = colander.SchemaNode(colander.String(),
                                       widget=HiddenWidget(),
                                       default=locale_name)

    schema = SubmissionSchema(after_bind=maybe_show_attachment)
    schema = schema.bind(maybe_show_attachment=context.show_attachment)
    form = Form(schema, buttons=[Button('submit', _('Submit'))])
    appstruct = None
    rendered_form = None
    if 'submit' in request.POST:
        controls = request.POST.items()
        try:
            appstruct = form.validate(controls)
            mail_submission(context, request, appstruct)
        except ValidationFailure, e:
            appstruct = None
            rendered_form = e.render()
Ejemplo n.º 10
0
class ContactformAddForm(AddFormView):
    schema_factory = ContactFormSchema
    add = ContactForm
    item_type = _(u"Contact Form")
Ejemplo n.º 11
0
    name = 'public_key'
    title = _(u'Public key')
    description = _(u'Your public key.')
    missing = u''
    default = u''


class PrivateKeySchemaNode(colander.SchemaNode):
    name = 'private_key'
    title = _(u'Private key')
    description = _(u'Your private key.')
    missing = u''
    default = u''


recaptcha_themes = ((u'red', _(u'Red')),
                    (u'white', _(u'White')),
                    (u'blackglass', _(u'Blackglass')),
                    (u'clean', _(u'Clean')))


class RecaptchaThemeSchemaNode(colander.SchemaNode):
    name = 'recaptcha_theme'
    title = _(u'reCaptcha theme')
    default = u'red'
    widget = deform.widget.SelectWidget(values=recaptcha_themes)


class ContactFormSchema(colander.MappingSchema):
    default_sender = DefaultSenderAddressNode(colander.String())
    show_captcha = ShowCaptchaSchemaNode(colander.Boolean())