Search code examples
pythondjangofckeditormod-wsgi

How do you use FCKEditor's image upload and browser with mod-wsgi?


I am using FCKEditor within a Django app served by Apache/mod-wsgi. I don't want to install php just for FCKEditor andI see FCKEditor offers image uploading and image browsing through Python. I just haven't found good instructions on how to set this all up.

So currently Django is running through a wsgi interface using this setup:

import os, sys

DIRNAME = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-3])
sys.path.append(DIRNAME)
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

In fckeditor in the editor->filemanager->connectors->py directory there is a file called wsgi.py:

from connector import FCKeditorConnector
from upload import FCKeditorQuickUpload

import cgitb
from cStringIO import StringIO

# Running from WSGI capable server (recomended)
def App(environ, start_response):
    "WSGI entry point. Run the connector"
    if environ['SCRIPT_NAME'].endswith("connector.py"):
        conn = FCKeditorConnector(environ)
    elif environ['SCRIPT_NAME'].endswith("upload.py"):
        conn = FCKeditorQuickUpload(environ)
    else:
        start_response ("200 Ok", [('Content-Type','text/html')])
        yield "Unknown page requested: "
        yield environ['SCRIPT_NAME']
        return
    try:
        # run the connector
        data = conn.doResponse()
        # Start WSGI response:
        start_response ("200 Ok", conn.headers)
        # Send response text
        yield data
    except:
        start_response("500 Internal Server Error",[("Content-type","text/html")])
        file = StringIO()
        cgitb.Hook(file = file).handle()
    yield file.getvalue()

I need these two things two work together by means of modifying my django wsgi file to serve the fckeditor parts correctly or make apache serve both django and fckeditor correctly on a single domain.


Solution

  • Edit: Ultimately I was unhappy with this solution also so I made a Django app that takes care of the file uploads and browsing.

    This is the solution I finally hacked together after reading the fckeditor code:

    import os, sys
    
    def fck_handler(environ, start_response):
        path = environ['PATH_INFO']
        if path.endswith(('upload.py', 'connector.py')):
            sys.path.append('/#correct_path_to#/fckeditor/editor/filemanager/connectors/py/')
            if path.endswith('upload.py'):
                from upload import FCKeditorQuickUpload
                conn = FCKeditorQuickUpload(environ)
            else:
                from connector import FCKeditorConnector
                conn = FCKeditorConnector(environ)
            try:
                data = conn.doResponse()
                start_response('200 Ok', conn.headers)
                return data
            except:
                start_response("500 Internal Server Error",[("Content-type","text/html")])
                return "There was an error"
        else:
            sys.path.append('/path_to_your_django_site/')
            os.environ['DJANGO_SETTINGS_MODULE'] = 'your_django_site.settings'
            import django.core.handlers.wsgi
            handler = django.core.handlers.wsgi.WSGIHandler()
            return handler(environ, start_response)
    
    application = fck_handler