view roundup/cgi/wsgi_handler.py @ 3909:e89bcb28f683

indexargs_url force ids to int ids appear as hyperdb.String instances, which confused indexargs_url when they appear in the filterspec. They need to be treated as treated as integers when generating URLs. It feels sort of hacky to check for 'id' like this but I'm at a loss for what else to do in this case. Suggestions are welcome :) Maybe we should look into using some other hyperdb class to represent ids? this fixes [SF#783492] Some trailing whitespace also got trimmed.
author Justus Pendleton <jpend@users.sourceforge.net>
date Tue, 18 Sep 2007 16:59:42 +0000
parents a2d22d0de0bc
children 0728808fdf5c
line wrap: on
line source

# WSGI interface for Roundup Issue Tracker
#
# This module is free software, you may redistribute it
# and/or modify under the same terms as Python.
#

import os
import cgi
import weakref

import roundup.instance
from roundup.cgi import TranslationService
from BaseHTTPServer import BaseHTTPRequestHandler


class Writer(object):
    '''Perform a start_response if need be when we start writing.'''
    def __init__(self, request):
        self.request = request #weakref.ref(request)
    def write(self, data):
        f = self.request.get_wfile()
        self.write = f
        return f(data)

class RequestDispatcher(object):
    def __init__(self, home, debug=False, timing=False, lang=None):
        assert os.path.isdir(home), '%r is not a directory'%(home,)
        self.home = home
        self.debug = debug
        self.timing = timing
        if lang:
            self.translator = TranslationService.get_translation(lang,
                tracker_home=home)
        else:
            self.translator = None

    def __call__(self, environ, start_response):
        """Initialize with `apache.Request` object"""
        self.environ = environ
        self.__start_response = start_response

        self.wfile = Writer(self)
        self.__wfile = None

        tracker = roundup.instance.open(self.home, not self.debug)

        # need to strip the leading '/'
        environ["PATH_INFO"] = environ["PATH_INFO"][1:]
        if self.timing:
            environ["CGI_SHOW_TIMING"] = self.timing

        form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)

        client = tracker.Client(tracker, self, environ, form,
            self.translator)
        try:
            client.main()
        except roundup.cgi.client.NotFound:
            self.start_response([('Content-Type', 'text/html')], 404)
            self.wfile.write('Not found: %s'%client.path)

        # all body data has been written using wfile
        return []

    def start_response(self, headers, response_code):
        """Set HTTP response code"""
        description = BaseHTTPRequestHandler.responses[response_code]
        self.__wfile = self.__start_response('%d %s'%(response_code,
            description), headers)

    def get_wfile(self):
        if self.__wfile is None:
            raise ValueError, 'start_response() not called'
        return self.__wfile


Roundup Issue Tracker: http://roundup-tracker.org/