Back to index

moin  1.9.0~rc2
fckconnector.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 """
00004 FCKeditor - The text editor for Internet - http://www.fckeditor.net
00005 Copyright (C) 2003-2009 Frederico Caldeira Knabben
00006 
00007 == BEGIN LICENSE ==
00008 
00009 Licensed under the terms of any of the following licenses at your
00010 choice:
00011 
00012 - GNU General Public License Version 2 or later (the "GPL")
00013 http://www.gnu.org/licenses/gpl.html
00014 
00015 - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
00016 http://www.gnu.org/licenses/lgpl.html
00017 
00018 - Mozilla Public License Version 1.1 or later (the "MPL")
00019 http://www.mozilla.org/MPL/MPL-1.1.html
00020 
00021 == END LICENSE ==
00022 
00023 Base Connector for Python (CGI and WSGI).
00024 
00025 See config.py for configuration settings
00026 
00027 """
00028 import cgi, os
00029 
00030 from fckutil import *
00031 from fckcommands import *   # default command's implementation
00032 from fckoutput import *     # base http, xml and html output mixins
00033 import config as Config
00034 
00035 class FCKeditorConnectorBase( object ):
00036        "The base connector class. Subclass it to extend functionality (see Zope example)"
00037 
00038        def __init__(self, environ=None):
00039               "Constructor: Here you should parse request fields, initialize variables, etc."
00040               self.request = FCKeditorRequest(environ) # Parse request
00041               self.headers = []                                       # Clean Headers
00042               if environ:
00043                      self.environ = environ
00044               else:
00045                      self.environ = os.environ
00046 
00047        # local functions
00048 
00049        def setHeader(self, key, value):
00050               self.headers.append ((key, value))
00051               return
00052 
00053 class FCKeditorRequest(object):
00054        "A wrapper around the request object"
00055        def __init__(self, environ):
00056               if environ: # WSGI
00057                      self.request = cgi.FieldStorage(fp=environ['wsgi.input'],
00058                                                  environ=environ,
00059                                                  keep_blank_values=1)
00060                      self.environ = environ
00061               else: # plain old cgi
00062                      self.environ = os.environ
00063                      self.request = cgi.FieldStorage()
00064               if 'REQUEST_METHOD' in self.environ and 'QUERY_STRING' in self.environ:
00065                      if self.environ['REQUEST_METHOD'].upper()=='POST':
00066                             # we are in a POST, but GET query_string exists
00067                             # cgi parses by default POST data, so parse GET QUERY_STRING too
00068                             self.get_request = cgi.FieldStorage(fp=None,
00069                                                  environ={
00070                                                  'REQUEST_METHOD':'GET',
00071                                                  'QUERY_STRING':self.environ['QUERY_STRING'],
00072                                                  },
00073                                                  )
00074               else:
00075                      self.get_request={}
00076 
00077        def has_key(self, key):
00078               return self.request.has_key(key) or self.get_request.has_key(key)
00079 
00080        def get(self, key, default=None):
00081               if key in self.request.keys():
00082                      field = self.request[key]
00083               elif key in self.get_request.keys():
00084                      field = self.get_request[key]
00085               else:
00086                      return default
00087               if hasattr(field,"filename") and field.filename: #file upload, do not convert return value
00088                      return field
00089               else:
00090                      return field.value