added link markup

added correct handling for the project's base dir
simplified static request handling
This commit is contained in:
lars 2010-04-15 18:17:05 +00:00
parent f276c11aeb
commit 79c516c90e
3 changed files with 42 additions and 52 deletions

Binary file not shown.

View file

@ -36,7 +36,7 @@
<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_markup_content()}</td>
<td>${one_submission.get_creation_time_string()}</td>
</tr>
</table>

View file

@ -1,27 +1,29 @@
#!/usr/bin/env python2.6
# add the current directory to the python search path
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# add the project directory to the python search path
import sys
sys.path.insert(0, "./")
sys.path.insert(0, BASE_DIR)
import forms
import sqlobject
import bobo
from genshi.template import TemplateLoader
import genshi.filters
import genshi.input
import genshi
import formencode
import uuid
import datetime
import os
import mimetypes
import webob
import mimetypes
import uuid
import re
db_filename = "database.sqlite"
db_filename = os.path.abspath(db_filename)
db_filename = os.path.join(BASE_DIR, "database.sqlite")
database = sqlobject.connectionForURI("sqlite://" + db_filename)
sqlobject.sqlhub.processConnection = database
loader = TemplateLoader(os.path.join(os.path.dirname(__file__), 'templates'), auto_reload=True)
loader = TemplateLoader(os.path.join(BASE_DIR, 'templates'), auto_reload=True)
BASE_DICT = {
"base_url": "/", # the trailing slash is necessary
@ -42,6 +44,18 @@ class ContentSubmission(sqlobject.SQLObject):
def get_creation_time_string(self):
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<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()
# the markup is now marked as "safe" -> genshi will output it literally
return markup
class PollSetting(sqlobject.SQLObject):
poll_id = sqlobject.ForeignKey("Poll")
@ -124,7 +138,7 @@ def validate_poll_setting(key, value):
return value
elif setting_type == bool:
text = value.tolower()
if text in ("0", "false", "no", "off", "disabled"):
if text in ("0", "false", "no", "off", "disabled", ""):
return False
elif text in ("1", "true", "yes", "on", "enabled"):
return True
@ -328,49 +342,25 @@ for table in (Poll, ContentSubmission, PollSetting):
for poll in Poll.select():
print poll
####### Ab hier hat m rumgesaut
### das ist natuerlich voellig falsch, aber so kann ich schnell mit dem Layout anfangen.
### Aender es also ruhig ab!
@bobo.query('/media/:pathname')
def statische_sachen(pathname):
@bobo.query('/media/:p1')
@bobo.query('/media/:p1/:p2')
@bobo.query('/media/:p1/:p2/:p3')
def static_files(p1=None, p2=None, p3=None):
pathlist = [p1, p2, p3]
pathname = os.path.join(BASE_DIR, "templates", "media")
for subdir in pathlist:
if not subdir is None:
pathname = os.path.join(pathname, subdir)
response = webob.Response()
content_type = mimetypes.guess_type(pathname)[0]
if content_type is not None:
response.content_type = content_type
try:
response.body = open("templates/media/"+pathname).read()
response.body = open(pathname).read()
except IOError:
raise bobo.NotFound
return response
@bobo.query('/media/images/:pathname')
def statische_sachen2(pathname):
response = webob.Response()
content_type = mimetypes.guess_type(pathname)[0]
if content_type is not None:
response.content_type = content_type
try:
response.body = open("templates/media/images/"+pathname).read()
except IOError:
raise bobo.NotFound
return response
####### bis hier hier hat m rumgesaut
# this line allows to use wortschlucker with mod_wsgi
# see: http://groups.google.com/group/bobo-web/msg/2ba55fc381658cd1
#application = bobo.Application(bobo_resources=__name__)