added support for non-boolean poll settings

This commit is contained in:
lars 2010-05-07 02:32:31 +00:00
parent 9f01982035
commit d964ba0f6a
2 changed files with 47 additions and 16 deletions

View file

@ -4,6 +4,7 @@ class PollForm(formencode.Schema):
author = formencode.validators.UnicodeString(strip=True, not_empty=True) author = formencode.validators.UnicodeString(strip=True, not_empty=True)
title = formencode.validators.UnicodeString(strip=True, not_empty=True) title = formencode.validators.UnicodeString(strip=True, not_empty=True)
description = formencode.validators.UnicodeString(strip=True, not_empty=True) description = formencode.validators.UnicodeString(strip=True, not_empty=True)
template = formencode.validators.UnicodeString(strip=True)
class SubmitForm(formencode.Schema): class SubmitForm(formencode.Schema):
submitter = formencode.validators.UnicodeString(strip=True, not_empty=True) submitter = formencode.validators.UnicodeString(strip=True, not_empty=True)

View file

@ -41,9 +41,9 @@ POLL_SETTINGS = {
"close_date": (int, 0), "close_date": (int, 0),
} }
POLL_SETTINGS_TEMPLATES = { POLL_SETTING_TEMPLATES = {
"brainstorming": {}, "brainstorm": {"expose_date": 20},
"cards": {"show_all_submissions": False}, "cards": {"show_all_submissions": False, "close_date": 30},
"feedback": {"show_all_submissions": False}, "feedback": {"show_all_submissions": False},
"evaluation": {"show_all_submissions": False}, "evaluation": {"show_all_submissions": False},
"notes": {"show_statistics": False}, "notes": {"show_statistics": False},
@ -174,6 +174,14 @@ def validate_poll_setting(key, value):
return True return True
else: else:
return None return None
elif setting_type == int:
if value is None:
value = 0
try:
value = int(value)
except ValueError:
value = None
return value
else: else:
# all other types (e.g. int, float, ...) # all other types (e.g. int, float, ...)
try: try:
@ -226,13 +234,13 @@ def get_new_hash_key(length=16, charset=None):
return hash_key return hash_key
@bobo.query('/new') @bobo.query('/new')
@bobo.query('/new/:setting_defaults') @bobo.query('/new/:template')
@bobo.query('/new/:author/:title/:description') @bobo.query('/new/:author/:title/:description')
def new_poll(submit=None, cancel=None, setting_defaults=None, author=None, def new_poll(submit=None, cancel=None, author=None, title=None,
title=None, description=None, **kwargs): description=None, template=None, hide_errors=False):
# 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,
"template": template}
if cancel: if cancel:
return bobo.redirect(BASE_DICT["base_url"]) return bobo.redirect(BASE_DICT["base_url"])
elif not submit: elif not submit:
@ -246,18 +254,32 @@ def new_poll(submit=None, cancel=None, setting_defaults=None, author=None,
except formencode.Invalid, errors_packed: except formencode.Invalid, errors_packed:
errors = errors_packed.unpack_errors() errors = errors_packed.unpack_errors()
if errors: if errors:
value_dict["errors"] = errors if not hide_errors:
value_dict["errors"] = errors
return render("poll_new.html", input_data=data, **value_dict) return render("poll_new.html", input_data=data, **value_dict)
else: else:
# get the template settings
poll_settings = POLL_SETTINGS.copy()
if template in POLL_SETTING_TEMPLATES.keys():
template_settings = POLL_SETTING_TEMPLATES[template].copy()
else:
template_settings = POLL_SETTING_TEMPLATES["brainstorm"]
for key, value in template_settings.items():
poll_settings[key] = value
# create the new poll # create the new poll
hash_key = get_new_hash_key() hash_key = get_new_hash_key()
admin_hash_key = get_new_hash_key() admin_hash_key = get_new_hash_key()
now = datetime.datetime.now() now = datetime.datetime.now()
new_poll = Poll(hash_key=hash_key, admin_hash_key=admin_hash_key, timestamp_creation=now, **data) new_poll = Poll(hash_key=hash_key, admin_hash_key=admin_hash_key,
timestamp_creation=now, author=data["author"],
title=data["title"], description=data["description"])
# apply the template settings
for key, value in template_settings.items():
new_poll.change_setting(key, value)
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, template=None): def submit_content(hash_key=None, submitter=None, content=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)
@ -292,9 +314,10 @@ def delete_poll(admin_hash_key=None):
@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, def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None,
title=None, description=None, settings=None): title=None, description=None, settings=None, setting_expose_date=None,
setting_close_date=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, "template": ""}
poll_id = get_poll_admin_id(admin_hash_key) poll_id = get_poll_admin_id(admin_hash_key)
if poll_id is None: if poll_id is None:
return bobo.redirect(BASE_DICT["base_url"]) return bobo.redirect(BASE_DICT["base_url"])
@ -329,9 +352,14 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None,
data = forms.PollForm.to_python(data) data = forms.PollForm.to_python(data)
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 boolean "settings" after forms validation - since there is no destination type
data["settings"] = [key for key, value in poll_settings.items() if value] data["settings"] = [key for key, value in poll_settings.items() if value is True]
# the admin hash should also not be validated - thus we may not add it before validated_expose_date = validate_poll_setting("expose_date", setting_expose_date)
if not validated_expose_date is None:
data["setting_expose_date"] = validated_expose_date
validated_close_date = validate_poll_setting("close_date", setting_close_date)
if not validated_close_date is None:
data["setting_close_date"] = validated_close_date
if errors: if errors:
value_dict["errors"] = errors value_dict["errors"] = errors
return render("poll_admin_edit.html", input_data=data, **value_dict) return render("poll_admin_edit.html", input_data=data, **value_dict)
@ -346,6 +374,8 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None,
for key, value in poll_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)
poll.change_setting("expose_date", data["setting_expose_date"])
poll.change_setting("close_date", data["setting_close_date"])
return bobo.redirect(poll.get_admin_url()) return bobo.redirect(poll.get_admin_url())
else: else:
return render("poll_admin_edit.html", input_data=data, **value_dict) return render("poll_admin_edit.html", input_data=data, **value_dict)