2.1 --- a/examples/Common/QuestionPlayer/Resources/player_template.xhtml Tue Nov 25 00:07:07 2008 +0100
2.2 +++ b/examples/Common/QuestionPlayer/Resources/player_template.xhtml Sat Nov 29 02:11:56 2008 +0100
2.3 @@ -5,7 +5,7 @@
2.4 xmlns:template="http://www.boddie.org.uk/ns/xmltools/template">
2.5 <head>
2.6 <title>Questionnaire</title>
2.7 - <link xmlns:xlink="http://www.w3.org/1999/xlink" href="styles/styles.css"
2.8 + <link xmlns:xlink="http://www.w3.org/1999/xlink" href="{$root}styles/styles.css"
2.9 rel="stylesheet" type="text/css" />
2.10 </head>
2.11
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/examples/Common/QuestionPlayer/Resources/questions.xml Sat Nov 29 02:11:56 2008 +0100
3.3 @@ -0,0 +1,76 @@
3.4 +<?xml version="1.0" encoding="UTF-8"?>
3.5 +<questionnaire xmlns="">
3.6 + <question xmlns="" destination="1" choice-response="multiple" question-text="In what capacity did you attend the conference?">
3.7 + <choice xmlns="" response-choice="I attended the talks">
3.8 + <response-type xmlns="" value="choice"/>
3.9 + </choice>
3.10 + <choice xmlns="" response-choice="I attended the tutorials">
3.11 + <response-type xmlns="" value="choice"/>
3.12 + </choice>
3.13 + <choice xmlns="" response-choice="I was a speaker">
3.14 + <response-type xmlns="" value="choice"/>
3.15 + </choice>
3.16 + </question>
3.17 + <question xmlns="" destination="1" question-text="What was your general impression of this year's conference? (If you want to, give a short explanation for your choice.)">
3.18 + <choice xmlns="" response-choice="Favourable">
3.19 + <response-type xmlns="" value="textual-choice"/>
3.20 + </choice>
3.21 + <choice xmlns="" response-choice="Mixed">
3.22 + <response-type xmlns="" value="textual-choice"/>
3.23 + </choice>
3.24 + <choice xmlns="" response-choice="Negative">
3.25 + <response-type xmlns="" value="textual-choice"/>
3.26 + </choice>
3.27 + </question>
3.28 + <question xmlns="" destination="1" question-text="What was your impression of the venue?">
3.29 + <choice xmlns="" response-choice="Favourable">
3.30 + <response-type xmlns="" value="textual-choice"/>
3.31 + </choice>
3.32 + <choice xmlns="" response-choice="Mixed">
3.33 + <response-type xmlns="" value="textual-choice"/>
3.34 + </choice>
3.35 + <choice xmlns="" response-choice="Negative">
3.36 + <response-type xmlns="" value="textual-choice"/>
3.37 + </choice>
3.38 + </question>
3.39 + <question xmlns="" destination="1" question-text="Was it easy to find the venue? If not, please indicate any problems you may have had.">
3.40 + <choice xmlns="" response-choice="Yes">
3.41 + <response-type xmlns="" value="choice"/>
3.42 + </choice>
3.43 + <choice xmlns="" response-choice="No">
3.44 + <response-type xmlns="" value="textual-choice"/>
3.45 + </choice>
3.46 + </question>
3.47 + <question xmlns="" question-text="What were your impressions of the catering?" destination="1">
3.48 + <choice xmlns="" response-choice="Favourable">
3.49 + <response-type xmlns="" value="textual-choice"/>
3.50 + </choice>
3.51 + <choice xmlns="" response-choice="Mixed">
3.52 + <response-type xmlns="" value="textual-choice"/>
3.53 + </choice>
3.54 + <choice xmlns="" response-choice="Negative">
3.55 + <response-type xmlns="" value="textual-choice"/>
3.56 + </choice>
3.57 + </question>
3.58 + <question xmlns="" destination="1" question-text="What kind of food and drink would you like to see at this conference?">
3.59 + <choice xmlns="" response-choice="">
3.60 + <response-type xmlns="" value="text"/>
3.61 + </choice>
3.62 + </question>
3.63 + <question xmlns="" question-text="Do you see yourself coming to the conference next year? If not, please try and give a reason." destination="1">
3.64 + <choice xmlns="" response-choice="Yes">
3.65 + <response-type xmlns="" value="choice"/>
3.66 + </choice>
3.67 + <choice xmlns="" response-choice="No">
3.68 + <response-type xmlns="" value="textual-choice"/>
3.69 + </choice>
3.70 + </question>
3.71 + <question xmlns="" question-text="Would you consider coming to this venue again next year? If not, please state why." destination="1">
3.72 + <choice xmlns="" response-choice="Yes">
3.73 + <response-type xmlns="" value="choice"/>
3.74 + </choice>
3.75 + <choice xmlns="" response-choice="No">
3.76 + <response-type xmlns="" value="textual-choice"/>
3.77 + </choice>
3.78 + </question>
3.79 +</questionnaire>
3.80 \ No newline at end of file
4.1 --- a/examples/Common/QuestionPlayer/__init__.py Tue Nov 25 00:07:07 2008 +0100
4.2 +++ b/examples/Common/QuestionPlayer/__init__.py Sat Nov 29 02:11:56 2008 +0100
4.3 @@ -15,9 +15,11 @@
4.4
4.5 # Site map imports.
4.6
4.7 -from WebStack.Resources.ResourceMap import MapResource
4.8 -from WebStack.Resources.Selectors import EncodingSelector
4.9 +from WebStack.Resources.ResourceMap import SimpleMap
4.10 +from WebStack.Resources.Selectors import EncodingSelector, PathSelector, AuthInfoSelector
4.11 from WebStack.Resources.Static import DirectoryResource
4.12 +from WebStack.Resources.Login import LoginAuthenticator, LoginResource
4.13 +from WebStack.Resources.LoginRedirect import LoginRedirectAuthenticator, SiteLoginRedirectResource
4.14
4.15 # Configuration settings.
4.16
4.17 @@ -49,8 +51,27 @@
4.18
4.19 def create_document(self, trans, form):
4.20 if XSLFormsResource.create_document(self, trans, form):
4.21 - questions_xml = self.prepare_document("questions")
4.22 - questions = libxml2dom.parse(questions_xml)
4.23 + admin = trans.get_user() == "admin"
4.24 + response = trans.get_virtual_path_info().split("/")
4.25 +
4.26 + repository = DirectoryRepository(storage_directory)
4.27 + if admin and len(response) > 1:
4.28 + key = response[1]
4.29 + try:
4.30 + questions_xml = repository.lock(key)
4.31 + try:
4.32 + questions = libxml2dom.parse(questions_xml)
4.33 + finally:
4.34 + repository.unlock(key)
4.35 + except KeyError:
4.36 + trans.set_response_code(404)
4.37 + trans.set_content_type(WebStack.Generic.ContentType("text/plain"))
4.38 + trans.get_response_stream().write("Questionnaire not found")
4.39 + raise WebStack.Generic.EndOfResponse
4.40 + else:
4.41 + questions_xml = self.prepare_document("questions")
4.42 + questions = libxml2dom.parse(questions_xml)
4.43 +
4.44 doc = form.get_document()
4.45 imported_questions = doc.importNode(questions.documentElement, 1)
4.46 doc.replaceChild(imported_questions, doc.documentElement)
4.47 @@ -79,32 +100,93 @@
4.48 repository.unlock(key)
4.49 repository.unlock("distilled-" + key)
4.50
4.51 + def create_output(self, trans, form):
4.52 + XSLFormsResource.create_output(self, trans, form, stylesheet_parameters=trans.get_attributes())
4.53 +
4.54 +class QuestionnaireAdminResource(XSLFormsResource):
4.55 +
4.56 + "A resource providing a list of questionnaires."
4.57 +
4.58 + resource_dir = resources(__file__)
4.59 + template_resources = {
4.60 + "questionnaires" : output("admin_template.xhtml")
4.61 + }
4.62 + init_resources = {
4.63 + "questionnaires" : input("admin_template.xhtml")
4.64 + }
4.65 +
4.66 + def select_activity(self, trans, form):
4.67 + form.set_activity("questionnaires")
4.68 +
4.69 + def create_document(self, trans, form):
4.70 + if XSLFormsResource.create_document(self, trans, form):
4.71 + doc = form.get_document()
4.72 + questionnaires = doc.documentElement
4.73 + for filename in os.listdir(storage_directory):
4.74 + if filename[0].isdigit():
4.75 + questionnaire = doc.createElement("questionnaire")
4.76 + questionnaire.setAttribute("name", filename)
4.77 + questionnaire.setAttribute("date-text", time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(filename))))
4.78 + questionnaires.appendChild(questionnaire)
4.79 +
4.80 + def create_output(self, trans, form):
4.81 + XSLFormsResource.create_output(self, trans, form, stylesheet_parameters=trans.get_attributes())
4.82 +
4.83 # Site map initialisation.
4.84
4.85 def get_site():
4.86
4.87 "Return a simple Web site resource."
4.88
4.89 + # Authentication objects.
4.90 +
4.91 + secret_key="this_must_be_secret"
4.92 + credentials = [
4.93 + ("admin", "123"),
4.94 + ]
4.95 + login_authenticator = LoginAuthenticator(secret_key, credentials)
4.96 + login_redirect_authenticator = LoginRedirectAuthenticator(secret_key)
4.97 +
4.98 # Get the main resource and the directory used by the application.
4.99
4.100 questionnaire_resource = QuestionnairePlayerResource()
4.101 + admin_resource = QuestionnaireAdminResource()
4.102 directory = questionnaire_resource.resource_dir
4.103
4.104 + # Get some login resources.
4.105 +
4.106 + login_redirector = SiteLoginRedirectResource(
4.107 + authenticator=login_redirect_authenticator,
4.108 + login_url="login",
4.109 + app_url="",
4.110 + resource=SimpleMap({
4.111 + "" : admin_resource,
4.112 + None : questionnaire_resource
4.113 + })
4.114 + )
4.115 + login_redirector.logout_parameter_name="logout"
4.116 +
4.117 + login_resource = LoginResource(login_authenticator)
4.118 +
4.119 # Make a simple Web site.
4.120
4.121 - resource = MapResource({
4.122 + resource = SimpleMap({
4.123 # Static resources:
4.124 "styles" : DirectoryResource(os.path.join(directory, "styles"), {"css" : "text/css"}),
4.125 + # Login resource:
4.126 + "login" : login_resource,
4.127 + # Administration resource:
4.128 + "admin" : login_redirector,
4.129 # Main page:
4.130 "" : questionnaire_resource
4.131 })
4.132
4.133 - return EncodingSelector(resource, encoding)
4.134 + return EncodingSelector(PathSelector(AuthInfoSelector(resource, login_redirect_authenticator)), encoding)
4.135
4.136 # Resource preparation ahead of time - useful for making installations.
4.137
4.138 def prepare_resources():
4.139 - for cls in [QuestionnairePlayerResource]:
4.140 + for cls in [QuestionnairePlayerResource, QuestionnaireAdminResource]:
4.141 xslforms_prepare_resources(cls)
4.142
4.143 # vim: tabstop=4 expandtab shiftwidth=4