Changeset 7284


Ignore:
Timestamp:
6 Dec 2011, 15:50:22 (13 years ago)
Author:
uli
Message:
  • Register captchas as utils rather than adapters.
  • Sketches of captcha chooser.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/captcha.py

    r7283 r7284  
    2424import urllib2
    2525from zope import schema
     26from zope.component import getUtilitiesFor
    2627from zope.interface import Interface
    2728from zope.publisher.interfaces.http import IHTTPRequest
     
    9192    By default we require no configuration data.
    9293    """
     94
     95class ICaptchaChooser(Interface):
     96    """A chooser that collects available captchas.
     97    """
     98    def getAvailCaptchas(self):
     99        """Return a dict of available captchas with registered names as keys.
     100        """
     101
     102    def getCaptcha(self):
     103        """Get captcha chosen for a certain site or default.
     104        """
     105
     106    def getConfigData(self, captcha):
     107        """Get a dict of config data for a certain captcha.
     108        """
     109
     110    def setConfigData(self, captcha, key, value):
     111        """Set a certain configuration key for a certain captcha.
     112        """
     113
     114#
     115# Global captcha chooser
     116#
     117from zope.publisher.browser import TestRequest
     118class CaptchaChooser(grok.GlobalUtility):
     119
     120    grok.implements(ICaptchaChooser)
     121
     122    def getAvailCaptchas(self):
     123        """Get all available captchas registered as utils for ICaptcha.
     124
     125        The default captcha (as it most probably is a copy of another
     126        registered captcha) is left out of the result.
     127
     128        Result will be a dict with registration names as keys and the
     129        specific captcha instances as values.
     130        """
     131        result = getUtilitiesFor(ICaptcha)
     132        return dict([x for x in result
     133                       if x != u''])
     134
     135    def getCaptcha(self):
     136        site = grok.getSite()
     137        name = ''
     138        if site is None:
     139            # return the default captcha, registered without name
     140            return getUtility(ICaptcha)
     141        name = site.get('configuration', None).get('captcha', u'')
     142        return getUtility(ICaptcha, name=name)
    93143
    94144#
     
    109159        return
    110160
    111 class NullCaptcha(grok.Adapter):
     161class NullCaptcha(object):
    112162    """A captcha that does not expect any input.
    113163
     
    120170    maintainer decides not to use it at all.
    121171    """
    122     grok.context(IHTTPRequest)
    123172    grok.implements(ICaptcha)
    124     grok.name('No captcha')
    125 
    126     def __init__(self, context=None):
    127         self.context = None
     173    #grok.name(u'No captcha')
    128174
    129175    def verify(self, captcha_request):
     
    133179        return u''
    134180
     181
     182# This captcha is registered twice: one time as a 'no captcha' captcha
     183# and then also as the default captcha (with empty name)
     184grok.global_utility(NullCaptcha, name=u'No captcha')
     185grok.global_utility(NullCaptcha, name=u'')
    135186
    136187#
Note: See TracChangeset for help on using the changeset viewer.