2009-05-17 11:34:55 +00:00
|
|
|
"""
|
|
|
|
questionnaire - Django Questionnaire App
|
|
|
|
========================================
|
|
|
|
|
|
|
|
Create flexible questionnaires.
|
|
|
|
|
|
|
|
Author: Robert Thomson <git AT corporatism.org>
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.dispatch import Signal
|
|
|
|
import imp
|
|
|
|
|
2015-02-13 23:42:41 +00:00
|
|
|
__all__ = ['question_proc', 'answer_proc', 'add_type', 'AnswerException',
|
|
|
|
'questionset_done', 'questionnaire_done', ]
|
2009-05-17 11:34:55 +00:00
|
|
|
|
|
|
|
QuestionChoices = []
|
2015-02-13 23:42:41 +00:00
|
|
|
QuestionProcessors = {} # supply additional information to the templates
|
|
|
|
Processors = {} # for processing answers
|
2009-05-17 11:34:55 +00:00
|
|
|
|
2014-11-05 02:00:10 +00:00
|
|
|
questionnaire_start = Signal(providing_args=["runinfo", "questionnaire"])
|
|
|
|
questionset_start = Signal(providing_args=["runinfo", "questionset"])
|
2009-05-17 11:34:55 +00:00
|
|
|
questionset_done = Signal(providing_args=["runinfo", "questionset"])
|
|
|
|
questionnaire_done = Signal(providing_args=["runinfo", "questionnaire"])
|
|
|
|
|
2015-02-13 23:42:41 +00:00
|
|
|
|
2009-05-17 11:34:55 +00:00
|
|
|
class AnswerException(Exception):
|
2015-02-13 23:42:41 +00:00
|
|
|
"""Thrown from an answer processor to generate an error message"""
|
2009-05-17 11:34:55 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def question_proc(*names):
|
|
|
|
"""
|
|
|
|
Decorator to create a question processor for one or more
|
|
|
|
question types.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
@question_proc('typename1', 'typename2')
|
|
|
|
def qproc_blah(request, question):
|
|
|
|
...
|
|
|
|
"""
|
2015-02-13 23:42:41 +00:00
|
|
|
|
2009-05-17 11:34:55 +00:00
|
|
|
def decorator(func):
|
|
|
|
global QuestionProcessors
|
|
|
|
for name in names:
|
|
|
|
QuestionProcessors[name] = func
|
|
|
|
return func
|
2015-02-13 23:42:41 +00:00
|
|
|
|
2009-05-17 11:34:55 +00:00
|
|
|
return decorator
|
|
|
|
|
2015-02-13 23:42:41 +00:00
|
|
|
|
2009-05-17 11:34:55 +00:00
|
|
|
def answer_proc(*names):
|
|
|
|
"""
|
|
|
|
Decorator to create an answer processor for one or more
|
|
|
|
question types.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
@question_proc('typename1', 'typename2')
|
|
|
|
def qproc_blah(request, question):
|
|
|
|
...
|
|
|
|
"""
|
2015-02-13 23:42:41 +00:00
|
|
|
|
2009-05-17 11:34:55 +00:00
|
|
|
def decorator(func):
|
|
|
|
global Processors
|
|
|
|
for name in names:
|
|
|
|
Processors[name] = func
|
|
|
|
return func
|
2015-02-13 23:42:41 +00:00
|
|
|
|
2009-05-17 11:34:55 +00:00
|
|
|
return decorator
|
|
|
|
|
2015-02-13 23:42:41 +00:00
|
|
|
|
2009-05-17 11:34:55 +00:00
|
|
|
def add_type(id, name):
|
|
|
|
"""
|
|
|
|
Register a new question type in the admin interface.
|
|
|
|
At least an answer processor must also be defined for this
|
|
|
|
type.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
add_type('mysupertype', 'My Super Type [radio]')
|
|
|
|
"""
|
|
|
|
global QuestionChoices
|
2015-02-13 23:42:41 +00:00
|
|
|
QuestionChoices.append((id, name))
|
2009-05-17 11:34:55 +00:00
|
|
|
|
|
|
|
|
2015-02-13 23:42:41 +00:00
|
|
|
import questionnaire.qprocessors # make sure ours are imported first
|
2009-05-17 11:34:55 +00:00
|
|
|
|
2014-04-25 16:21:38 +00:00
|
|
|
add_type('sameas', 'Same as Another Question (put sameas=question.number in checks or sameasid=question.id)')
|
2009-05-17 11:34:55 +00:00
|
|
|
|
|
|
|
for app in settings.INSTALLED_APPS:
|
|
|
|
try:
|
|
|
|
app_path = __import__(app, {}, {}, [app.split('.')[-1]]).__path__
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
imp.find_module('qprocessors', app_path)
|
|
|
|
except ImportError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
__import__("%s.qprocessors" % app)
|