input of date values implemented

This commit is contained in:
lars 2010-05-08 00:25:51 +00:00
parent 4f7782cefe
commit 841be7d138
2 changed files with 75 additions and 21 deletions

View file

@ -6,6 +6,11 @@ class PollForm(formencode.Schema):
description = formencode.validators.UnicodeString(strip=True, not_empty=True) description = formencode.validators.UnicodeString(strip=True, not_empty=True)
template = formencode.validators.UnicodeString(strip=True) template = formencode.validators.UnicodeString(strip=True)
class PollSettingsForm(PollForm):
settings = formencode.validators.Set()
setting_expose_date = formencode.validators.UnicodeString(strip=True, not_empty=True)
setting_close_date = formencode.validators.UnicodeString(strip=True, not_empty=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)
content = formencode.validators.UnicodeString(strip=True, not_empty=True) content = formencode.validators.UnicodeString(strip=True, not_empty=True)

View file

@ -34,17 +34,22 @@ BASE_DICT = {
"errors": {}, "errors": {},
} }
# used as the default setting for expose/close dates
DEFAULT_DAYS_AHEAD = 7
DATE_FORMAT = "%d.%m.%Y"
DEFAULT_DATE = datetime.datetime.now() + datetime.timedelta(days=DEFAULT_DAYS_AHEAD)
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), "public": (bool, False),
"expose_date": (int, 0), "expose_date": (datetime.datetime, DEFAULT_DATE),
"close_date": (int, 0), "close_date": (datetime.datetime, DEFAULT_DATE),
} }
POLL_SETTING_TEMPLATES = { POLL_SETTING_TEMPLATES = {
"brainstorm": {"expose_date": 20}, "brainstorm": {},
"cards": {"show_all_submissions": False, "close_date": 30}, "cards": {"show_all_submissions": False},
"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},
@ -118,14 +123,22 @@ class Poll(sqlobject.SQLObject):
current_dict[key] = meta_info[1] current_dict[key] = meta_info[1]
return current_dict return current_dict
def get_settings_strings(self):
settings = self.get_settings()
result = {}
for key, value in settings.items():
result[key] = get_poll_setting_string(key, value)
return result
def change_setting(self, key, value): def change_setting(self, key, value):
validated_value = validate_poll_setting(key, value) validated_value = validate_poll_setting(key, value)
if not validated_value is None: if not validated_value is None:
validated_value = get_poll_setting_string(key, validated_value)
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[0].value = str(validated_value) poll_setting[0].value = validated_value
elif poll_setting.count() == 0: elif poll_setting.count() == 0:
PollSetting(poll_id=self.id, key=key, value=str(validated_value)) PollSetting(poll_id=self.id, key=key, value=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)]
@ -195,6 +208,23 @@ class PollMesh:
return self.related return self.related
def get_poll_setting_string(key, value):
if not key in POLL_SETTINGS.keys():
return ""
setting_type = POLL_SETTINGS[key][0]
if setting_type in (basestring, unicode, str):
return value
elif setting_type == bool:
return str(value)
elif setting_type == datetime.datetime:
# some old entries are invalid
if value is None:
return ""
else:
return value.strftime(DATE_FORMAT)
else:
return str(value)
def validate_poll_setting(key, value): def validate_poll_setting(key, value):
if not key in POLL_SETTINGS.keys(): if not key in POLL_SETTINGS.keys():
return None return None
@ -203,17 +233,29 @@ def validate_poll_setting(key, value):
return value return value
elif setting_type == bool: elif setting_type == bool:
if value is None: if value is None:
value = "false" return False
if isinstance(value, bool): elif isinstance(value, bool):
return value return value
else: else:
text = value.lower() text = value.lower()
if text in ("0", "false", "no", "off", "disabled", "", None): if text in ("0", "false", "no", "off", "disabled", ""):
return False return False
elif text in ("1", "true", "yes", "on", "enabled"): elif text in ("1", "true", "yes", "on", "enabled"):
return True return True
else: else:
return None return None
elif setting_type == datetime.datetime:
if value is None:
# default: one week later
value = datetime.date.today() + datetime.timedelta(days=DEFAULT_DAYS_AHEAD)
elif type(value) == datetime.datetime:
pass
else:
try:
value = datetime.datetime.strptime(value, DATE_FORMAT)
except ValueError:
value = None
return value
elif setting_type == int: elif setting_type == int:
if value is None: if value is None:
value = 0 value = 0
@ -396,6 +438,10 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None,
data["title"] = poll.title data["title"] = poll.title
if description is None: if description is None:
data["description"] = poll.description data["description"] = poll.description
if setting_expose_date is None:
setting_expose_date = poll.get_settings_strings()["expose_date"]
if setting_close_date is None:
setting_close_date = poll.get_settings_strings()["close_date"]
poll_settings = poll.get_settings() poll_settings = poll.get_settings()
# update the settings only after a submit (otherwise we clear all current settings) # update the settings only after a submit (otherwise we clear all current settings)
if submit: if submit:
@ -409,22 +455,26 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None,
pass pass
for setting_key in poll_settings.keys(): for setting_key in poll_settings.keys():
poll_settings[setting_key] = setting_key in settings poll_settings[setting_key] = setting_key in settings
# store the new settings or create the new poll # collect all errors
errors = {} errors = {}
# add boolean "settings" after forms validation - since there is no destination type
data["settings"] = [key for key, value in poll_settings.items() if value is True]
for key, value in (("expose_date", setting_expose_date), ("close_date", setting_close_date)):
validated_value = validate_poll_setting(key, value)
if validated_value is None:
# keep the entered value and report an error
errors[key] = u"Ungültiges Datum"
data["setting_%s" % key] = value
else:
data["setting_%s" % key] = get_poll_setting_string(key, validated_value)
# use the validator to check for possible errors
if submit: if submit:
# check for errors only if the content is submitted (not just rendered) # check for errors only if the content is submitted (not just rendered)
try: try:
data = forms.PollForm.to_python(data) data = forms.PollSettingsForm.to_python(data)
except formencode.Invalid, errors_packed: except formencode.Invalid, errors_packed:
errors = errors_packed.unpack_errors() errors = errors_packed.unpack_errors()
# add boolean "settings" after forms validation - since there is no destination type # store the new settings
data["settings"] = [key for key, value in poll_settings.items() if value is True]
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)
@ -437,7 +487,7 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None,
current_settings = poll.get_settings() current_settings = poll.get_settings()
# update settings # update settings
for key, value in poll_settings.items(): for key, value in poll_settings.items():
if current_settings[key] != value: if (POLL_SETTINGS[key][0] == bool) and (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("expose_date", data["setting_expose_date"])
poll.change_setting("close_date", data["setting_close_date"]) poll.change_setting("close_date", data["setting_close_date"])
@ -484,7 +534,6 @@ def show_poll_list(render_file, page_size, page=None, filter_private=True):
page = 1 page = 1
end = start + page_size - 1 end = start + page_size - 1
value_dict["polls"] = polls[start : end + 1] value_dict["polls"] = polls[start : end + 1]
print value_dict["polls"]
# show a link for the next page, if more polls are available # show a link for the next page, if more polls are available
value_dict["show_next_link"] = (end + 1 < len(polls)) value_dict["show_next_link"] = (end + 1 < len(polls))
value_dict["show_previous_link"] = (start > 0) value_dict["show_previous_link"] = (start > 0)