Skip to content

Commit

Permalink
More flexible font path. Controller prefix. Mako template.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphbean committed Mar 1, 2012
1 parent cac29f9 commit 32a0067
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions tw2/captcha/plugins/image/vanasco_dowty/__init__.py
Expand Up @@ -12,6 +12,9 @@
font_size_max = Captcha.text_font_size_max

captcha.font__paths = [Captcha.text_font_path]
for font_path in captcha.font__paths:
assert os.path.exists(font_path), \
'The font_path "%s" does not exist' % (font_path,)
captcha.captcha__text__render_mode = Captcha.text_render_mode
captcha.captcha__font_range = (font_size_min, font_size_max)

Expand Down
6 changes: 5 additions & 1 deletion tw2/captcha/templates/captcha.mak
@@ -1,2 +1,6 @@
<%namespace name="tw" module="tw2.core.mako_util"/>
<div ${tw.attrs(attrs=w.attrs)}></div>
<span id="${w.attrs['id']}:wrapper">
<img id="${w.attrs['id']}_img"
src="${w.controller_prefix}/image/${w.payload}"/>
<input type="text" ${tw.attrs(attrs=w.attrs)}/>
</span>
29 changes: 19 additions & 10 deletions tw2/captcha/widgets.py
Expand Up @@ -15,12 +15,10 @@

modname = '.'.join(__name__.split('.')[:-1])

class CaptchaWidget(twc.Widget):
template = "mako:tw2.captcha.templates.captcha"


class Captcha(CaptchaWidget):
resources = CaptchaWidget.resources + [
class Captcha(twc.Widget):
template = "mako:tw2.captcha.templates.captcha"
resources = [
twc.CSSLink(modname=modname, filename="static/ext/captcha.css"),
]

Expand All @@ -37,6 +35,8 @@ class Captcha(CaptchaWidget):
timeout = twc.Param("Time in minutes during which the captcha will be valid.",
default=5)

controller_prefix = twc.Param("URL prefix of captcha controller",
default="/tw2.captcha/")
picture_width = twc.Param("Picture width in pixel.", default=300)
picture_height = twc.Param("Picture width in pixel.", default=100)
picture_bg_color = twc.Param('Picture background color.', default='#DDDDDD')
Expand All @@ -46,8 +46,10 @@ class Captcha(CaptchaWidget):
'Minimal font size for the text on the captcha.', default=30)
text_font_size_max = twc.Param(
'Maximal font size for the text on the captcha.', default=45)
text_font_path = twc.Param('Full path to the font to be used in for the text.',
default= 'tw2/captcha/static/fonts/tuffy/Tuffy.ttf')
text_font_path = twc.Param(
'Full path to the font to be used in for the text. ' +
'Either relative to the package or absolute.',
default='static/fonts/tuffy/Tuffy.ttf')
text_render_mode = twc.Param('Rendering method for the text.',
default='by_letter')
ascii_char = twc.Param('Character allowed in the ascii text',
Expand All @@ -59,6 +61,13 @@ class Captcha(CaptchaWidget):
stop_range = twc.Param('For the equation, maximum number allowed',
default=100)

payload = twc.Variable("Internally used hash of the captcha.")

@classmethod
def post_define(cls):
if not cls.text_font_path[0] == os.path.sep:
base = os.path.sep.join(__file__.split(os.path.sep)[:-1])
cls.text_font_path = base + os.path.sep + cls.text_font_path

def prepare(self):

Expand Down Expand Up @@ -92,9 +101,9 @@ def prepare(self):
for ep in iter_entry_points('tw2.captcha.text_generators', txt_gen):
self.text_generator = ep.load()

payload = self.create_payload()
print "********************", payload
image = self.image(payload)
self.payload = self.create_payload()
print "********************", self.payload
image = self.image(self.payload)

def image(self, value):
"Serve a jpeg for the given payload value."
Expand Down

0 comments on commit 32a0067

Please sign in to comment.