Beispiel #1
0
    def _set_request(self, request):
        """
        Assign raw request data to the form

        :arg request_data: raw request data (e.g. request.POST)
        :type request_data: Dictionary (dotted or nested or dotted or MultiDict)
        """
        self._request = request
        request_data = getattr(request, self.method.upper())
        # Decode request data according to the request's charset.
        request_data = UnicodeMultiDict(
            request_data, encoding=util.get_post_charset(request))
        # Remove the sequence factory data from the request
        for k in request_data.keys():
            if '*' in k:
                request_data.pop(k)
        # We need the _request_data to be populated so sequences know how many
        # items they have (i.e. .fields method on a sequence uses the number of
        # values on the _request_data)

        # Convert request data to a dottedish friendly representation
        request_data = _unflatten_request_data(request_data)
        self._request_data = dotted(request_data)
        self._request_data = dotted(
            self.widget.pre_parse_incoming_request_data(
                self.structure, request_data))
Beispiel #2
0
 def test_charset_detection(self):
     tests = [
         # form field takes precedence
         (('utf-8', 'iso-8859-1'), 'utf-8'),
         # header used next
         (('', 'iso-8859-1'), 'iso-8859-1'),
         # default to utf-8
         (('', ''), 'utf-8'),
         # nonsense in form field
         (('Xuhuwef', 'utf-8'), 'utf-8'),
     ]
     for test, expected in tests:
         request = webob.Request.blank(
             '/', POST={'_charset_': test[0]},
             headers=[('Content-Type', 'application/x-www-form-urlencoded;charset=%s'%test[1])])
         self.assertEqual(util.get_post_charset(request), expected)
Beispiel #3
0
 def test_charset_detection(self):
     tests = [
         # form field takes precedence
         (('utf-8', 'iso-8859-1'), 'utf-8'),
         # header used next
         (('', 'iso-8859-1'), 'iso-8859-1'),
         # default to utf-8
         (('', ''), 'utf-8'),
         # nonsense in form field
         (('Xuhuwef', 'utf-8'), 'utf-8'),
     ]
     for test, expected in tests:
         request = webob.Request.blank(
             '/',
             POST={'_charset_': test[0]},
             headers=[
                 ('Content-Type',
                  'application/x-www-form-urlencoded;charset=%s' % test[1])
             ])
         self.assertEqual(util.get_post_charset(request), expected)
Beispiel #4
0
 def validate(self, request, failure_callable=None, success_callable=None):
     """ 
     Get the data without raising exceptions and then validate the data. If
     there are errors, raise them; otherwise return the data
     """
     # If we pass in explicit failure and success callables then do this
     # first
     if failure_callable is not None and success_callable is not None:
         return self._validate_and_call(request, \
                       failure_callable=None, success_callable=None)
     self.errors = {}
     # Check this request was POSTed by this form.
     if not request.method =='POST' and \
        request.POST.get('__formish_form__',None) == self.name:
         raise Exception("request does not match form name")
     
     request_post = UnicodeMultiDict(request.POST, \
                     encoding=util.get_post_charset(request))
     # Remove the sequence factory data from the request
     for k in request_post.keys():
         if '*' in k:
             request_post.pop(k)
     # We need the _request_data to be populated so sequences know how many
     # items they have (i.e. .fields method on a sequence uses the number of
     # values on the _request_data)
     self._request_data = dottedDict(request_post)
     self.request_data = validation.pre_parse_request_data( \
                 self.structure,dottedDict(request_post))
     data = self.get_unvalidated_data( \
                 self.request_data, raise_exceptions=False)
     #self._request_data = dottedDict(request_post)
     try:
         self.structure.attr.validate(data)
     except schemaish.attr.Invalid, e:
         for key, value in e.error_dict.items():
             if key not in self.errors:
                 self.errors[key] = value
Beispiel #5
0
    def _set_request(self, request):
        """
        Assign raw request data to the form

        :arg request_data: raw request data (e.g. request.POST)
        :type request_data: Dictionary (dotted or nested or dotted or MultiDict)
        """
        self._request = request
        request_data = getattr(request, self.method.upper())
        # Decode request data according to the request's charset.
        request_data = UnicodeMultiDict(request_data,
                                        encoding=util.get_post_charset(request))
        # Remove the sequence factory data from the request
        for k in request_data.keys():
            if '*' in k:
                request_data.pop(k)
        # We need the _request_data to be populated so sequences know how many
        # items they have (i.e. .fields method on a sequence uses the number of
        # values on the _request_data)

        # Convert request data to a dottedish friendly representation
        request_data = _unflatten_request_data(request_data)
        self._request_data = dotted(request_data)
        self._request_data = dotted(self.widget.pre_parse_incoming_request_data(self.structure,request_data))