Changeset 2799 in products


Ignore:
Timestamp:
Sep 9, 2010 5:37:06 PM (14 years ago)
Author:
mylan
Message:

#195: Fixed context getting in the widget

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.formlib.captcha/trunk/quintagroup/formlib/captcha/widget.py

    r2210 r2799  
    11from DateTime import DateTime 
    22 
    3 from zope.component import getMultiAdapter 
     3try: 
     4    from zope.site.hooks import getSite 
     5except ImportError: 
     6    from zope.app.component.hooks import getSite 
    47from zope.app.form.browser import ASCIIWidget 
    58from zope.app.form.interfaces import ConversionError 
     
    1013 
    1114from Products.CMFCore.utils import getToolByName 
     15from Products.CMFCore.interfaces import ISiteRoot 
    1216 
    1317from quintagroup.captcha.core.utils import decrypt, parseKey, encrypt1, getWord 
     
    1519_ = MessageFactory('quintagroup.formlib.captcha') 
    1620 
     21import logging 
     22 
     23logger = logging.getLogger('quintagroup.formlib.captcha') 
    1724 
    1825class CaptchaWidget(ASCIIWidget): 
     26 
     27    def get_site(self): 
     28        # get from plone.app.form.widgets.wysiwygwdget 
     29        site = getSite() 
     30        while site is not None and not ISiteRoot.providedBy(site): 
     31            site = aq_parent(site) 
     32        return site 
     33 
    1934    def __call__(self): 
    20         context = self.context.context 
    21  
    2235        kwargs = {'type': self.type, 
    2336                  'name': self.name, 
     
    2841                  'extra': self.extra} 
    2942 
    30         portal_url = getToolByName(context, 'portal_url')() 
    31         key = context.getCaptcha() 
     43        site = self.get_site() 
     44        portal_url = getToolByName(site , 'portal_url')() 
     45        key = site.getCaptcha() 
    3246 
    3347        if self._prefix: 
     
    4660    def _toFieldValue(self, input): 
    4761        # Verify the user input against the captcha 
    48         context = self.context.context 
    49         request = context.REQUEST 
    50          
     62 
    5163        # get captcha type (static or dynamic) 
    52         captcha_type = context.getCaptchaType() 
     64        site = self.get_site() 
     65        captcha_type = site.getCaptchaType() 
    5366         
    5467        # validate captcha input 
     
    6073                prefix = '' 
    6174             
    62             hashkey = request.get('%shashkey' % prefix, '') 
    63             decrypted_key = decrypt(context.captcha_key, hashkey) 
     75            hashkey = self.request.get('%shashkey' % prefix, '') 
     76            decrypted_key = decrypt(site.captcha_key, hashkey) 
    6477            parsed_key = parseKey(decrypted_key) 
    6578             
     
    6881             
    6982            if captcha_type == 'static': 
    70                 img = getattr(context, '%s.jpg' % index) 
     83                img = getattr(site, '%s.jpg' % index) 
    7184                solution = img.title 
    7285                enc = encrypt1(input) 
     
    7588                solution = getWord(int(index)) 
    7689             
    77             captcha_tool = getToolByName(context, 'portal_captchas') 
     90            captcha_tool = getToolByName(site, 'portal_captchas') 
    7891            if (enc != solution) or (captcha_tool.has_key(decrypted_key)) or (DateTime().timeTime() - float(date) > 3600): 
    7992                raise ConversionError(_(u'Please re-enter validation code.')) 
Note: See TracChangeset for help on using the changeset viewer.