Commits

Anonymous committed 698e702

added the option to configure the location of each registration related
html or email template file.

Comments (0)

Files changed (4)

tgext/registration2/controllers.py

 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
+from pkg_resources import resource_string
 import logging
 import datetime
 import sha
 cfg = config.get('registration_config', None)
 if cfg:
     registration_config.readfile(cfg)
-config.registration_config = registration_config.registration
+config.reg_config = registration_config.registration
 
 from widgets import new_user_form, edit_user_form, lost_password_form, \
         delete_user_form
         super(UserRegistration, self).__init__()
         random.seed()
         self.hash_salt = ''.join([random.choice(string.printable) for i in range(20)])
-        self.config = config.registration_config
     
     @expose()
     def index(self):
             redir = 'new'
         return redirect(url([request.path, redir]))
             
-    @expose(template='tgext.registration2.templates.new')
+    @expose(template=config.reg_config.templates.new)
     def new(self, tg_errors=None, **kw):
         if identity.not_anonymous():
             redirect('./edit_user')
         return dict(form=new_user_form, action='./create', 
                     submit_text=_('Create Account'))
         
-    @expose(template='tgext.registration2.templates.create')
+    @expose(template=config.reg_config.templates.create)
     @validate(form=new_user_form, error_handler=new)
     def create(self, user_name, email, email2, display_name, password1, password2, 
                recaptcha_response_field, **kw):
                 # some other error code ?
                 error_msg = '%s' % error_message
     
-        if self.config.unverified_user.groups and not error_msg:
+        if config.reg_config.unverified_user.groups and not error_msg:
             # we have unverified_user.groups.  Add the user to the User table
             # and add the appropriate groups
             user = self.promote_pending_user(pend)
             
         return dict(name=display_name, email=email, error_msg=error_msg)
 
-    @expose(template='tgext.registration2.templates.validate')
+    @expose(template=config.reg_config.templates.validate)
     def validate_new_user(self, user='', key=''):
         pend = retrieve_one(pendinguser, user_name=user)
         if pend and pend.validation_key == key:
-            if self.config.unverified_user.groups:
+            if config.reg_config.unverified_user.groups:
                 # The pending user is already in the Users table
                 new_user = retrieve_one(user_class, user_name=user)
                 self.remove_all_groups(new_user)
             log.info('%s Bad validation using user=%s validation_key=%s' % 
                         (request.remote_addr, user, key))
             return dict(is_valid=False, 
-                        admin_email=self.config.mail.admin_email)
+                        admin_email=config.reg_config.mail.admin_email)
             
     def promote_pending_user(self, pending_user):
         """Copies a pending user from pending and into the official 'users'.
                                           key=pending_user.validation_key))
         url = '%s/validate_new_user?%s' % (reg_base_url, queryargs)
         
-        body = pkg_resources.resource_string(__name__, 
-                        'templates/email_body_new.txt')
+        _module, _file = config.reg_config.email_body.new
+        body = resource_string(_module, _file)
         
         self.send_email(pending_user.email_address, 
-                        self.config.mail.admin_email, 
-                        _(self.config.mail.new.subject), 
+                        config.reg_config.mail.admin_email, 
+                        _(config.reg_config.mail.new.subject), 
                         body % {'validation_url': url})
      
-    @expose(template='tgext.registration2.templates.lost_password')
+    @expose(template=config.reg_config.templates.lost_password)
     def lost_password(self, tg_errors=None, **kw):
         "Show the lost password form."
         if identity.not_anonymous():
             redirect('./edit_user')
-        policy = self.config.lost_password_policy
+        policy = config.reg_config.lost_password_policy
         return dict(policy=policy, form=lost_password_form, 
                     action="recover_lost_password")
         
-    @expose(template="tgext.registration2.templates.recover_lost_password")
+    @expose(template=config.reg_config.templates.recover_lost_password)
     @validate(form=lost_password_form, error_handler=lost_password)
     def recover_lost_password(self, email=None, username=None):
         "Resets (or mails) a user's forgotten password."
             redirect('./edit_user')
         reset_password = user = user_email = None
         user = retrieve_one(user_class, email_address=email, user_name=username)
-        policy = self.config.lost_password_policy
+        policy = config.reg_config.lost_password_policy
 
         log.info('%s Recover lost password request for user=%s email=%s' % 
                  (request.remote_addr, username, email))
             for i in range(0, random.choice((6, 7, 8, 9))):
                 new_pw = '%s%s' % (new_pw, random.choice(chars))
 
-            body = pkg_resources.resource_string(__name__, 
-                        './templates/email_body_reset_password.txt')
+            _module, _file = config.reg_config.email_body.reset_password
+            body = resource_string(_module, _file)
             reg_base_url = self.registration_base_url()
 
             key = self.validation_hash(email + user.display_name + new_pw)
             queryargs = urllib.urlencode(dict(user=pend.user_name, key=key))
             url = '%s/validate_reset_password?%s' % (reg_base_url, queryargs)
             self.send_email(user.email_address, 
-                            self.config.mail.admin_email, 
-                            _(self.config.mail.lost_password.subject), 
+                            config.reg_config.mail.admin_email, 
+                            _(config.reg_config.mail.lost_password.subject), 
                             body % {'password': new_pw, 'validation_url': url, 'user_name':user.user_name})
             user_email = user.email_address
             reset_password = True
         elif user and policy == 'send_current':  # sending the current password
-            body = pkg_resources.resource_string(__name__, 
-                        './templates/email_body_lost_password.txt')
+            _module, _file = config.reg_config.email_body.lost_password
+            body = resource_string(_module, _file)
             self.send_email(user.email_address,
-                            self.config.mail.admin_email, 
-                            _(self.config.mail.lost_password.subject),
+                            config.reg_config.mail.admin_email, 
+                            _(config.reg_config.mail.lost_password.subject),
                             body % {'password': user.password, 
                                     'user_name': user.user_name})
             user_email = user.email_address
 
         return dict(email=user_email, reset_password=reset_password)
         
-    @expose(template='tgext.registration2.templates.validate_reset_password')
+    @expose(template=config.reg_config.templates.validate_reset_password)
     def validate_reset_password(self, user='', key=''):
         pend = retrieve_one(pendinguser, user_name=user)
         if pend and pend.validation_key == key:
             log.info('%s Bad validation using user=%s validation_key=%s' % 
                         (request.remote_addr, user, key))
             return dict(is_valid=False, 
-                        admin_email=self.config.mail.admin_email)
+                        admin_email=config.reg_config.mail.admin_email)
 
-    @expose(template='tgext.registration2.templates.edit_user')
+    @expose(template=config.reg_config.templates.edit_user)
     @require(identity.not_anonymous())
     def edit_user(self, tg_errors=None, **kw):
         "Edit current user information."
                                           key=validation_key))
         url = '%s/validate_email_change?%s' % (reg_base_url, queryargs)
         
-        body = pkg_resources.resource_string(__name__, 
-                                    'templates/changed_email.txt')
+        _module, _file = config.reg_config.email_body.changed_email
+        body = resource_string(_module, _file)
         self.send_email(new_email,
-                    self.config.mail.admin_email, 
-                    _(self.config.mail.changed_email.subject),
+                    config.reg_config.mail.admin_email, 
+                    _(config.reg_config.mail.changed_email.subject),
                     body % {'validation_url': url})
     
-    @expose(template='tgext.registration2.templates.validate_email')
+    @expose(template=config.reg_config.templates.validate_email)
     def validate_email_change(self, email, key):
         "Validate the email address change and update the database appropriately."
         is_valid = False
-        admin_email = self.config.mail.admin_email
+        admin_email = config.reg_config.mail.admin_email
         email_change = retrieve_one(emailchange, new_email_address=email)
         if not email_change:
             return dict(is_valid=False, admin_email=admin_email)
                     name=user.display_name,
                     admin_email=admin_email)
 
-    @expose(template='tgext.registration2.templates.delete_user')
+    @expose(template=config.reg_config.templates.delete_user)
     @require(identity.not_anonymous())
     def delete_user(self, **kw):
         "Remove a user from the application."
         
     def add_standard_groups(self, user):
         "Add the user to the groups specified in the config file."
-        self.add_groups(user, self.config.verified_user.groups)
+        self.add_groups(user, config.reg_config.verified_user.groups)
     
     def add_unverified_groups(self, user):
         "Adds the user to the unverified user groups specified in the config file."
-        self.add_groups(user, self.config.unverified_user.groups)
+        self.add_groups(user, config.reg_config.unverified_user.groups)
         
     def add_groups(self, user, group_list):
         "Adds the user to each of the groups in the group_list sequence."
         return '%s%s' % (request.host_url, path)
         
     def send_email(self, to_addr, from_addr, subject, body):
-        """Send an email.
-        """
+        "Send an email."
         # Using turbomail if it exists, 'dumb' method otherwise
         if turbomail and config.get('mail.on'):
             msg = turbomail.Message(from_addr, to_addr, subject)
             msg['From'] = from_addr
             msg['To'] = to_addr
         
-            smtp = smtplib.SMTP(self.config.mail.smtp_server, 
-                                self.config.mail.smtp_server_port)
-            if self.config.mail.smtp_server_username:
-                smtp.login(self.config.mail.smtp_server_username, 
-                           self.config.mail.smtp_server_password)
+            smtp = smtplib.SMTP(config.reg_config.mail.smtp_server, 
+                                config.reg_config.mail.smtp_server_port)
+            if config.reg_config.mail.smtp_server_username:
+                smtp.login(config.reg_config.mail.smtp_server_username, 
+                           config.reg_config.mail.smtp_server_password)
             smtp.sendmail(from_addr, to_addr, msg.as_string())
             smtp.quit()

tgext/registration2/registration.cfg

 # at reCAPTCHA
 registration.recaptcha.public_key = 'you know it'
 registration.recaptcha.private_key = 'you know it'
+
+# The templates for the pages served by the registration controller.
+registration.templates.new = 'tgext.registration2.templates.new'
+registration.templates.create = 'tgext.registration2.templates.create'
+registration.templates.validate = 'tgext.registration2.templates.validate'
+registration.templates.lost_password = 'tgext.registration2.templates.lost_password'
+registration.templates.recover_lost_password = 'tgext.registration2.templates.recover_lost_password'
+registration.templates.validate_reset_password = 'tgext.registration2.templates.validate_reset_password'
+registration.templates.edit_user = 'tgext.registration2.templates.edit_user'
+registration.templates.validate_email = 'tgext.registration2.templates.validate_email'
+registration.templates.delete_user = 'tgext.registration2.templates.delete_user'
+
+# The templates for the email body of various messages.
+# The values are tuples of (_module_name_, _file_under_module_).
+registration.email_body.new = \
+    ('tgext.registration2', 'templates/email_body_new.txt')
+registration.email_body.reset_password = \
+    ('tgext.registration2', 'templates/email_body_reset_password.txt')
+registration.email_body.lost_password = \
+    ('tgext.registration2', 'templates/email_body_lost_password.txt')
+registration.email_body.changed_email = \
+    ('tgext.registration2', 'templates/email_body_changed_email.txt')

tgext/registration2/registration_config.py

 registration.verified_user = _empty()
 registration.unverified_user = _empty()
 registration.recaptcha = _empty()
+registration.templates = _empty()
+registration.email_body = _empty()
 
 def readfile(filename):
     execfile(filename)

tgext/registration2/widgets.py

                     
     recaptcha_response_field = ReCaptchaWidget(
                     label= _("reCAPTCHA"),
-                    public_key=config.registration_config.recaptcha.public_key,
+                    public_key=config.reg_config.recaptcha.public_key,
                     use_ssl=True,
                     )
                     
     password2 = UnicodeString(not_empty=True, max=40)
     chained_validators = [FieldsMatch('password1', 'password2'),
                           FieldsMatch('email', 'email2'),
-    ReCaptchaValidator(config.registration_config.recaptcha.private_key, 
-                                                  request.remote_addr),
+                          ReCaptchaValidator(config.reg_config.recaptcha.private_key, 
+                                             request.remote_addr),
                          ]
     
 class ExistingUserSchema(TGSchema):