twitter fixed

submission moderation
other minor stuff
This commit is contained in:
lars 2011-04-01 00:44:39 +00:00
parent 19fd6d004e
commit 43cce6432e

View file

@ -23,7 +23,9 @@ import datetime
import mimetypes import mimetypes
import uuid import uuid
import re import re
import hashlib
import twitter import twitter
import urllib2
CONFIG_FILE = os.path.join(BASE_DIR, "wortschlucker.conf") CONFIG_FILE = os.path.join(BASE_DIR, "wortschlucker.conf")
@ -79,17 +81,18 @@ class ContentSubmission(sqlobject.SQLObject):
return str(self.timestamp_creation) return str(self.timestamp_creation)
def get_markup_content(self): def get_markup_content(self):
def get_link_markup(match): mark_links = get_markup_with_links(self.content)
prefix, url, suffix = match.groups()
# only take the TLD part of the url
short_name = url.split("/")[2]
return """%s<a href="%s">%s</a>%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)
markup = genshi.input.HTML(mark_links) | genshi.filters.HTMLSanitizer() markup = genshi.input.HTML(mark_links) | genshi.filters.HTMLSanitizer()
# the markup is now marked as "safe" -> genshi will output it literally # the markup is now marked as "safe" -> genshi will output it literally
return markup 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): class PollSetting(sqlobject.SQLObject):
poll_id = sqlobject.ForeignKey("Poll") 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)]) related.extend([poll.first for poll in PollRelation.selectBy(second=self.id)])
return related 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): def get_settings(self):
current_dict = {} current_dict = {}
for setting in PollSetting.selectBy(poll_id=self.id): for setting in PollSetting.selectBy(poll_id=self.id):
@ -156,9 +165,11 @@ class Poll(sqlobject.SQLObject):
complete_url = self.get_url(absolute=True) complete_url = self.get_url(absolute=True)
title = "%s %s %s" % (config.get('misc', 'twitter_alert_prefix'), title = "%s %s %s" % (config.get('misc', 'twitter_alert_prefix'),
self.title[:79], complete_url) self.title[:79], complete_url)
username = config.get('misc', 'twitter_alert_user') twitter_key = config.get('misc', 'twitter_consumer_key')
password = config.get('misc', 'twitter_alert_password') twitter_secret = config.get('misc', 'twitter_consumer_secret')
publish_twitter_alert(title, username, password) 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): 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)]
@ -189,8 +200,8 @@ class Poll(sqlobject.SQLObject):
def get_submit_url(self, absolute=False): def get_submit_url(self, absolute=False):
return get_url_string("%s%s/submit" % (BASE_DICT["base_url"], self.hash_key), absolute) return get_url_string("%s%s/submit" % (BASE_DICT["base_url"], self.hash_key), absolute)
def get_admin_url(self, absolute=False): def get_admin_url(self, absolute=False, suffix=""):
return get_url_string("%s%s" % (BASE_DICT["base_url"], self.admin_hash_key), absolute) return get_url_string("%s%s%s" % (BASE_DICT["base_url"], self.admin_hash_key, suffix), absolute)
def get_edit_url(self, absolute=False): def get_edit_url(self, absolute=False):
return get_url_string("%s%s/admin" % (BASE_DICT["base_url"], self.admin_hash_key), absolute) 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 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<a href="%s">%s</a>%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): def get_poll_setting_string(key, value):
if not key in POLL_SETTINGS.keys(): if not key in POLL_SETTINGS.keys():
return "" return ""
@ -382,14 +403,16 @@ def get_new_hash_key(length=16, charset=None):
return hash_key return hash_key
def publish_twitter_alert(text, user, passwd): def publish_twitter_alert(text, key, secret,access_key,access_secret):
api = twitter.Api(username=user, password=passwd) api = twitter.Api(consumer_key= key, consumer_secret = secret, access_token_key=access_key, access_token_secret= access_secret)
try: try:
api.PostUpdate(text) api.PostUpdate(text)
except HTTPError: except urllib2.HTTPError,e:
# twitter error, most likely because of a duplicate message # twitter error, most likely because of a duplicate message
# or maybe an authentication failure # or maybe an authentication failure
pass print e.code
except urllib2.URLError, e:
print e.reason
@bobo.query('/new') @bobo.query('/new')
@ -471,6 +494,19 @@ def delete_poll(admin_hash_key=None):
poll.delete_poll() poll.delete_poll()
return bobo.redirect(BASE_DICT["base_url"]) 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') @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,