Changeset 678
- Timestamp:
- 12/01/06 11:56:48
- Files:
-
- qPloneCaptchas/trunk/Extensions/Install.py (modified) (3 diffs)
- qPloneCaptchas/trunk/config.py (modified) (1 diff)
- qPloneCaptchas/trunk/skins/plone_captchas/dynamic/getCaptchaImage.py (modified) (1 diff)
- qPloneCaptchas/trunk/skins/plone_captchas/prefs_captchas_setup_form.cpt (modified) (4 diffs)
- qPloneCaptchas/trunk/utils.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
qPloneCaptchas/trunk/Extensions/Install.py
r677 r678 17 17 18 18 safeEditProperty(self, 'captcha_key', generateKey(8)) 19 20 # add Property sheet to portal_properies 21 pp = getToolByName(self, 'portal_properties') 22 if not 'qPloneCaptchas' in pp.objectIds(): 23 pp.addPropertySheet(id='qPloneCaptchas', title= '%s Properties' % 'qPloneCaptchas') 24 out.write("Adding %s property sheet to portal_properies\n" % 'qPloneComments' ) 25 props_sheet = pp['qPloneCaptchas'] 26 updateProperties(props_sheet, out, PROPERTIES) 27 28 19 29 20 30 Layers = [] … … 87 97 controlpanel_tool = getToolByName(self, 'portal_controlpanel') 88 98 controlpanel_tool.unregisterConfiglet(CONFIGLET_ID) 99 # Remove Product's property sheet from portal_properties 100 pp = getToolByName(self, 'portal_properties') 101 if 'qPloneCaptchas' in pp.objectIds(): 102 pp.manage_delObjects(ids=('qPloneCaptchas',)) 89 103 90 104 def updateProperties(pp_ps, out, *args): … … 104 118 addTool = self.manage_addProduct['qPloneCaptchas'].manage_addTool 105 119 addTool('CaptchaTool') 120 121 def updateProperties(pp_ps, out, *args): 122 for prop in args: 123 for prop_id, prop_value, prop_type in prop: 124 if not pp_ps.hasProperty(prop_id): 125 pp_ps.manage_addProperty(prop_id, prop_value, prop_type) 126 out.write("Adding %s property to %s property sheet\n" % (prop_id, 'qPloneComments')) qPloneCaptchas/trunk/config.py
r677 r678 12 12 CONFIGLET_ID = "prefs_captchas_setup_form" 13 13 CONFIGLET_NAME = "qPloneCaptchas setup" 14 PROPERTIES = (('static_captchas', 'True', 'boolean'),) 14 15 DEFAULT_IMAGE_SIZE = 27 16 DEFAULT_BG = 'gray' 17 DEFAULT_FONT_COLOR = 'black' 18 DEFAULT_PERIOD = 0.05 19 DEFAULT_AMPLITUDE = 5 20 DEFAULT_OFFSET = (0.5, 0.5) 21 DEFAULT_DISTORTION = [DEFAULT_PERIOD, DEFAULT_AMPLITUDE, DEFAULT_OFFSET] 22 23 PROPERTIES = (('image_size', DEFAULT_IMAGE_SIZE, 'int'), 24 ('background', DEFAULT_BG, 'string'), 25 ('font_color', DEFAULT_FONT_COLOR, 'string'), 26 ('period', DEFAULT_PERIOD, 'float'), 27 ('amplitude', DEFAULT_AMPLITUDE, 'float'), 28 ('random_params', True, 'boolean')) 15 29 16 30 try: qPloneCaptchas/trunk/skins/plone_captchas/dynamic/getCaptchaImage.py
r677 r678 9 9 from Products.qPloneCaptchas.utils import gen_captcha, decrypt, \ 10 10 getWord, parseKey 11 from Products.CMFCore.utils import getToolByName 12 13 propTool = getToolByName(context, 'portal_properties') 14 captchaProps = propTool['qPloneCaptchas'] 15 16 period = captchaProps.getProperty('period') 17 amplitude = captchaProps.getProperty('amplitude') 11 18 12 19 hk = context.REQUEST.traverse_subpath[0] 13 20 dk = decrypt(context.captcha_key, hk) 14 21 key = parseKey(dk)['key'] 15 im = gen_captcha(getWord(int(key)), 27) 22 kwargs = {'text': getWord(int(key)), 23 'size': captchaProps.getProperty('image_size'), 24 'bkground': captchaProps.getProperty('background'), 25 'font_color': captchaProps.getProperty('font_color'), 26 'distortion':[period, amplitude, (0.0, 0.0)]} 27 28 im = gen_captcha(**kwargs) 16 29 17 30 context.REQUEST.RESPONSE.setHeader('Content-Type', 'image/jpeg') qPloneCaptchas/trunk/skins/plone_captchas/prefs_captchas_setup_form.cpt
r677 r678 1 <html xmlns="http://www.w3.org/1999/xhtml" 2 xml:lang="en" 1 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" 3 2 lang="en" 4 3 i18n:domain="plonecaptchas" 5 4 metal:use-macro="here/prefs_main_template/macros/master"> 5 6 <metal:block fill-slot="top_slot" 7 tal:define="dummy python:request.set('disable_border',1)" /> 6 8 7 9 <body> … … 12 14 <div metal:define-macro="main" i18n:domain="plonecaptchas"> 13 15 <div class="configlet"> 14 <div class="documentEditable">15 16 <div class="contentActions">17 18 </div>19 <div class="documentContent" metal:define-slot="prefs_content">20 16 <h1 i18n:translate="heading_comments_setup">Plone Captchas Setup</h1> 21 17 … … 55 51 </fieldset> 56 52 57 <fieldset> 58 <legend i18n:translate="legend_setup_options">captcha image setup options</legend> 59 <div tal:define="ct here/getCaptchaType;"> 60 <input name="static_captchas" 61 id="use_static_captchas" 62 type="radio" 63 value="static" 64 tal:attributes="checked python:test(ct=='static', 'checked', '')"/> 65 <label for="use_static_captchas">Use static captchas</label> 66 <br /> 67 <input name="static_captchas" 68 id="use_dynamic_captchas" 69 type="radio" 70 disabled="" 71 value="dynamic" 72 tal:attributes="checked python:test(ct=='dynamic', 'checked', ''); 73 disabled python:test(context.checkPIL(), '', 'disabled');"/> 74 <label for="use_dynamic_captchas">Use dynamic captchas</label> 53 <fieldset tal:define="props_sheet here/portal_properties/qPloneCaptchas"> 54 <legend i18n:translate="legend_setup_options">Dynamic captchas image setup options</legend> 55 56 <div class="field" 57 tal:define="property_id string:image_size; 58 prop_value python:props_sheet.getProperty(property_id, None);" 59 tal:condition="python:props_sheet.hasProperty(property_id)"> 60 61 <input type="text" 62 name="" 63 id="" 64 tabindex ="" 65 tal:attributes="name property_id; 66 id property_id; 67 value prop_value; 68 tabindex tabindex/next;"/> 69 70 <label i18n:translate="string:${property_id}" 71 for="" 72 tal:attributes="for string:${property_id}"> 73 Font size 74 </label> 75 75 </div> 76 77 <div class="field" 78 tal:define="property_id string:background; 79 prop_value python:props_sheet.getProperty(property_id, None);" 80 tal:condition="python:props_sheet.hasProperty(property_id)"> 81 82 <input type="text" 83 name="" 84 id="" 85 tabindex ="" 86 tal:attributes="name property_id; 87 id property_id; 88 value prop_value; 89 tabindex tabindex/next;"/> 90 91 <label i18n:translate="string:${property_id}" 92 for="" 93 tal:attributes="for string:${property_id}"> 94 Background 95 </label> 96 </div> 97 98 <div class="field" 99 tal:define="property_id string:font_color; 100 prop_value python:props_sheet.getProperty(property_id, None);" 101 tal:condition="python:props_sheet.hasProperty(property_id)"> 102 103 <input type="text" 104 name="" 105 id="" 106 tabindex ="" 107 tal:attributes="name property_id; 108 id property_id; 109 value prop_value; 110 tabindex tabindex/next;"/> 111 112 <label i18n:translate="string:${property_id}" 113 for="" 114 tal:attributes="for string:${property_id}"> 115 Font color 116 </label> 117 </div> 118 119 <div class="field" 120 tal:define="property_id string:period; 121 prop_value python:props_sheet.getProperty(property_id, None);" 122 tal:condition="python:props_sheet.hasProperty(property_id)"> 123 124 <input type="text" 125 name="" 126 id="" 127 tabindex ="" 128 tal:attributes="name property_id; 129 id property_id; 130 value prop_value; 131 tabindex tabindex/next;"/> 132 133 <label i18n:translate="string:${property_id}" 134 for="" 135 tal:attributes="for string:${property_id}"> 136 Period 137 </label> 138 </div> 139 140 <div class="field" 141 tal:define="property_id string:amplitude; 142 prop_value python:props_sheet.getProperty(property_id, None);" 143 tal:condition="python:props_sheet.hasProperty(property_id)"> 144 145 <input type="text" 146 name="" 147 id="" 148 tabindex ="" 149 tal:attributes="name property_id; 150 id property_id; 151 value prop_value; 152 tabindex tabindex/next;"/> 153 154 <label i18n:translate="string:${property_id}" 155 for="" 156 tal:attributes="for string:${property_id}"> 157 Amplitude 158 </label> 159 </div> 160 161 <div class="field" 162 tal:define="property_id string:random_params; 163 prop_value python:props_sheet.getProperty(property_id, None);" 164 tal:condition="python:props_sheet.hasProperty(property_id)"> 165 166 <input type="checkbox" 167 name="" 168 id="" 169 tabindex ="" 170 tal:attributes="name property_id; 171 id property_id; 172 value prop_value; 173 tabindex tabindex/next;"/> 174 175 <label i18n:translate="string:${property_id}" 176 for="" 177 tal:attributes="for string:${property_id}"> 178 Random values 179 </label> 180 </div> 181 76 182 </fieldset> 77 183 … … 89 195 </div> 90 196 </div> 91 </div>92 </div>93 197 </div> 94 198 </body> qPloneCaptchas/trunk/utils.py
r677 r678 4 4 from random import randint 5 5 from Products.qPloneCaptchas.data import basic_english 6 from Products.qPloneCaptchas.config import havePIL, CAPTCHAS_COUNT6 from Products.qPloneCaptchas.config import * 7 7 from DateTime import DateTime 8 8 import re … … 15 15 return md5.new(s).hexdigest().upper() 16 16 17 def gen_captcha( text, fnt_sz, fmt='JPEG'):17 def gen_captcha(**kwargs): 18 18 """Generate a captcha image""" 19 19 import ImageFile … … 28 28 from cStringIO import StringIO 29 29 30 amplitude = random.uniform(3, 6.5) 31 period = random.uniform(0.04, 0.1) 32 offset = (random.uniform(0, math.pi * 2 / period), 33 random.uniform(0, math.pi * 2 / period)) 30 if kwargs.has_key('text'): 31 text = kwargs['text'] 32 else: 33 text = None 34 35 if kwargs.has_key('size'): 36 fnt_sz = kwargs['size'] 37 else: 38 fnt_sz = DEFAULT_IMAGE_SIZE 39 40 if kwargs.has_key('bkground'): 41 bkground = kwargs['bkground'] 42 else: 43 bkground = DEFAULT_BG 44 45 if kwargs.has_key('font_color'): 46 font_color = kwargs['font_color'] 47 else: 48 font_color = DEFAULT_FONT_COLOR 49 50 if kwargs.has_key('distortion'): 51 distortion = kwargs['distortion'] 52 else: 53 distortion = DEFAULT_DISTORTION 54 55 period = distortion[0] 56 amplitude = distortion[1] 57 offset = distortion[2] 34 58 35 59 def getTransform(image): … … 46 70 FONT_PATH = DATA_PATH + '/fonts' 47 71 48 fgcolor = 'black'; bgcolor = 'gray'; textColor='black'49 50 72 #select font for captcha text 51 73 ALL_FONTS=('Bd', 'It', 'MoBI', 'Mono', 'Se', 'BI', 'MoBd', 'MoIt', 'SeBd', '') … … 55 77 56 78 #------------------------------render background1 ----------------------- 57 image = Image.new('RGB', (textSize[0]+ 10,textSize[1]+10), bgcolor)58 image.paste(b gcolor)79 image = Image.new('RGB', (textSize[0]+7,textSize[1]+7), bkground) 80 image.paste(bkground) 59 81 #------------------------------render Text2 ------------------------ 60 82 draw = ImageDraw.Draw(image) … … 63 85 y = int((image.size[1] - textSize[1]) * alignment[1] + 0.5) 64 86 65 draw.text((x,y), text, font=font, fill= textColor)87 draw.text((x,y), text, font=font, fill=font_color) 66 88 67 89 #------------------------------render Distortion3 ----------------------- … … 107 129 img = image.transform(image.size, Image.MESH, mesh, Image.BILINEAR) 108 130 109 #---------------------------------------------------------------------------110 131 # save the image to a file 111 img.save(outFile, format= fmt)132 img.save(outFile, format='jpeg') 112 133 outFile.seek(0) 113 134 src = outFile.read() … … 150 171 return {'date': ps.group(1), 'key':ps.group(2)} 151 172 152 """153 def addExpiredKey(context, key):154 context.portal_captchas.new(key)155 """156 157 173 def toHex(s): 158 174 lst = []
