def create_profile(self, user): """ Create a ``RegistrationProfile`` for a given ``User``, and return the ``RegistrationProfile``. The activation key for the ``RegistrationProfile`` will be a SHA1 hash, generated from a combination of the ``User``'s username and a random salt. """ salt = sha.new(str(random.random())).hexdigest()[:5] activation_key = sha.new(salt + user.username).hexdigest() # prepend "key_" to the key_name, because key_names can't start with numbers registrationprofile = RegistrationProfile(user=user, activation_key=activation_key) db = DB_Session() db.add(registrationprofile) db.flush() db.refresh(registrationprofile) db.commit() db.close() return registrationprofile
def create_inactive_user(cls, username, password, email, domain_override="", send_email=True): new_user = User(username=username, email=email, is_active=False) new_user.set_password(password) db = DB_Session() db.add(new_user) db.flush() db.refresh(new_user) db.commit() db.close() registration_profile = cls.create_profile(new_user) if send_email: current_site = domain_override # current_site = Site.objects.get_current() subject = web.template.render("templates").activation_email_subject( {"site": current_site, "activation_key": registration_profile.activation_key} ) # render_to_string('registration/activation_email_subject.txt', # { 'site': current_site,'activation_key': registration_profile.activation_key }) # Email subject *must not* contain newlines subject = "".join(subject.__str__().splitlines()) message = web.template.render("templates").activation_email( { "activation_key": registration_profile.activation_key, "expiration_days": settings.ACCOUNT_ACTIVATION_DAYS, "site": current_site, } ) # render_to_string('registration/activation_email.txt', # { 'activation_key': registration_profile.activation_key, # 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS, # 'site': current_site }) web.sendmail(settings.DEFAULT_FROM_EMAIL, new_user.email, subject, message) return new_user