webform.py :  » Blog » Frog » FrogComplete-1.8 » snakeserver » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Blog » Frog 
Frog » FrogComplete 1.8 » snakeserver » webform.py
#############################################################################
#
#  $Id: webform.py,v 1.9 2005/02/18 13:03:13 irmen Exp $
#  Form-related stuff (form parameters processing, file uploads)
#
#  This is part of "Snakelets" - Python Web Application Server
#  which is (c) Irmen de Jong - irmen@users.sourceforge.net
#
#############################################################################


import os
import urllib
import copy
import cgi

#
#   Exception that can be raised if something went wrong.
#
class FormFileUploadError(Exception):
    pass


#
#   FormUploadedFile: all data you need to know about an uploaded file
#
class FormUploadedFile:
    def __init__(self, name, cgiFieldStorage):
        self.name=name
        self.file=cgiFieldStorage.file
        self.filename=os.path.basename(cgiFieldStorage.filename)
        self.disposition=cgiFieldStorage.disposition
        self.dispositionOptions=cgiFieldStorage.disposition_options
        self.mimeType=cgiFieldStorage.type
        self.typeOptions=cgiFieldStorage.type_options
    def __repr__(self):
        return str(self)
    def __str__(self):
        return "<uploaded file '"+self.filename+"' in field '"+self.name+"' at "+hex(id(self))+">"

#
#   The Request's FORM object (with the form parameters)
#   It's a dictionary parameter-->value
#   where value can be singular or a list of values.
#   Uploaded file attachments are treated differently!
#   (that will be FormUploadedFile instances).
#
class Form(dict):
    def __init__(self, cgiFieldStorage, encoding=None):
        for param in cgiFieldStorage.keys():
            value=cgiFieldStorage[param]
            # Find out what type of entry it is.
            # Also, if an (input-)encoding is given, decode the parameters into unicode.
            if type(value) is list:
                if encoding:
                    self[param.decode(encoding)]=[ mf.value.decode(encoding) for mf in value]
                else:
                    self[param]=[ mf.value for mf in value]
            elif value.file and value.filename:
                self[param]=FormUploadedFile(param,value)
            else:
                if encoding:
                    uparam = param.decode(encoding)
                    uvalue=value.value.decode(encoding)
                    self[uparam]=uvalue
                else:
                    self[param]=value.value

    def parseQueryArgs(self, queryargs):
        # this is used to parse any GET-style query args when the method was POST.
        extraParams={}
        for key,value in cgi.parse_qsl(queryargs):
            extraParams.setdefault(key,[]).append(value)
        for key in extraParams:
            if key in self:
                if not type(self[key]) is list:
                    self[key] = [self[key]]
                self[key].extend(extraParams[key])
            else:
                self[key]=extraParams[key]

    def simplifyValues(self):
        # every value that is a list with one element,
        # will be replaced by only that element.
        removelist=[]
        for (name, value) in self.iteritems():
            if type(value) is list:
                if len(value)==1:
                    self[name]=value[0]
                elif len(value)==0:
                    removelist+=name
        for name in removelist:
            del self[name]

    def urlencode(self):
        return urllib.urlencode(self)

    def copy(self):
        return copy.copy(self)    # make a copy of self, not only of the dict

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.