Commits

Simone Gasbarroni committed 046459d

fix DalIntegrityError, on_complete hooks now give reg params

Comments (0)

Files changed (3)

registration/controllers/root.py

 
         hooks = config['hooks'].get('registration.on_complete', [])
         for func in hooks:
-            func(email_data)
+            try:
+                func(reg, email_data)
+            except TypeError:
+                # Backward compatibility with hooks not accepting the registration
+                func(email_data)
 
-        email_data['body'] = email_data['body'] % vars(reg)
-        email_data['rich'] = email_data['rich'] % vars(reg)
+        email_data['body'] = email_data['body'] % reg.dictified
+        email_data['rich'] = email_data['rich'] % reg.dictified
 
         send_email(reg.email_address, **email_data)
         return dict(email=email, email_data=email_data)
     def activate(self, code, **kw):
         reg = config['registration_dal'].get_inactive(code)
         if not reg:
-            flash(_('Registration not found or already activated'))
+            flash(_('Registration not found or already activated'), 'error')
             return redirect(self.mount_point)
 
         u = app_model.User(user_name=reg.user_name,
         try:
             u = config['registration_dal'].out_of_uow_flush(u)
         except DalIntegrityError:
-            flash(_('Username already activated'))
+            print 'Integrity Error!'
+            flash(_('Username already activated'), 'error')
             return redirect(self.mount_point)
 
+        print 'Flushed', u.__ming__
+
         reg.user = u
         reg.password = '******'
         reg.activated = datetime.now()

registration/model/ming_models.py

 from tgext.pluggable.utils import mount_point
 from registration.model import DBSession
 from registration.model.dal_interface import IRegistration, DalIntegrityError
-from pymongo.errors import OperationFailure
+from pymongo.errors import DuplicateKeyError
 
 
 class Registration(MappedClass):
 
     user_id = FieldProperty(s.String, index=True)
 
+    @property
+    def dictified(self):
+        return dict(time=self.time, user_name=self.user_name, email_address=self.email_address,
+                    code=self.code, activated=self.activated, user_id=self.user_id,
+                    activation_link=self.activation_link)
+
     @cached_property
     def user(self):
         return app_model.User.get(ObjectId(self.user_id))
     def out_of_uow_flush(self, entity):
         try:
             DBSession.flush()
-        except OperationFailure:
+        except DuplicateKeyError:
+            DBSession.clear()
             raise DalIntegrityError
         return entity
 
         return Registration.get_inactive(code)
 
     def pending_activation(self):
-        return Registration.query.find({'activated': None})
+        return Registration.query.find({'activated': None})

registration/model/sqla_models.py

     user_id = Column(Integer, ForeignKey(primary_key(app_model.User)))
     user = relation(app_model.User, uselist=False, backref=backref('registration', uselist=False, cascade='all'))
 
+    @property
+    def dictified(self):
+	return vars(self)
+
     @cached_property
     def activation_link(self):
         return url(mount_point('registration') + '/activate',
         return Registration.get_inactive(code)
 
     def pending_activation(self):
-        return DBSession.query(Registration).filter(Registration.activated==None)
+        return DBSession.query(Registration).filter(Registration.activated==None)