示例#1
0
文件: utils.py 项目: goschtl/zope
def html_marshal(**kw):
    """ Marshal variables for html forms.
    """
    vars = []
    for key, converter, value in complex_marshal( kw.items() ):
        vars.append( ( key + converter, escape( str(value) ) ) )
    return tuple(vars)
示例#2
0
 def make_query(self, *args, **kwargs):
     '''Construct a URL query string, with marshalling markup.
 
     If there are positional arguments, they must be dictionaries.
     They are combined with the dictionary of keyword arguments to form
     a dictionary of query names and values.
 
     Query names (the keys) must be strings.  Values may be strings,
     integers, floats, or DateTimes, and they may also be lists or
     namespaces containing these types.  Names and string values
     should not be URL-quoted.  All arguments are marshalled with
     complex_marshal().
     '''
     d = {}
     for arg in args:
         d.update(arg)
     d.update(kwargs)
 
     uq = urllib.quote
     qlist = complex_marshal(d.items())
     for i in range(len(qlist)):
         k, m, v = qlist[i]
         qlist[i] = '%s%s=%s' % (uq(k), m, uq(str(v).encode('raw_unicode_escape')))
 
     return '&'.join(qlist)
示例#3
0
文件: utils.py 项目: bendavis78/zope
def html_marshal(**kw):
    """ Marshal variables for html forms.
    """
    vars = []
    for key, converter, value in complex_marshal(kw.items()):
        vars.append((key + converter, escape(str(value))))
    return tuple(vars)
示例#4
0
 def makeImageRepositoryQuery(self, data=None, add=None, omit=None):
     d = {}
     if data is not None:
         d.update(data)
     if add is not None:
         for key in add:
             if not d.has_key(key):
                 d[key] = add[key]
             else:
                 if isinstance(d[key], list):
                     d[key] = d[key] + add[key]
     if omit is not None:
         for key in omit:
             if d.has_key(key):
                 value = omit[key]
                 if not isinstance(value, list):
                     value = [value]
                 for v in value:
                     d[key] = [x for x in d[key] if x!=v]
     uq = urllib.quote
     qlist = complex_marshal(d.items())
     for i in range(len(qlist)):
         k, m, v = qlist[i]
         qlist[i] = '%s%s=%s' % (uq(k), m, uq(str(v)))
 
     return '&'.join(qlist)
示例#5
0
    def makeImageRepositoryQuery(self, data=None, add=None, omit=None):
        d = {}
        if data is not None:
            d.update(data)
        if add is not None:
            for key in add:
                if not d.has_key(key):
                    d[key] = add[key]
                else:
                    if isinstance(d[key], list):
                        d[key] = d[key] + add[key]
        if omit is not None:
            for key in omit:
                if d.has_key(key):
                    value = omit[key]
                    if not isinstance(value, list):
                        value = [value]
                    for v in value:
                        d[key] = [x for x in d[key] if x != v]
        uq = urllib.quote
        qlist = complex_marshal(d.items())
        for i in range(len(qlist)):
            k, m, v = qlist[i]
            qlist[i] = '%s%s=%s' % (uq(k), m, uq(str(v)))

        return '&'.join(qlist)
示例#6
0
def make_hidden_input(*args, **kwargs):
    """Construct a set of hidden input elements, with marshalling markup.

  If there are positional arguments, they must be dictionaries.
  They are combined with the dictionary of keyword arguments to form
  a dictionary of query names and values.

  Query names (the keys) must be strings.  Values may be strings,
  integers, floats, or DateTimes, and they may also be lists or
  namespaces containing these types.  All arguments are marshalled with
  complex_marshal().
  """

    d = {}
    for arg in args:
        d.update(arg)
    d.update(kwargs)

    hq = lambda x: cgi.escape(x, quote=True)
    qlist = complex_marshal(d.items())
    for i in range(len(qlist)):
        k, m, v = qlist[i]
        qlist[i] = '<input type="hidden" name="%s%s" value="%s" />' % (hq(k), m, hq(str(v)))

    return "\n".join(qlist)
示例#7
0
def make_hidden_input(*args, **kwargs):
  '''Construct a set of hidden input elements, with marshalling markup.

  If there are positional arguments, they must be dictionaries.
  They are combined with the dictionary of keyword arguments to form
  a dictionary of query names and values.

  Query names (the keys) must be strings.  Values may be strings,
  integers, floats, or DateTimes, and they may also be lists or
  namespaces containing these types.  All arguments are marshalled with
  complex_marshal().
  '''

  d = {}
  for arg in args:
      d.update(arg)
  d.update(kwargs)

  hq = lambda x:cgi.escape(x, quote=True)
  qlist = complex_marshal(d.items())
  for i in range(len(qlist)):
      k, m, v = qlist[i]
      qlist[i] = ('<input type="hidden" name="%s%s" value="%s" />'
                  % (hq(k), m, hq(str(v))))

  return '\n'.join(qlist)
示例#8
0
 def testMarshallRecords(self):
     '''Test marshalling records'''
     test_date = DateTime()
     record = {'arg1': 1, 'arg2': test_date, 'arg3': 'str'}
     result = complex_marshal([('record',record),])
     assert result == [('record.arg1', ':int:record', 1),
                       ('record.arg2', ':date:record', test_date),
                       ('record.arg3', ':record', 'str')]
示例#9
0
 def testMarshallLists(self):
     '''Test marshalling lists'''
     test_date = DateTime()
     list_ = [1, test_date, 'str']
     result = complex_marshal([('list',list_),])
     assert result == [('list', ':int:list', 1),
                       ('list', ':date:list', test_date),
                       ('list', ':list', 'str')]
示例#10
0
 def testMarshallLists(self):
     '''Test marshalling lists'''
     test_date = DateTime()
     list_ = [1, test_date, 'str']
     result = complex_marshal([('list',list_),])
     assert result == [('list', ':int:list', 1),
                       ('list', ':date:list', test_date),
                       ('list', ':list', 'str')]
示例#11
0
 def testMarshallRecords(self):
     '''Test marshalling records'''
     test_date = DateTime()
     record = {'arg1': 1, 'arg2': test_date, 'arg3': 'str'}
     result = complex_marshal([('record',record),])
     assert result == [('record.arg1', ':int:record', 1),
                       ('record.arg2', ':date:record', test_date),
                       ('record.arg3', ':record', 'str')]
示例#12
0
 def testMarshallListsInRecords(self):
     '''Test marshalling lists inside of records'''
     test_date = DateTime()
     record = {'arg1': [1, test_date, 'str'], 'arg2': 1}
     result = complex_marshal([('record',record),])
     assert result == [('record.arg1', ':int:list:record', 1),
                       ('record.arg1', ':date:list:record', test_date),
                       ('record.arg1', ':list:record', 'str'),
                       ('record.arg2', ':int:record', 1)]
示例#13
0
 def testMarshallListsInRecords(self):
     '''Test marshalling lists inside of records'''
     test_date = DateTime()
     record = {'arg1': [1, test_date, 'str'], 'arg2': 1}
     result = complex_marshal([('record',record),])
     assert result == [('record.arg1', ':int:list:record', 1),
                       ('record.arg1', ':date:list:record', test_date),
                       ('record.arg1', ':list:record', 'str'),
                       ('record.arg2', ':int:record', 1)]
示例#14
0
 def testMarshallLists(self):
     '''Test marshalling lists'''
     test_date = DateTime()
     test_unicode =  u'unic\xF3de'
     list_ = [1, test_date, 'str', test_unicode]
     result = complex_marshal([('list',list_),])
     assert result == [('list', ':int:list', 1),
                       ('list', ':date:list', test_date),
                       ('list', ':list', 'str'),
                       ('list', ':utf8:ustring:list', test_unicode.encode('utf8'))]
示例#15
0
 def testMarshallRecords(self):
     '''Test marshalling records'''
     test_date = DateTime()
     test_unicode =  u'unic\xF3de'
     record = {'arg1': 1, 'arg2': test_date, 'arg3': 'str', 'arg4': test_unicode }
     result = complex_marshal([('record',record),])
     assert result == [('record.arg1', ':int:record', 1),
                       ('record.arg2', ':date:record', test_date),
                       ('record.arg3', ':record', 'str'),
                       ('record.arg4', ':utf8:ustring:record', test_unicode.encode('utf8'))]
示例#16
0
 def testMarshallLists(self):
     '''Test marshalling lists'''
     test_date = DateTime()
     test_unicode =  u'unic\xF3de'
     list_ = [1, test_date, 'str', test_unicode]
     result = complex_marshal([('list',list_),])
     assert result == [('list', ':int:list', 1),
                       ('list', ':date:list', test_date),
                       ('list', ':list', 'str'),
                       ('list', ':utf8:ustring:list', test_unicode.encode('utf8'))]
示例#17
0
 def testMarshallRecords(self):
     '''Test marshalling records'''
     test_date = DateTime()
     test_unicode =  u'unic\xF3de'
     record = {'arg1': 1, 'arg2': test_date, 'arg3': 'str', 'arg4': test_unicode }
     result = complex_marshal([('record',record),])
     assert result == [('record.arg1', ':int:record', 1),
                       ('record.arg2', ':date:record', test_date),
                       ('record.arg3', ':record', 'str'),
                       ('record.arg4', ':utf8:ustring:record', test_unicode.encode('utf8'))]
示例#18
0
 def testMarshallLists(self):
     '''Test marshalling lists'''
     test_date = DateTime()
     list_ = [1, test_date, 'str', u'unic\xF3de']
     result = complex_marshal([
         ('list', list_),
     ])
     arg4_type = ':list'
     self.assertEqual(result, [('list', ':int:list', 1),
                               ('list', ':date:list', test_date),
                               ('list', ':list', 'str'),
                               ('list', arg4_type, u'unic\xF3de')])
示例#19
0
    def testMarshallRecordsInRecords(self):
        '''Test marshalling records inside records'''
        sub_record = {'sub1': 'deep', 'sub2': 1}
        record = {'arg1':'top', 'arg2':sub_record, 'arg3':12}
        result = complex_marshal([('r_record',record),])
        self.maxDiff = 1000

        #dictionaries don't preserve order, manually stort
        self.assertEqual(sorted(result),
                         [('r_record.arg1', ':record', 'top'),
                          ('r_record.arg2.sub1', ':record:record', 'deep'),
                          ('r_record.arg2.sub2', ':int:record:record', 1),
                          ('r_record.arg3', ':int:record', 12),
                         ])
示例#20
0
    def testMarshalListsInRecords(self):
        '''Test marshalling lists inside of records'''
        test_date = DateTime()
        test_unicode =  u'unic\xF3de'
        record = {'arg1': [1, test_date, test_unicode], 'arg2': 1, 'arg3':  test_unicode}
        result = complex_marshal([('record',record),])

        expect = [('record.arg1', ':int:list:record', 1),
                  ('record.arg1', ':date:list:record', test_date),
                  ('record.arg1', ':utf8:ustring:list:record', test_unicode.encode('utf8')),
                  ('record.arg2', ':int:record', 1),
                  ('record.arg3', ':utf8:ustring:record', test_unicode.encode('utf8'))]

        self.assertEqual(result, expect)
示例#21
0
    def testMarshallRecordsInRecords(self):
        '''Test marshalling records inside records'''
        sub_record = {'sub1': 'deep', 'sub2': 1}
        record = {'arg1':'top', 'arg2':sub_record, 'arg3':12}
        result = complex_marshal([('r_record',record),])
        self.maxDiff = 1000

        #dictionaries dont' preserve order - use self.assertItemsEqual
        self.assertItemsEqual(result,
                              [('r_record.arg1', ':record', 'top'),
                               ('r_record.arg2.sub1', ':record:record', 'deep'),
                               ('r_record.arg2.sub2', ':int:record:record', 1),
                               ('r_record.arg3', ':int:record', 12)]
                              )
示例#22
0
    def testMarshalListsInRecords(self):
        '''Test marshalling lists inside of records'''
        test_date = DateTime()
        test_unicode =  u'unic\xF3de'
        record = {'arg1': [1, test_date, test_unicode], 'arg2': 1, 'arg3':  test_unicode}
        result = complex_marshal([('record',record),])

        expect = [('record.arg1', ':int:list:record', 1),
                  ('record.arg1', ':date:list:record', test_date),
                  ('record.arg1', ':utf8:ustring:list:record', test_unicode.encode('utf8')),
                  ('record.arg2', ':int:record', 1),
                  ('record.arg3', ':utf8:ustring:record', test_unicode.encode('utf8'))]

        self.assertEqual(result, expect)
示例#23
0
 def testMarshallLists(self):
     '''Test marshalling lists'''
     test_date = DateTime()
     list_ = [1, test_date, 'str', u'unic\xF3de']
     result = complex_marshal([('list', list_), ])
     if PY2:
         arg4_type = ':utf8:ustring:list'
     else:
         arg4_type = ':list'
     self.assertEqual(result,
                      [('list', ':int:list', 1),
                       ('list', ':date:list', test_date),
                       ('list', ':list', 'str'),
                       ('list', arg4_type, u'unic\xF3de')])
示例#24
0
 def testMarshallRecordsInLists(self):
     '''Test marshalling records inside lists'''
     test_date = DateTime()
     record = {'arg1': [1, test_date, 'str'], 'arg2': 1}
     test_list = ['top', record, 12]
     result = complex_marshal([('list_key',test_list),])
     self.maxDiff = 1000
     self.assertEqual(result,
                      [('list_key', ':list', 'top'),
                       ('list_key.arg1', ':int:list:record:list', 1),
                       ('list_key.arg1', ':date:list:record:list', test_date),
                       ('list_key.arg1', ':list:record:list', 'str'),
                       ('list_key.arg2', ':int:record:list', 1),
                       ('list_key', ':int:list', 12)]
                      )
示例#25
0
 def testMarshallRecordsInLists(self):
     '''Test marshalling records inside lists'''
     test_date = DateTime()
     record = {'arg1': [1, test_date, 'str'], 'arg2': 1}
     test_list = ['top', record, 12]
     result = complex_marshal([('list_key',test_list),])
     self.maxDiff = 1000
     self.assertEqual(result,
                      [('list_key', ':list', 'top'),
                       ('list_key.arg1', ':int:list:record:list', 1),
                       ('list_key.arg1', ':date:list:record:list', test_date),
                       ('list_key.arg1', ':list:record:list', 'str'),
                       ('list_key.arg2', ':int:record:list', 1),
                       ('list_key', ':int:list', 12)]
                      )
示例#26
0
 def testMarshallListsInRecords(self):
     '''Test marshalling lists inside of records'''
     test_date = DateTime()
     record = {'arg1': [1, test_date, 'str', u'unic\xF3de'], 'arg2': 1}
     result = complex_marshal([
         ('record', record),
     ])
     arg1_type = ':list:record'
     self.assertEqual(
         set(result),
         set([('record.arg1', ':int:list:record', 1),
              ('record.arg1', ':date:list:record', test_date),
              ('record.arg1', ':list:record', 'str'),
              ('record.arg1', arg1_type, u'unic\xF3de'),
              ('record.arg2', ':int:record', 1)]))
示例#27
0
 def testMarshallListsInRecords(self):
     '''Test marshalling lists inside of records'''
     test_date = DateTime()
     record = {'arg1': [1, test_date, 'str', u'unic\xF3de'], 'arg2': 1}
     result = complex_marshal([('record', record), ])
     if PY2:
         arg1_type = ':utf8:ustring:list:record'
     else:
         arg1_type = ':list:record'
     self.assertEqual(
         set(result),
         set([('record.arg1', ':int:list:record', 1),
              ('record.arg1', ':date:list:record', test_date),
              ('record.arg1', ':list:record', 'str'),
              ('record.arg1', arg1_type, u'unic\xF3de'),
              ('record.arg2', ':int:record', 1)]))
示例#28
0
 def testMarshallRecords(self):
     '''Test marshalling records'''
     test_date = DateTime()
     record = {
         'arg1': 1, 'arg2': test_date,
         'arg3': 'str', 'arg4': u'unic\xF3de',
     }
     result = complex_marshal([('record', record), ])
     if PY2:
         arg4_type = ':utf8:ustring:record'
     else:
         arg4_type = ':record'
     self.assertEqual(
         set(result),
         set([('record.arg1', ':int:record', 1),
              ('record.arg2', ':date:record', test_date),
              ('record.arg3', ':record', 'str'),
              ('record.arg4', arg4_type, u'unic\xF3de')]))
示例#29
0
 def testMarshallListsInLists(self):
     '''Test marshalling lists in lists'''
     #As you see, you cannot see the boundry between sub_list1 and sub_list2
     #this is an 'oops'. There is no way to marshal this back into a list of lists
     #Solution: Use a delimiter :)
     test_date = DateTime()
     sub_list = [1, test_date, 'str']
     sub_list2 = [42, test_date, '2ndstr']
     list_ = [sub_list,"delimiter ha!", sub_list2]
     result = complex_marshal([('lil',list_),])
     self.assertEqual(result,
                      [('lil', ':int:list:list', 1),
                       ('lil', ':date:list:list', test_date),
                       ('lil', ':list:list', 'str'),
                       ('lil', ':list', 'delimiter ha!'),
                       ('lil', ':int:list:list', 42),
                       ('lil', ':date:list:list', test_date),
                       ('lil', ':list:list', '2ndstr')])
示例#30
0
 def testMarshallRecords(self):
     '''Test marshalling records'''
     test_date = DateTime()
     record = {
         'arg1': 1, 'arg2': test_date,
         'arg3': 'str', 'arg4': u'unic\xF3de',
     }
     result = complex_marshal([('record', record), ])
     if PY2:
         arg4_type = ':utf8:ustring:record'
     else:
         arg4_type = ':record'
     self.assertEqual(
         set(result),
         set([('record.arg1', ':int:record', 1),
              ('record.arg2', ':date:record', test_date),
              ('record.arg3', ':record', 'str'),
              ('record.arg4', arg4_type, u'unic\xF3de')]))
示例#31
0
 def testMarshallListsInLists(self):
     '''Test marshalling lists in lists'''
     #As you see, you cannot see the boundry between sub_list1 and sub_list2
     #this is an 'oops'. There is no way to marshal this back into a list of lists
     #Solution: Use a delimiter :)
     test_date = DateTime()
     sub_list = [1, test_date, 'str']
     sub_list2 = [42, test_date, '2ndstr']
     list_ = [sub_list,"delimiter ha!", sub_list2]
     result = complex_marshal([('lil',list_),])
     self.assertEqual(result,
                      [('lil', ':int:list:list', 1),
                       ('lil', ':date:list:list', test_date),
                       ('lil', ':list:list', 'str'),
                       ('lil', ':list', 'delimiter ha!'),
                       ('lil', ':int:list:list', 42),
                       ('lil', ':date:list:list', test_date),
                       ('lil', ':list:list', '2ndstr')])
def make_hidden_input(*args, **kwargs):
    '''Construct a set of hidden input elements, with marshalling markup.

    If there are positional arguments, they must be dictionaries.
    They are combined with the dictionary of keyword arguments to form
    a dictionary of query names and values.

    Query names (the keys) must be strings.  Values may be strings,
    integers, floats, or DateTimes, and they may also be lists or
    namespaces containing these types.  All arguments are marshalled with
    complex_marshal().
    '''

    # There has to be a better way, but I haven't found it.  Essentially
    # Allowing this to be a hidden variable results in an array coming
    # back in the request when the user starts to move through the process
    # again.  Somewere that populated array is replaced with an empty array
    # setting the shipping cost to 0.  This only happens if the site has
    # shipping set up and the user hits 'Back' from the review and pay step
# This 'fix' actually results in shipping always being 0.
#    if kwargs.has_key('form.shipping_method_code'):
#        del kwargs['form.shipping_method_code']

    d = {}
    for arg in args:
        d.update(arg)
    d.update(kwargs)

    hq = lambda x: cgi.escape(x, quote=True)
    qlist = complex_marshal(d.items())
    for i in range(len(qlist)):
        k, m, v = qlist[i]
        try:
            # try to convert to unicode first, because str() of the unicode
            # string may fail
            v = unicode(v)
        except UnicodeDecodeError:
            v = str(v)
        qlist[i] = ('<input type="hidden" name="%s%s" value="%s">'
                    % (hq(k), m, safe_unicode(hq(v))))

    return '\n'.join(qlist)
示例#33
0
文件: utils.py 项目: bendavis78/zope
def html_marshal(**kw):
    """ Marshal variables for html forms.
    """
    vars = [(key + converter, value)
            for key, converter, value in complex_marshal(kw.items())]
    return tuple(vars)
示例#34
0
文件: utils.py 项目: CGTIC/Plone_SP
def html_marshal(**kw):
    """ Marshal variables for html forms.
    """
    vars = [ (key + converter, value)
             for key, converter, value in complex_marshal(kw.items()) ]
    return tuple(vars)