Exemplo n.º 1
0
class ObjectFieldWidget(FieldWidget):
    grok.adapts(ObjectField, Interface, Interface)

    template = getTemplate('objectfieldwidget.cpt')
    
    def prepareContentValue(self, value):
        if value is NO_VALUE:
            return {self.identifier: []}
        return {self.identifier: value}

    def update(self):
        super(ObjectFieldWidget, self).update()
        value = self.component.dataManager(self.inputValue())
        form = cloneFormData(self.form, value, self.identifier)
        self.objectWidgets = Widgets(form=form, request=self.request)
        self.objectWidgets.extend(self.component.objectFields)
        self.objectWidgets.update()
Exemplo n.º 2
0
class FormCanvas(FormData):
    """This represent a simple form setup: setup some fields and
    actions, prepare widgets for it.
    """
    label = u''
    description = u''

    actions = Actions()
    fields = Fields()

    __component_name__ = ''
    template = default_template

    @property
    def action_url(self):
        url = IURL(self.context, self.request, default=None)
        if url is not None:
            return u"%s/%s" % (url, self.__component_name__)
        return u""

    def __init__(self, context, request, **kwargs):
        super(FormCanvas, self).__init__(context, request, **kwargs)
        self.actionWidgets = Widgets(form=self, request=self.request)
        self.fieldWidgets = Widgets(form=self, request=self.request)
        self._updated = False

    @property
    def translate(self):
        localizer = getLocalizer()
        if localizer is not None:
            return localizer.translate
        return None

    def update(self, *args, **kwargs):
        pass

    def namespace(self):
        namespace = {}
        namespace['context'] = self.context
        namespace['request'] = self.request
        namespace['form'] = self
        namespace['view'] = self
        return namespace

    def extractData(self, fields=None):
        if fields is None:
            fields = self.fields
        return super(FormCanvas, self).extractData(fields)

    def haveRequiredFields(self):
        for field in self.fields:
            if field.required:
                return True
        return False

    def updateActions(self):
        action, result = self.actions.process(self, self.request)
        if ISuccessMarker.providedBy(result) and result.url is not None:
            code = result.code or 302
            exception = REDIRECTIONS[code]
            raise exception(result.url)
        return action, result

    def updateWidgets(self):
        self.fieldWidgets.extend(self.fields)
        self.actionWidgets.extend(self.actions)

        self.fieldWidgets.update()
        self.actionWidgets.update()

    def render(self, *args, **kwargs):
        """This is the default render method.
        Not providing a template will make it fails.
        Override this method, if needed (eg: return a string)
        """
        if self.template is None:
            raise NotImplementedError("Template is not defined.")
        return self.template.render(
            self, translate=self.translate, **self.namespace())
Exemplo n.º 3
0
class FormCanvas(FormData):
    """This represent a simple form setup: setup some fields and
    actions, prepare widgets for it.
    """
    grok.baseclass()

    label = u''
    description = u''

    actions = Actions()
    fields = Fields()

    protected = False
    csrftoken = None
    
    __component_name__ = ''
    template = default_template

    @property
    def action_url(self):
        url = queryMultiAdapter((self.context, self.request), IURL)
        if url is not None:
            return u"%s/%s" % (url, self.__component_name__)
        return u""

    def __init__(self, context, request):
        super(FormCanvas, self).__init__(context, request)
        self.actionWidgets = Widgets(form=self, request=self.request)
        self.fieldWidgets = Widgets(form=self, request=self.request)
        self._updated = False

    def setUpToken(self):
        session = getSession()
        if session is None:
            raise CSRFTokenGenerationError("No session.")
        self.csrftoken = session.get('__csrftoken__')
        if self.csrftoken is None:
            self.csrftoken = str(binascii.hexlify(urandom(32)))
            session['__csrftoken__'] = self.csrftoken
        
    def checkToken(self):
        session = getSession()
        if session is None:
            raise CSRFTokenGenerationError("No session.")
        cookietoken = session.get('__csrftoken__')
        if cookietoken is None:
            raise InvalidCSRFToken(_('Invalid CSRF token'))
        if cookietoken != self.request.form.get('form.field.__csrftoken__', None):
            raise InvalidCSRFToken(_('Invalid CSRF token'))

    @property
    def target_language(self):
        return ILanguage(self.request, None)

    def get_csrftoken(self):
        return unicode(self.csrftoken)

    def update(self, *args, **kwargs):
        if self.protected:
            from dolmen.forms.base.fields import Field
            from zope import schema, interface
            class ICSRF(interface.Interface):
                __csrftoken__ = schema.TextLine(title=u'csrf', defaultFactory=self.get_csrftoken)
            self.setUpToken()
            self.fields.extend(Fields(ICSRF))
            self.fields['__csrftoken__'].mode = 'hidden'

    def namespace(self):
        namespace = {}
        namespace['context'] = self.context
        namespace['request'] = self.request
        namespace['view'] = self
        return namespace

    def extractData(self, fields=None):
        if fields is None:
            fields = self.fields
        return super(FormCanvas, self).extractData(fields)

    def haveRequiredFields(self):
        return reduce(
            operator.or_,
            [False] + map(operator.attrgetter('required'), self.fields))

    def updateActions(self):
        action, result = self.actions.process(self, self.request)
        if action is not None and self.protected:
            # This form has CSRF protection enabled.
            self.checkToken()

        if ISuccessMarker.providedBy(result) and result.url is not None:
            code = result.code or 302
            exception = REDIRECTIONS[code]
            raise exception(result.url)
        return action, result

    def updateWidgets(self):
        self.fieldWidgets.extend(self.fields)
        self.actionWidgets.extend(self.actions)

        self.fieldWidgets.update()
        self.actionWidgets.update()

    def render(self, *args, **kwargs):
        """This is the default render method.
        Not providing a template will make it fails.
        Override this method, if needed (eg: return a string)
        """
        if self.template is None:
            raise NotImplementedError("Template is not defined.")
        return self.template.render(
            self, target_language=self.target_language, **self.namespace())