Plone 4+ imports (removes warnings under Plone 4.1)

svn path=/plone.formwidget.captcha/trunk/; revision=48779
This commit is contained in:
Thomas Desvenain 2011-04-13 13:02:32 +00:00
parent d291a02f4a
commit 5ab5eeeab3
2 changed files with 30 additions and 24 deletions

View File

@ -4,6 +4,7 @@ Changelog
1.0b3 - unreleased
------------------
* Plone 4+ imports (removes warnings under Plone 4.1)
1.0b2 - 2010-09-02
------------------
@ -15,22 +16,22 @@ Changelog
* Move to plone.app.discussion-captcha feature declaration to meta.zcml.
[timo]
* Fix verify method for empty string input values. This is necessary for
* Fix verify method for empty string input values. This is necessary for
plone.app.discussion 1.0b5 compatibility.
[timo]
[timo]
1.0b1 - 2010-06-02
------------------
* Fix _generate_words method. The "nowish" variable tells us in which 5 minutes
slot we are in. Therefore we increment the time slot by 1, not by 5 (minutes).
* Fix _generate_words method. The "nowish" variable tells us in which 5 minutes
slot we are in. Therefore we increment the time slot by 1, not by 5 (minutes).
[tbesluau]
* Declare that plone.formwidget.captcha provides a Captcha field that can be
* Declare that plone.formwidget.captcha provides a Captcha field that can be
used by plone.app.discussion to add a Captcha field to comment forms.
[timo]
1.0a2 - 2010-01-28
------------------

View File

@ -2,7 +2,7 @@
import os.path
import random
import re
import sha
import string
import sys
import time
@ -13,7 +13,12 @@ from zope.interface import implements
from zope.component import getUtility
from Acquisition import aq_inner
from App.config import getConfiguration
from Globals import package_home
try:
# Plone 4+
from App.Common import package_home
except:
from Globals import package_home
from Products.Five import BrowserView
from plone.keyring.interfaces import IKeyManager
@ -33,10 +38,10 @@ _TEST_TIME = None
class Captcha(BrowserView):
implements(ICaptchaView)
_session_id = None
__name__ = 'captcha'
def _setcookie(self, id):
"""Set the session cookie"""
resp = self.request.response
@ -64,24 +69,24 @@ class Captcha(BrowserView):
self._setcookie(self._session_id)
# Put the cookie value into the request for immediate consumption
self.request.cookies[COOKIE_ID] = self._session_id
def _generate_words(self):
"""Create words for the current session
We generate one for the current 5 minutes, plus one for the previous
5. This way captcha sessions have a livespan of 10 minutes at most.
"""
session = self.request[COOKIE_ID]
nowish = _TEST_TIME or int(time.time() / 300)
# The line above defines nowish, which tells us what five minutes slot
# The line above defines nowish, which tells us what five minutes slot
# we're in. Indeed, every second, int(time.time()) increments by 1, so
# int(time.time() / 300) will increment by 1 every 5 minutes.
secret = getUtility(IKeyManager).secret()
seeds = [sha.new(secret + session + str(nowish)).digest(),
sha.new(secret + session + str(nowish - 1)).digest()]
# The line above generates a seed based on the "nowish" of 5 minutes ago.
words = []
for seed in seeds:
word = []
@ -90,19 +95,19 @@ class Captcha(BrowserView):
word.append(CHARS[index])
words.append(''.join(word))
return words
def _url(self, type):
return '%s/@@%s/%s' % (
aq_inner(self.context).absolute_url(), self.__name__, type)
def image_tag(self):
self._generate_session()
return '<img src="%s" />' % (self._url('image'),)
def audio_url(self):
self._generate_session()
return self._url('audio')
def verify(self, input):
if not input:
return False
@ -114,11 +119,11 @@ class Captcha(BrowserView):
self.request.response.expireCookie(COOKIE_ID, path='/')
except KeyError:
pass # No cookie
return result
# Binary data subpages
def _setheaders(self, type):
resp = self.request.response
resp.setHeader('content-type', type)
@ -126,14 +131,14 @@ class Captcha(BrowserView):
resp.setHeader('cache-control', 'no-cache, no-store')
resp.setHeader('pragma', 'no-cache')
resp.setHeader('expires', 'now')
def image(self):
"""Generate a captcha image"""
self._verify_session()
self._setheaders('image/png')
return skimpyAPI.Png(self._generate_words()[0],
fontpath=VERAMONO).data()
def audio(self):
"""Generate a captcha audio file"""
self._verify_session()