test plugins for nospam.py and session.py

Steven Armstrong <[email protected]>
Newsgroups gmane.comp.web.pyblosxom.user
Message-ID <[email protected]>
Hi

A user has asked for support setting up the nospam and session plugins.
So I've written the attached two test plugins to separately test the beasts.

Maybe that helps someone.

cheers
Steven
test_nospam.py (text/x-python, 2.3 KB)
"""
Plugin to test the nospam.py plugin.

To install:
1) Put test_nospam.py in your plugin directory.
2) In config.py add test_nospam to py['load_plugins']

Then hit http://yourdomain.com/weblog/test_nospam with a browser.
"""

from nospam import _generateImage

_template = """<html>
<head>
<title>nospam.py test plugin</title>
</head>
<body>
<p><img src="%(base_url)s/test_nospam.png?test_nospam_number=%(test_nospam_number)s"</p>
%(test_nospam_message)s
<form name="f1" action="%(base_url)s/test_nospam" method="post">
<p>
<label for="test_nospam_number">Enter a number or string: </label>
<input type="text" id="test_nospam_number" name="test_nospam_number" value="" />
</p>
<input type="submit" value="Submit" />
</form>
<p>Note: This only tests the generation of the image itself.<br />
You'll still have to make sure that the comment plugin is installed and configured properly.</p>
</body>
</html>
"""


def _writeImage(request, number):
    image = _generateImage(number)

    response = request.getResponse()
    response.addHeader('Content-Type', 'image/png')
    image.save(response, "PNG")


def cb_handle(args):
    request = args['request']
    http = request.getHttp()
    config = request.getConfiguration()
    path_info = http["PATH_INFO"]


    if path_info.endswith("/test_nospam.png"):
        form = request.getForm()
        test_nospam_number = form.getvalue("test_nospam_number", None)
        _writeImage(request, test_nospam_number)

        # return True to tell pyblosxom that the request has been taken care of
        return 1

    elif path_info.endswith("/test_nospam"):
        form = request.getForm()
        test_nospam_number = form.getvalue("test_nospam_number", None)
        if test_nospam_number is not None:
            test_nospam_message = "<p>You entered: %s</p>" % test_nospam_number
        else:
            test_nospam_message = ""

        result = _template % {"base_url": config["base_url"], "test_nospam_message": test_nospam_message, "test_nospam_number": test_nospam_number}

        response = request.getResponse()
        response.addHeader("Content-type", "text/html")
        response.addHeader("Content-length", "%d" % len(result))
        response.write(result)

        # return True to tell pyblosxom that the request has been taken care of
        return 1
test_session.py (text/x-python, 1.7 KB)
"""
Plugin to test the session.py plugin.

To install:
1) Put test_session.py in your plugin directory.
2) In config.py add test_session to py['load_plugins']

Then hit http://yourdomain.com/weblog/test_session with a browser.
"""

_template = """<html>
<head>
<title>session.py test plugin</title>
</head>
<body>
<p>You visited this page %(counter)d times.</p>
<p>The session id is: %(sid)s</p>
<p>
<a href="%(url)s">Test the session</a> (or hit reload)<br />
<a href="%(url)s?clear=1">Invalidate the session</a>
</p>
</body>
</html>
"""


def cb_handle(args):
    request = args['request']
    http = request.getHttp()
    config = request.getConfiguration()

    if http['PATH_INFO'].endswith("/test_session"):
        form = request.getForm()
        session = request.getSession()        

        if "clear" in form:
            session.invalidate()
            sid = ""
            test_session_counter = 0
        else:
            sid = session.id()

            if "test_session_counter" in session:
                test_session_counter = int(session["test_session_counter"])
            else:
                test_session_counter = 0
            
            test_session_counter = test_session_counter +1

            session["test_session_counter"] = test_session_counter
            session.save()

        result = _template % {"counter": test_session_counter, "sid": sid, "url": config["base_url"] +"/test_session"}
        
        response = request.getResponse()
        response.addHeader("Content-type", "text/html")
        response.addHeader("Content-length", "%d" % len(result))
        response.write(result)

        # return True to tell pyblosxom that the request has been taken care of
        return 1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.