Commits

al...@alex3.resheteva.lan  committed 8f7c813

added recaptcha support and requirement

  • Participants
  • Parent commits 7c9dd4b

Comments (0)

Files changed (5)

           'setuptools',
           'genshi',
           'tw.output',
+          'tw.recaptcha',
           # -*- Extra requirements: -*-
       ],
       extras_require = {

File tgext/registration2/controllers.py

 
 from pylons import request
-from tg import expose, redirect, config, validate, require, flash, TGController
+from tg import expose, redirect, config, validate, require, flash, TGController, url
 import repoze.what.predicates as identity
 from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin
 import pkg_resources
 import string
 import random
 from email.MIMEText import MIMEText
+
+import registration_config
+cfg = config.get('registration_config', None)
+if cfg:
+    registration_config.readfile(cfg)
+config.registration_config = registration_config.registration
+
 from widgets import new_user_form, edit_user_form, lost_password_form, \
         delete_user_form
 
+
 model = config.model
 DBSession = config.DBSession
 user_class = config.sa_auth['user_class']
         super(UserRegistration, self).__init__()
         random.seed()
         self.hash_salt = ''.join([random.choice(string.printable) for i in range(20)])
-        import registration_config
-        cfg = config.get('registration_config', None)
-        if cfg:
-            registration_config.readfile(cfg)
-        self.config = registration_config.registration
+        self.config = config.registration_config
     
     @expose()
     def index(self):
         if identity.not_anonymous():
-            redirect(request.path + '/edit_user')
+            redir = 'edit_user'
         else:
-            redirect(request.path + '/new')
+            redir = 'new'
+        return redirect(url([request.path, redir]))
             
     @expose(template='tgext.registration2.templates.new')
     def new(self, tg_errors=None, **kw):
         
     @expose(template='tgext.registration2.templates.create')
     @validate(form=new_user_form, error_handler=new)
-    def create(self, user_name, email, email2, display_name, password1, password2):
+    def create(self, user_name, email, email2, display_name, password1, password2, **kw):
         if identity.not_anonymous():
             redirect('./edit_user')
         key = self.validation_hash(email + display_name + password1)
                            display_name=display_name,
                            password=password1,
                            validation_key=key
-                          )  
+                          )
         DBSession.add(pend)
         error_msg = None
         try:
             except smtplib.SMTPRecipientsRefused, args:
                 msg = _("The provided new email was refused by our server, please provide a vali    d email.")
 
-        flash(msg)
-        redirect('./edit_user')
+        if msg:
+            flash(msg)
+        return redirect('./edit_user')
         
     def mail_changed_email_validation(self, new_email):
         """Sends an email out that has validation information for changed email addresses.

File tgext/registration2/registration.cfg

 
 # The Subject line that will be on the validation email for changed email addresses
 registration.mail.changed_email.subject = "Your email address change"
+
+# The public and private key for reCAPTCHA. You may obtain these by registering 
+# at reCAPTCHA
+registration.recaptcha.public_key = 'you know it'
+registration.recaptcha.private_key = 'you know it'

File tgext/registration2/registration_config.py

 registration.mail.new = _empty()
 registration.verified_user = _empty()
 registration.unverified_user = _empty()
+registration.recaptcha = _empty()
 
 def readfile(filename):
     execfile(filename)

File tgext/registration2/widgets.py

 from tw.forms import TableForm, TextField, PasswordField
 from tw.output import OutputText
 from tw.api import WidgetsList
+from tw.recaptcha import ReCaptchaWidget
+from tw.recaptcha.validator import ReCaptchaValidator
 
 import model
 
 from pylons.i18n import ugettext as _
 
+from tg import config
+
+
 class NewUserFields(WidgetsList):
     
     user_name = TextField('user_name',
                     label= _("Password (again)"),
                     help_text=_("Same password as above (the two should match)."))
                     
+    recaptcha_response_field = ReCaptchaWidget(
+                    label= _("reCAPTCHA"),
+                    public_key=config.registration_config.recaptcha.public_key,
+                    use_ssl=True,
+                    )
+                    
 class ExistingUserFields(WidgetsList):
     
     user_name = OutputText('user_name',
         return value
         
 class TGSchema(Schema):    
-    filter_extra_fields = True
+    filter_extra_fields = False
     allow_extra_fields = True
+    ignore_key_missing = False
     
 class NewUserSchema(TGSchema):    
 
     display_name = UnicodeString(not_empty=True, strip=True, max=255)
     password1 = UnicodeString(not_empty=True, max=40)
     password2 = UnicodeString(not_empty=True, max=40)
+    recaptcha_response_field = ReCaptchaValidator(config.registration_config.recaptcha.private_key, 
+                                                  request.remote_addr),
     chained_validators = [FieldsMatch('password1', 'password2'),
-                          FieldsMatch('email', 'email2')]
+                          FieldsMatch('email', 'email2'),
+                         ]
     
 class ExistingUserSchema(TGSchema):
     email = All(Email(not_empty=True, max=255),