Example #1
0
    def getExistingValue(self, field, userkey=None, raw=False):
        """ get a previous submission value for the given field
        raw - set to False to skip post-processing of values
        """
        if userkey is None:
            userkey = self.getUserKey()
        else:
            # permissions check
            self.checkUserKey(userkey)

        for adapterId in self.getRawActionAdapter():
            actionAdapter = getattr(self.aq_explicit, adapterId, None)
            try:
                statefulAdapter = IStatefulActionAdapter(actionAdapter)
            except TypeError:
                # does not support state
                continue
            value = statefulAdapter.getExistingValueFor(
                field,
                userkey,
            )
            if raw:
                return value
            elif value is not None:
                if isinstance(value, unicode):
                    # pfg doesn't want unicode, thanks
                    value = value.encode('utf-8')
                if value and isinstance(field, FGEncryptedStringField):
                    # replace with marker
                    value = ENCRYPTED_VALUE_MARKER
                return value
Example #2
0
    def hasExistingValues(self, userkey=None):
        """ has the current user previously submitted this form? """
        if userkey is None:
            userkey = self.getUserKey()
        else:
            # permissions check
            self.checkUserKey(userkey)

        for adapterId in self.getRawActionAdapter():
            actionAdapter = getattr(self.aq_explicit, adapterId, None)
            try:
                statefulAdapter = IStatefulActionAdapter(actionAdapter)
            except TypeError:
                # does not support state
                continue
            return statefulAdapter.hasExistingValuesFor(userkey)
Example #3
0
    def fgProcessActionAdapters(self, errors, fields=None, REQUEST=None):
        if fields is None:
            fields = [fo for fo in self._getFieldObjects()
                      if not IField.providedBy(fo)]

        if not errors:
            if self.getRawAfterValidationOverride():
                # evaluate the override.
                # In case we end up traversing to a template,
                # we need to make sure we don't clobber
                # the expression context.
                self.getAfterValidationOverride()
                self.cleanExpressionContext(request=self.REQUEST)

            # get a list of adapters with no duplicates, retaining order
            adapters = []
            for adapter in self.getRawActionAdapter():
                if adapter not in adapters:
                    adapters.append(adapter)

            for adapter in adapters:
                actionAdapter = getattr(self.aq_explicit, adapter, None)
                if actionAdapter is None:
                    logger.warn(
                      "Designated action adapter '%s' is missing; ignored. "
                      "Removing it from active list." %
                      adapter)
                    self.toggleActionActive(adapter)
                else:
                    # Now, see if we should execute it.
                    # If using the 'finalise' workflow, only trigger
                    # 'save data' adapters
                    if self.getUseFinaliseButton() \
                        and 'form_finalise' not in REQUEST:
                        if not IStatefulActionAdapter.providedBy(actionAdapter):
                            # skip it
                            continue

                    # Check to see if execCondition exists and has contents
                    if safe_hasattr(actionAdapter, 'execCondition') and \
                      len(actionAdapter.getRawExecCondition()):
                        # evaluate the execCondition.
                        # create a context for expression evaluation
                        context = getExprContext(self, actionAdapter)
                        doit = actionAdapter.getExecCondition(
                          expression_context=context)
                    else:
                        # no reason not to go ahead
                        doit = True

                    if doit:
                        result = actionAdapter.onSuccess(fields, \
                                                         REQUEST=REQUEST)
                        if type(result) is type({}) and len(result):
                            # return the dict, which hopefully uses
                            # field ids or FORM_ERROR_MARKER for keys
                            return result

        return errors
Example #4
0
    def isFormFinalised(self, userkey=None):
        """ has the current user's submission been finalised
        (and therefore no longer editable) """
        if not self.useFinaliseWorkflow():
            return False

        if userkey is None:
            userkey = self.getUserKey()
        else:
            # permissions check
            self.checkUserKey(userkey)

        # ask the stateful adapters
        for adapterId in self.getRawActionAdapter():
            actionAdapter = getattr(self.aq_explicit, adapterId, None)
            try:
                statefulAdapter = IStatefulActionAdapter(actionAdapter)
            except TypeError:
                # does not support state
                continue
            return statefulAdapter.isFinalised(userkey)
    def testStateful(self):
        """ test stateful support """

        # create a saver and add a record
        self.ff1.invokeFactory('FormSaveDataAdapter', 'saver')
        saver = self.ff1.saver
        self.ff1.setActionAdapter(['saver',])

        # make sure we fit the interface
        self.assertTrue(IStatefulActionAdapter.providedBy(saver))
        self.assertTrue(verifyClass(IStatefulActionAdapter, FormSaveDataAdapter))
        self.assertTrue(verifyObject(IStatefulActionAdapter, saver))

        # enable editing of submissions
        self.ff1.setAllowEditPrevious(True)

        # no data to start with
        self.assertFalse(self.ff1.hasExistingValues())
        self.assertFalse(self.ff1.getExistingValue(self.ff1.topic))

        request = FakeRequest(topic='test subject',
                              replyto='*****@*****.**',
                              comments='test comments')
        errors = self.ff1.fgvalidate(REQUEST=request)
        self.assertEqual(errors, {})

        self.assertTrue(self.ff1.hasExistingValues())
        self.assertEquals(self.ff1.getExistingValue(self.ff1.topic),
                          'test subject')

        # should also be able to directly query the saver
        userkey = self.ff1.getUserKey()
        self.assertTrue(saver.hasExistingValuesFor(userkey))
        self.assertEquals(saver.getExistingValueFor(self.ff1.replyto, userkey),
                          '*****@*****.**')

        self.logout()

        # fake user key - should not access to this
        userkey = '_fake_user_key_notused'
        self.assertRaises(Unauthorized,
                          saver.hasExistingValuesFor,
                          userkey)
        self.assertRaises(Unauthorized,
                          saver.getExistingValueFor,
                          self.ff1.topic,
                          userkey)