# HG changeset patch # User Paul Boddie # Date 1227228137 -3600 # Node ID a9db62a5844968c740c6f216ba9feab68e69f379 # Parent 5d56fca348fa48fb1876ee53f00648131a5ad699 Added an experimental questionnaire player. diff -r 5d56fca348fa -r a9db62a58449 examples/BaseHTTPRequestHandler/QuestionPlayerApp.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/BaseHTTPRequestHandler/QuestionPlayerApp.py Fri Nov 21 01:42:17 2008 +0100 @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +from WebStack.Adapters.BaseHTTPRequestHandler import deploy +import QuestionPlayer + +# Get a simple Web site. + +resource = QuestionPlayer.get_site() + +# Special magic incantation. + +print "Serving..." +deploy(resource, handle_errors=0) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 5d56fca348fa -r a9db62a58449 examples/Common/QuestionPlayer/Resources/player_template.xhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Common/QuestionPlayer/Resources/player_template.xhtml Fri Nov 21 01:42:17 2008 +0100 @@ -0,0 +1,100 @@ + + + + + Questionnaire + + + + +

Questionnaire

+ +
+ +
+ +
+ + + + + + + + + + + + +
Question #n + Question + + + + + +
Response +
+ + + +

+ + + + + + + + + + + + + + Choice +

+ + + +

+ +

+ + + + + +
+
+ +
+ +
+ +
+ +

+ to refresh the form. +

+ +

+ when you are happy with + your responses. +

+ +
+ +
+ + + diff -r 5d56fca348fa -r a9db62a58449 examples/Common/QuestionPlayer/Resources/styles/styles.css --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Common/QuestionPlayer/Resources/styles/styles.css Fri Nov 21 01:42:17 2008 +0100 @@ -0,0 +1,61 @@ +/* Page sections. */ + +div.questionnaire { + padding: 10px; +} + +div.operations { + padding-top: 10px; + clear: both; +} + +/* Questionnaire and shadow effect. */ + +div.shadow { + background-color: #555555; +} + +div.shadow table.questionnaire { + background-color: #ffffff; + border: 1px solid #000000; + padding: 5px; + position: relative; + top: -10px; + left: -10px; +} + +/* Questionnaire table. */ + +table.questionnaire { + width: 100%; + border-spacing: 0px; +} + +/* Questionnaire and preview cells. */ + +table.questionnaire td, table.questionnaire th { + padding: 10px; + margin: 0px; +} + +table.questionnaire td p { + margin: 0px 5px 5px 5px; +} + +.question, .question-options { + background-color: #dddddd; + color: #000000; + vertical-align: top; +} + +.response, .response-options { + background-color: #ffffff; + color: #000000; + vertical-align: top; +} + +.choice, .choice-options { + background-color: #eeeeee; + color: #000000; + vertical-align: top; +} diff -r 5d56fca348fa -r a9db62a58449 examples/Common/QuestionPlayer/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Common/QuestionPlayer/__init__.py Fri Nov 21 01:42:17 2008 +0100 @@ -0,0 +1,101 @@ +#!/usr/bin/env python + +""" +A WebStack application for "playing" questionnaires designed by the +questionnaire editor. +""" + +import WebStack.Generic +from XSLForms.Resources.WebResources import \ + XSLFormsResource, input, output, resources, prepare_resources as xslforms_prepare_resources +import os +import libxml2dom +from WebStack.Repositories.Directory import DirectoryRepository +import time + +# Site map imports. + +from WebStack.Resources.ResourceMap import MapResource +from WebStack.Resources.Selectors import EncodingSelector +from WebStack.Resources.Static import DirectoryResource + +# Configuration settings. + +encoding = "utf-8" +storage_directory = os.path.join(os.getcwd(), "Responses") + +# Resource classes. + +class QuestionnairePlayerResource(XSLFormsResource): + + "A resource providing a questionnaire editor." + + resource_dir = resources(__file__) + template_resources = { + "questionnaire" : output("player_template.xhtml") + } + #init_resources = { + # "questionnaire" : input("player_template.xhtml") + # } + document_resources = { + "questions" : "questions.xml" + } + + def select_activity(self, trans, form): + form.set_activity("questionnaire") + + def create_document(self, trans, form): + if XSLFormsResource.create_document(self, trans, form): + questions_xml = self.prepare_document("questions") + questions = libxml2dom.parse(questions_xml) + doc = form.get_document() + imported_questions = doc.importNode(questions.documentElement, 1) + doc.replaceChild(imported_questions, doc.documentElement) + + def respond_to_input(self, trans, form): + + parameters = form.get_parameters() + selectors = form.get_selectors() + questionnaire = form.get_document() + + # Send a response according to certain parameters. + # When saved, an XML version of the data is employed. + + if parameters.has_key("finish"): + repository = DirectoryRepository(storage_directory) + key = str(time.time()) + output_filename = repository.lock(key, 1) + try: + questionnaire.toStream(open(output_filename, "wb"), "utf-8", prettyprint=1) + finally: + repository.unlock(key) + +# Site map initialisation. + +def get_site(): + + "Return a simple Web site resource." + + # Get the main resource and the directory used by the application. + + questionnaire_resource = QuestionnairePlayerResource() + directory = questionnaire_resource.resource_dir + + # Make a simple Web site. + + resource = MapResource({ + # Static resources: + "styles" : DirectoryResource(os.path.join(directory, "styles"), {"css" : "text/css"}), + # Main page: + "" : questionnaire_resource + }) + + return EncodingSelector(resource, encoding) + +# Resource preparation ahead of time - useful for making installations. + +def prepare_resources(): + for cls in [QuestionnairePlayerResource]: + xslforms_prepare_resources(cls) + +# vim: tabstop=4 expandtab shiftwidth=4