diff --git a/wortschlucker/src/wortschlucker.py b/wortschlucker/src/wortschlucker.py index f8cb36a..c998e93 100755 --- a/wortschlucker/src/wortschlucker.py +++ b/wortschlucker/src/wortschlucker.py @@ -23,7 +23,9 @@ import datetime import mimetypes import uuid import re +import hashlib import twitter +import urllib2 CONFIG_FILE = os.path.join(BASE_DIR, "wortschlucker.conf") @@ -79,17 +81,18 @@ class ContentSubmission(sqlobject.SQLObject): return str(self.timestamp_creation) def get_markup_content(self): - def get_link_markup(match): - prefix, url, suffix = match.groups() - # only take the TLD part of the url - short_name = url.split("/")[2] - return """%s%s%s""" % (prefix, url, short_name, suffix) - # surround all urls with html markup - mark_links = re.sub(r"(\A|\s|\()(https?://[\w/\?\.\#=;,_\-\~]*)(\)|\s|\Z)", get_link_markup, self.content) + mark_links = get_markup_with_links(self.content) markup = genshi.input.HTML(mark_links) | genshi.filters.HTMLSanitizer() # the markup is now marked as "safe" -> genshi will output it literally return markup + def get_delete_url(self, absolute=False): + return self.poll_id.get_admin_url(absolute=absolute, + suffix="/delete/%s" % self.get_obfuscated_digest()) + + def get_obfuscated_digest(self): + return hashlib.md5(str(self.id)).hexdigest() + class PollSetting(sqlobject.SQLObject): poll_id = sqlobject.ForeignKey("Poll") @@ -123,6 +126,12 @@ class Poll(sqlobject.SQLObject): related.extend([poll.first for poll in PollRelation.selectBy(second=self.id)]) return related + def get_description_markup(self): + mark_links = get_markup_with_links(self.description) + markup = genshi.input.HTML(mark_links) | genshi.filters.HTMLSanitizer() + # the markup is now marked as "safe" -> genshi will output it literally + return markup + def get_settings(self): current_dict = {} for setting in PollSetting.selectBy(poll_id=self.id): @@ -156,9 +165,11 @@ class Poll(sqlobject.SQLObject): complete_url = self.get_url(absolute=True) title = "%s %s %s" % (config.get('misc', 'twitter_alert_prefix'), self.title[:79], complete_url) - username = config.get('misc', 'twitter_alert_user') - password = config.get('misc', 'twitter_alert_password') - publish_twitter_alert(title, username, password) + twitter_key = config.get('misc', 'twitter_consumer_key') + twitter_secret = config.get('misc', 'twitter_consumer_secret') + twitter_access_key = config.get('misc', 'twitter_access_token_key') + twitter_access_secret = config.get('misc', 'twitter_access_token_secret') + publish_twitter_alert(title, twitter_key, twitter_secret, twitter_access_key, twitter_access_secret) def get_num_of_submitters(self): all_submitters = [submission.submitter for submission in ContentSubmission.selectBy(poll_id=self.id)] @@ -189,8 +200,8 @@ class Poll(sqlobject.SQLObject): def get_submit_url(self, absolute=False): return get_url_string("%s%s/submit" % (BASE_DICT["base_url"], self.hash_key), absolute) - def get_admin_url(self, absolute=False): - return get_url_string("%s%s" % (BASE_DICT["base_url"], self.admin_hash_key), absolute) + def get_admin_url(self, absolute=False, suffix=""): + return get_url_string("%s%s%s" % (BASE_DICT["base_url"], self.admin_hash_key, suffix), absolute) def get_edit_url(self, absolute=False): return get_url_string("%s%s/admin" % (BASE_DICT["base_url"], self.admin_hash_key), absolute) @@ -228,6 +239,16 @@ class PollMesh: return self.related +def get_markup_with_links(text): + def get_link_markup(match): + prefix, url, suffix = match.groups() + # only take the TLD part of the url + short_name = url.split("/")[2] + return """%s%s%s""" % (prefix, url, short_name, suffix) + # surround all urls with html markup + return re.sub(r"(\A|\s|\()(https?://[\w/\?\.\#=;,_\-\~&]*)(\)|\s|\Z)", + get_link_markup, text) + def get_poll_setting_string(key, value): if not key in POLL_SETTINGS.keys(): return "" @@ -382,14 +403,16 @@ def get_new_hash_key(length=16, charset=None): return hash_key -def publish_twitter_alert(text, user, passwd): - api = twitter.Api(username=user, password=passwd) +def publish_twitter_alert(text, key, secret,access_key,access_secret): + api = twitter.Api(consumer_key= key, consumer_secret = secret, access_token_key=access_key, access_token_secret= access_secret) try: api.PostUpdate(text) - except HTTPError: + except urllib2.HTTPError,e: # twitter error, most likely because of a duplicate message # or maybe an authentication failure - pass + print e.code + except urllib2.URLError, e: + print e.reason @bobo.query('/new') @@ -471,6 +494,19 @@ def delete_poll(admin_hash_key=None): poll.delete_poll() return bobo.redirect(BASE_DICT["base_url"]) +@bobo.query('/:admin_hash_key/delete/:submission_id_digest') +def delete_submission(admin_hash_key=None, submission_id_digest=None): + admin_poll_id = get_poll_admin_id(admin_hash_key) + if (not admin_poll_id is None) and (not submission_id_digest is None): + poll = Poll.get(admin_poll_id) + # This loop is slightly expensive, but it does not expose the overall + # count of submissions (via the id). + for submission in ContentSubmission.selectBy(poll_id=poll.id): + if submission.get_obfuscated_digest() == submission_id_digest: + submission.destroySelf() + break + return bobo.redirect(poll.get_admin_url()) + @bobo.query('/:admin_hash_key/admin') def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None, title=None, description=None, settings=None,