# HG changeset patch # User paulb # Date 1093711931 0 # Node ID 64c78f1a922d57bc30c00a95dbdcf1c8149319b0 # Parent aba9091606258d511a76bbf1cc1d3fefe1ff9ab2 [project @ 2004-08-28 16:52:11 by paulb] Introduced helper functionality to reduce duplicated code dealing with FieldStorage objects (in WebStack.Helpers.Request). diff -r aba909160625 -r 64c78f1a922d WebStack/Helpers/Request.py --- a/WebStack/Helpers/Request.py Sat Aug 28 16:51:53 2004 +0000 +++ b/WebStack/Helpers/Request.py Sat Aug 28 16:52:11 2004 +0000 @@ -82,4 +82,53 @@ self.name = name self.value = value +def get_storage_items(storage_body): + + """ + Return the items (2-tuples of the form key, values) from the 'storage_body'. + This is used in conjunction with FieldStorage objects. + """ + + items = [] + for key in storage_body.keys(): + items.append((key, storage_body[key])) + return items + +def get_body_fields(field_items, encoding): + + """ + Returns a dictionary mapping field names to lists of field values for all + entries in the given 'field_items' (2-tuples of the form key, values) using + the given 'encoding'. + This is used in conjunction with FieldStorage objects. + """ + + fields = {} + + for field_name, field_values in field_items: + if type(field_values) == type([]): + fields[field_name] = [] + for field_value in field_values: + fields[field_name].append(get_body_field(field_value.value, encoding)) + else: + fields[field_name] = [get_body_field(field_values.value, encoding)] + + return fields + +def get_body_field(field_str, encoding): + + """ + Returns the appropriate value for the given 'field_str' string using the + given 'encoding'. + """ + + # Detect stray FieldStorage objects (eg. with Webware). + + if hasattr(field_str, "value"): + return get_body_field(field_str.value, encoding) + elif encoding is not None: + return unicode(field_str, encoding) + else: + return field_str + # vim: tabstop=4 expandtab shiftwidth=4