implemented proper settings handling
added some defaults for future setting templates
This commit is contained in:
parent
402c9541a8
commit
de315af6c9
1 changed files with 59 additions and 25 deletions
|
@ -7,9 +7,10 @@ BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, os.path.pardir
|
||||||
import sys
|
import sys
|
||||||
sys.path.insert(0, os.path.join(BASE_DIR, "src"))
|
sys.path.insert(0, os.path.join(BASE_DIR, "src"))
|
||||||
|
|
||||||
|
import bobo
|
||||||
import forms
|
import forms
|
||||||
import sqlobject
|
import sqlobject
|
||||||
import bobo
|
import cherrypy
|
||||||
from genshi.template import TemplateLoader
|
from genshi.template import TemplateLoader
|
||||||
import genshi.filters
|
import genshi.filters
|
||||||
import genshi.input
|
import genshi.input
|
||||||
|
@ -35,8 +36,23 @@ BASE_DICT = {
|
||||||
POLL_SETTINGS = {
|
POLL_SETTINGS = {
|
||||||
"show_all_submissions": (bool, True),
|
"show_all_submissions": (bool, True),
|
||||||
"show_statistics": (bool, True),
|
"show_statistics": (bool, True),
|
||||||
|
"public": (bool, False),
|
||||||
|
"expose_date": (int, 0),
|
||||||
|
"close_date": (int, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
POLL_SETTINGS_TEMPLATES = {
|
||||||
|
"brainstorming": {},
|
||||||
|
"cards": {"show_all_submissions": False},
|
||||||
|
"feedback": {"show_all_submissions": False},
|
||||||
|
"evaluation": {"show_all_submissions": False},
|
||||||
|
"notes": {"show_statistics": False},
|
||||||
|
"shopping": {"show_statistics": False},
|
||||||
|
"clipboard": {"show_statistics": False},
|
||||||
|
"namefinder": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ContentSubmission(sqlobject.SQLObject):
|
class ContentSubmission(sqlobject.SQLObject):
|
||||||
submitter = sqlobject.UnicodeCol()
|
submitter = sqlobject.UnicodeCol()
|
||||||
content = sqlobject.UnicodeCol()
|
content = sqlobject.UnicodeCol()
|
||||||
|
@ -65,6 +81,11 @@ class PollSetting(sqlobject.SQLObject):
|
||||||
value = sqlobject.UnicodeCol()
|
value = sqlobject.UnicodeCol()
|
||||||
|
|
||||||
|
|
||||||
|
class PollRelation(sqlobject.SQLObject):
|
||||||
|
first = sqlobject.ForeignKey("Poll")
|
||||||
|
second = sqlobject.ForeignKey("Poll")
|
||||||
|
|
||||||
|
|
||||||
class Poll(sqlobject.SQLObject):
|
class Poll(sqlobject.SQLObject):
|
||||||
author = sqlobject.UnicodeCol()
|
author = sqlobject.UnicodeCol()
|
||||||
hash_key = sqlobject.StringCol()
|
hash_key = sqlobject.StringCol()
|
||||||
|
@ -77,18 +98,20 @@ class Poll(sqlobject.SQLObject):
|
||||||
current_dict = {}
|
current_dict = {}
|
||||||
for setting in PollSetting.selectBy(poll_id=self.id):
|
for setting in PollSetting.selectBy(poll_id=self.id):
|
||||||
if setting.key in POLL_SETTINGS.keys():
|
if setting.key in POLL_SETTINGS.keys():
|
||||||
current_dict[setting.key] = self.validate_poll_setting(setting.key, setting.value)
|
current_dict[setting.key] = validate_poll_setting(setting.key, setting.value)
|
||||||
for key, meta_info in POLL_SETTINGS.items():
|
for key, meta_info in POLL_SETTINGS.items():
|
||||||
if not key in current_dict.keys():
|
if not key in current_dict.keys():
|
||||||
current_dict[key] = meta_info[1]
|
current_dict[key] = meta_info[1]
|
||||||
return current_dict
|
return current_dict
|
||||||
|
|
||||||
def change_setting(self, key, value):
|
def change_setting(self, key, value):
|
||||||
validated_value = self.validate_poll_setting(key, value)
|
validated_value = validate_poll_setting(key, value)
|
||||||
if not validated_value is None:
|
if not validated_value is None:
|
||||||
poll_setting = PollSetting.selectBy(poll_id=self.id, key=key)
|
poll_setting = PollSetting.selectBy(poll_id=self.id, key=key)
|
||||||
if poll_setting.count() == 1:
|
if poll_setting.count() == 1:
|
||||||
poll_setting.value = str(validated_value)
|
poll_setting[0].value = str(validated_value)
|
||||||
|
elif poll_setting.count() == 0:
|
||||||
|
PollSetting(poll_id=self.id, key=key, value=str(validated_value))
|
||||||
|
|
||||||
def get_num_of_submitters(self):
|
def get_num_of_submitters(self):
|
||||||
all_submitters = [submission.submitter for submission in ContentSubmission.selectBy(poll_id=self.id)]
|
all_submitters = [submission.submitter for submission in ContentSubmission.selectBy(poll_id=self.id)]
|
||||||
|
@ -139,13 +162,18 @@ def validate_poll_setting(key, value):
|
||||||
if setting_type in (basestring, unicode, str):
|
if setting_type in (basestring, unicode, str):
|
||||||
return value
|
return value
|
||||||
elif setting_type == bool:
|
elif setting_type == bool:
|
||||||
text = value.tolower()
|
if value is None:
|
||||||
if text in ("0", "false", "no", "off", "disabled", ""):
|
value = "false"
|
||||||
return False
|
if isinstance(value, bool):
|
||||||
elif text in ("1", "true", "yes", "on", "enabled"):
|
return value
|
||||||
return True
|
|
||||||
else:
|
else:
|
||||||
return None
|
text = value.lower()
|
||||||
|
if text in ("0", "false", "no", "off", "disabled", "", None):
|
||||||
|
return False
|
||||||
|
elif text in ("1", "true", "yes", "on", "enabled"):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return None
|
||||||
else:
|
else:
|
||||||
# all other types (e.g. int, float, ...)
|
# all other types (e.g. int, float, ...)
|
||||||
try:
|
try:
|
||||||
|
@ -200,7 +228,8 @@ def get_new_hash_key(length=16, charset=None):
|
||||||
@bobo.query('/new')
|
@bobo.query('/new')
|
||||||
@bobo.query('/new/:setting_defaults')
|
@bobo.query('/new/:setting_defaults')
|
||||||
@bobo.query('/new/:author/:title/:description')
|
@bobo.query('/new/:author/:title/:description')
|
||||||
def new_poll(submit=None, cancel=None, setting_defaults=None, author=None, title=None, description=None):
|
def new_poll(submit=None, cancel=None, setting_defaults=None, author=None,
|
||||||
|
title=None, description=None, **kwargs):
|
||||||
# TODO: implement "setting_defaults" for different (pre-defined) categories of polls
|
# TODO: implement "setting_defaults" for different (pre-defined) categories of polls
|
||||||
value_dict = get_default_values()
|
value_dict = get_default_values()
|
||||||
data = {"author": author, "title": title, "description": description}
|
data = {"author": author, "title": title, "description": description}
|
||||||
|
@ -228,7 +257,7 @@ def new_poll(submit=None, cancel=None, setting_defaults=None, author=None, title
|
||||||
return bobo.redirect(new_poll.get_admin_url())
|
return bobo.redirect(new_poll.get_admin_url())
|
||||||
|
|
||||||
@bobo.query('/:hash_key/submit')
|
@bobo.query('/:hash_key/submit')
|
||||||
def submit_content(hash_key=None, submitter=None, content=None):
|
def submit_content(hash_key=None, submitter=None, content=None, template=None):
|
||||||
value_dict = get_default_values()
|
value_dict = get_default_values()
|
||||||
data = {"submitter": submitter, "content": content}
|
data = {"submitter": submitter, "content": content}
|
||||||
poll_id = get_poll_id(hash_key)
|
poll_id = get_poll_id(hash_key)
|
||||||
|
@ -262,7 +291,8 @@ def delete_poll(admin_hash_key=None):
|
||||||
return bobo.redirect(BASE_DICT["base_url"])
|
return bobo.redirect(BASE_DICT["base_url"])
|
||||||
|
|
||||||
@bobo.query('/:admin_hash_key/admin')
|
@bobo.query('/:admin_hash_key/admin')
|
||||||
def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None, title=None, description=None, **kwargs):
|
def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None,
|
||||||
|
title=None, description=None, settings=None):
|
||||||
value_dict = get_default_values()
|
value_dict = get_default_values()
|
||||||
data = {"author": author, "title": title, "description": description}
|
data = {"author": author, "title": title, "description": description}
|
||||||
poll_id = get_poll_admin_id(admin_hash_key)
|
poll_id = get_poll_admin_id(admin_hash_key)
|
||||||
|
@ -278,15 +308,19 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None, titl
|
||||||
data["title"] = poll.title
|
data["title"] = poll.title
|
||||||
if description is None:
|
if description is None:
|
||||||
data["description"] = poll.description
|
data["description"] = poll.description
|
||||||
settings = poll.get_settings()
|
poll_settings = poll.get_settings()
|
||||||
# override with the given settings (taken from the form input with the prefix "setting_")
|
# update the settings only after a submit (otherwise we clear all current settings)
|
||||||
for setting_key, setting_value in kwargs.items():
|
if submit:
|
||||||
if setting_key.startswith("setting_"):
|
# override with the given settings (taken from the form input with the prefix "setting_")
|
||||||
real_key = setting_key[len("setting_"):]
|
if settings is None:
|
||||||
if real_key in POLL_SETTINGS.keys():
|
settings = []
|
||||||
valid_value = validate_poll_setting(real_key, setting_value)
|
elif not isinstance(settings, list):
|
||||||
if not valid_value is None:
|
settings = [settings]
|
||||||
settings[real_key] = valid_value
|
else:
|
||||||
|
# it is a list - everything is fine
|
||||||
|
pass
|
||||||
|
for setting_key in poll_settings.keys():
|
||||||
|
poll_settings[setting_key] = setting_key in settings
|
||||||
# store the new settings or create the new poll
|
# store the new settings or create the new poll
|
||||||
errors = {}
|
errors = {}
|
||||||
if submit:
|
if submit:
|
||||||
|
@ -296,7 +330,7 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None, titl
|
||||||
except formencode.Invalid, errors_packed:
|
except formencode.Invalid, errors_packed:
|
||||||
errors = errors_packed.unpack_errors()
|
errors = errors_packed.unpack_errors()
|
||||||
# add "settings" after forms validation - since there is no destination type
|
# add "settings" after forms validation - since there is no destination type
|
||||||
data["settings"] = settings
|
data["settings"] = [key for key, value in poll_settings.items() if value]
|
||||||
# the admin hash should also not be validated - thus we may not add it before
|
# the admin hash should also not be validated - thus we may not add it before
|
||||||
if errors:
|
if errors:
|
||||||
value_dict["errors"] = errors
|
value_dict["errors"] = errors
|
||||||
|
@ -309,7 +343,7 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None, titl
|
||||||
poll.description = data["description"]
|
poll.description = data["description"]
|
||||||
current_settings = poll.get_settings()
|
current_settings = poll.get_settings()
|
||||||
# update settings
|
# update settings
|
||||||
for key, value in settings.items():
|
for key, value in poll_settings.items():
|
||||||
if current_settings[key] != value:
|
if current_settings[key] != value:
|
||||||
poll.change_setting(key, value)
|
poll.change_setting(key, value)
|
||||||
return bobo.redirect(poll.get_admin_url())
|
return bobo.redirect(poll.get_admin_url())
|
||||||
|
@ -372,7 +406,7 @@ def static_files(p1=None, p2=None, p3=None):
|
||||||
return get_static_file(pathname)
|
return get_static_file(pathname)
|
||||||
|
|
||||||
|
|
||||||
for table in (Poll, ContentSubmission, PollSetting):
|
for table in (Poll, ContentSubmission, PollSetting, PollRelation):
|
||||||
#Poll.dropTable()
|
#Poll.dropTable()
|
||||||
if not table.tableExists():
|
if not table.tableExists():
|
||||||
table.createTable()
|
table.createTable()
|
||||||
|
|
Loading…
Reference in a new issue