added submission of content

added more detailed poll view
changed default settings to be more verbose
This commit is contained in:
lars 2010-04-15 11:50:58 +00:00
parent 366b11d733
commit 81d84b3498
5 changed files with 91 additions and 15 deletions

Binary file not shown.

View file

@ -5,3 +5,7 @@ class PollForm(formencode.Schema):
title = formencode.validators.UnicodeString(strip=True, not_empty=True)
description = formencode.validators.UnicodeString(strip=True, not_empty=True)
class SubmitForm(formencode.Schema):
submitter = formencode.validators.UnicodeString(strip=True, not_empty=True)
content = formencode.validators.UnicodeString(strip=True, not_empty=True)

View file

@ -13,8 +13,8 @@
<li>Author: ${poll.author}</li>
<li>Created: ${poll.get_creation_time_string()}</li>
<li>Description: ${poll.description}</li>
<li>User URL: <a href="${poll.get_url()}" title="link of this poll">${poll.get_url()}</a></li>
<li>Administrative URL: <a href="${poll.get_admin_url()}" title="link for managing this poll">${poll.get_admin_url()}</a></li>
<li>Public URL: <a href="${poll.get_url()}" title="link of this poll">${poll.get_url()}</a></li>
<li>Administrative URL: <a href="${poll.get_admin_url()}" title="link for managing this poll">${poll.get_admin_url()}</a> (here)</li>
<li py:if="poll.get_settings()">Settings:<ul>
<li py:for="key, value in poll.get_settings().items()">${key}: ${value}</li>
</ul></li>

View file

@ -9,13 +9,47 @@
</head>
<body>
<h1>Poll: ${poll.title}</h1>
<ul>
<li>Author: ${poll.author}</li>
<li>Created: ${poll.get_creation_time_string()}</li>
<li>Description: ${poll.description}</li>
<li>URL: <a href="${poll.get_url()}" title="link of this poll">${poll.get_url()}</a></li>
</ul>
<div class="poll_summary">
<ul>
<li>Author: ${poll.author}</li>
<li>Description: ${poll.description}</li>
<li>Created: ${poll.get_creation_time_string()}</li>
<li>Public URL: <a href="${poll.get_url()}" title="link of this poll">${poll.get_url()}</a></li>
</ul>
</div>
<div class="poll_submit_form">
<h2>Submit something</h2>
<form action="${poll.get_submit_url()}" method="post">
<ul>
<li><label for="submitter">Submitter:</label><input type="text" id="submitter" name="submitter" />
<span py:if="'submitter' in errors" class="error">${errors.submitter}</span></li>
<li><label for="content">New content:</label><input type="text" id="content" name="content" />
<span py:if="'content' in errors" class="error">${errors.content}</span></li>
</ul>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
<!-- previous submissions -->
<div class="poll_submissions" py:if="poll.get_settings()['show_all_submissions']">
<h2>Previous submissions</h2>
<table py:if="poll.get_num_of_submissions() > 0">
<tr><th>Submitter</th><th>Content</th><th>Timestamp</th></tr>
<tr py:for="one_submission in poll.get_submissions()">
<td>${one_submission.submitter}</td>
<td>${one_submission.content}</td>
<td>${one_submission.get_creation_time_string()}</td>
</tr>
</table>
<span py:if="poll.get_submissions().count() == 0">No submissions posted</span>
</div>
<!-- statistics -->
<div class="poll_statistics" py:if="poll.get_settings()['show_all_submissions']">
<h2>Statistics</h2>
<ul>
<li>Number of submitters: ${poll.get_num_of_submitters()}</li>
<li>Number of submissions: ${poll.get_num_of_submissions()}</li>
</ul>
</div>
</body>
</html>

View file

@ -27,20 +27,26 @@ BASE_DICT = {
}
POLL_SETTINGS = {
"show_all_submissions": (bool, False),
"show_all_submissions": (bool, True),
"show_statistics": (bool, True),
}
class WordSubmission(sqlobject.SQLObject):
class ContentSubmission(sqlobject.SQLObject):
submitter = sqlobject.StringCol()
content = sqlobject.StringCol()
poll_id = sqlobject.ForeignKey("Poll")
timestamp_creation = sqlobject.DateTimeCol()
def get_creation_time_string(self):
return str(self.timestamp_creation)
class PollSetting(sqlobject.SQLObject):
poll_id = sqlobject.ForeignKey("Poll")
key = sqlobject.StringCol()
value = sqlobject.StringCol()
class Poll(sqlobject.SQLObject):
author = sqlobject.StringCol()
hash_key = sqlobject.StringCol()
@ -67,7 +73,7 @@ class Poll(sqlobject.SQLObject):
poll_setting.value = str(validated_value)
def get_num_of_submitters(self):
all_submitters = [submission.submitter for submission in WordSubmission.selectBy(poll_id=self.id)]
all_submitters = [submission.submitter for submission in ContentSubmission.selectBy(poll_id=self.id)]
unique_submitters = []
for submitter in all_submitters:
if not submitter in unique_submitters:
@ -75,10 +81,13 @@ class Poll(sqlobject.SQLObject):
return len(unique_submitters)
def get_num_of_submissions(self):
return WordSubmission.selectBy(poll_id=self.id).count()
return ContentSubmission.selectBy(poll_id=self.id).count()
def get_submissions(self):
return ContentSubmission.selectBy(poll_id=self.id)
def delete_poll(self):
submissions = WordSubmission.selectBy(poll_id=self.id)
submissions = ContentSubmission.selectBy(poll_id=self.id)
settings = PollSetting.selectBy(poll_id=self.id)
for submission in submissions:
submission.destroySelf()
@ -89,6 +98,9 @@ class Poll(sqlobject.SQLObject):
def get_url(self):
return "%spolls/%s" % (BASE_DICT["base_url"], self.hash_key)
def get_submit_url(self):
return "%spolls/%s/submit" % (BASE_DICT["base_url"], self.hash_key)
def get_admin_url(self):
return "%spolls/%s" % (BASE_DICT["base_url"], self.admin_hash_key)
@ -183,6 +195,32 @@ def new_poll(submit=None, cancel=None, author=None, title=None, description=None
new_poll = Poll(hash_key=hash_key, admin_hash_key=admin_hash_key, timestamp_creation=now, **data)
return bobo.redirect(new_poll.get_admin_url())
@bobo.query('/polls/:hash_key/submit')
def submit_content(hash_key=None, submitter=None, content=None):
value_dict = get_default_values()
data = {"submitter": submitter, "content": content}
poll_id = get_poll_id(hash_key)
if not poll_id is None:
poll = Poll.get(poll_id)
value_dict["poll"] = poll
errors = {}
try:
data = forms.SubmitForm.to_python(data)
except formencode.Invalid, errors_packed:
errors = errors_packed.unpack_errors()
if errors:
value_dict["errors"] = errors
return render("poll_details.html", input_data=data, **value_dict)
else:
# create the new submission content
data["timestamp_creation"] = datetime.datetime.now()
data["poll_id"] = poll.id
ContentSubmission(**data)
# remove "content" for the next input
del data["content"]
return render("poll_details.html", input_data=data, **value_dict)
return bobo.redirect(BASE_DICT["base_url"])
@bobo.query('/polls/:admin_hash_key/delete')
def delete_poll(admin_hash_key=None):
admin_poll_id = get_poll_admin_id(admin_hash_key)
@ -277,7 +315,7 @@ def show_one_poll(poll_hash=None):
return bobo.redirect(BASE_DICT["base_url"])
for table in (Poll, WordSubmission, PollSetting):
for table in (Poll, ContentSubmission, PollSetting):
#Poll.dropTable()
if not table.tableExists():
table.createTable()