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><th>Submitter</th><th>Content</th><th>Timestamp</th></tr>
<tr py:for="one_submission in poll.get_submissions()"> <tr py:for="one_submission in poll.get_submissions()">
<td>${one_submission.submitter}</td> <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> <td>${one_submission.get_creation_time_string()}</td>
</tr> </tr>
</table> </table>

View file

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