Beispiel #1
0
    def test_get_user_activation(self):
        from pyramid_signup.models import Activation
        from pyramid_signup.managers import ActivationManager
        from pyramid_signup.managers import UserManager
        from pyramid_signup.models import User

        user1 = User(username='******',
                     password='******',
                     email='*****@*****.**')
        user2 = User(username='******',
                     password='******',
                     email='*****@*****.**')

        activation = Activation()
        user2.activation = activation

        self.session.add(user1)
        self.session.add(user2)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = ActivationManager(request)
        user_mgr = UserManager(request)

        new_user = user_mgr.get_by_username('sontek2')

        new_activation = mgr.get_by_code(activation.code)

        assert activation == new_activation
        assert new_user.activation == new_activation
Beispiel #2
0
    def test_get_user_by_activation_with_multiple_users(self):
        from pyramid_signup.models import User
        from pyramid_signup.models import Activation
        from pyramid_signup.managers import UserManager

        user1 = User(username='******',
                     password='******',
                     email='*****@*****.**')
        user2 = User(username='******',
                     password='******',
                     email='*****@*****.**')

        activation = Activation()
        user2.activation = activation

        self.session.add(user1)
        self.session.add(user2)

        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        new_user = mgr.get_by_activation(activation)

        assert new_user == user2
Beispiel #3
0
    def login(self):
        if self.request.method == 'GET':
            if self.request.user:
                return HTTPFound(location=self.login_redirect_view)

            return {'form': self.form.render()}
        elif self.request.method == 'POST':
            try:
                controls = self.request.POST.items()
                captured = self.form.validate(controls)
            except deform.ValidationFailure, e:
                return {'form': e.render(), 'errors': e.error.children}

            username = captured['Username']
            password = captured['Password']

            mgr = UserManager(self.request)

            user = mgr.get_user(username, password)

            if user:
                if not user.activated:
                    self.request.session.flash(
                        _(u'Your account is not active, please check your e-mail.'
                          ), 'error')
                    return {'form': self.form.render()}
                else:
                    return self.authenticated(self.request, user.pk)

            self.request.session.flash(_('Invalid username or password.'),
                                       'error')

            return {'form': self.form.render(appstruct=captured)}
Beispiel #4
0
    def test_register_creates_user(self):
        from pyramid_signup.views import RegisterController
        from pyramid_mailer.mailer import DummyMailer
        from pyramid_mailer.interfaces import IMailer
        from pyramid_signup.managers import UserManager

        self.config.include('pyramid_signup')
        self.config.registry.registerUtility(DummyMailer(), IMailer)

        self.config.add_route('index', '/')

        request = self.get_csrf_request(post={
            'Username': '******',
            'Password': {
                'value': 'test123',
                'confirm': 'test123',
            },
            'Email': '*****@*****.**'
        },
                                        request_method='POST')

        request.user = Mock()
        controller = RegisterController(request)
        response = controller.post()

        assert response.status_int == 302

        mgr = UserManager(request)
        user = mgr.get_by_username('admin')

        assert user != None
Beispiel #5
0
    def test_activate_invalid(self):
        from pyramid_signup.views import RegisterController
        from pyramid_signup.models import User
        from pyramid_signup.models import Activation
        from pyramid_mailer.interfaces import IMailer
        from pyramid_mailer.mailer import DummyMailer
        from pyramid_signup.managers import UserManager
        self.config.include('pyramid_signup')
        self.config.add_route('index', '/')

        self.config.registry.registerUtility(DummyMailer(), IMailer)

        user = User(username='******', password='******')
        user.activation = Activation()

        self.session.add(user)
        self.session.flush()

        request = testing.DummyRequest()
        request.matchdict = Mock()
        get = Mock()
        get.return_value = 'invalid'
        request.matchdict.get = get

        controller = RegisterController(request)
        response = controller.activate()
        mgr = UserManager(request)
        user = mgr.get_by_username('sontek')

        assert not user.activated
        assert response.status_int == 404
Beispiel #6
0
    def test_activate_invalid(self):
        from pyramid_signup.views import RegisterController
        from pyramid_signup.models import User
        from pyramid_signup.models import Activation
        from pyramid_mailer.interfaces import IMailer
        from pyramid_mailer.mailer import DummyMailer
        from pyramid_signup.managers import UserManager
        self.config.include('pyramid_signup')
        self.config.add_route('index', '/')

        self.config.registry.registerUtility(DummyMailer(), IMailer)

        user = User(username='******', password='******')
        user.activation = Activation()

        self.session.add(user)
        self.session.flush()

        request = testing.DummyRequest()
        request.matchdict = Mock()
        get = Mock()
        get.return_value = 'invalid'
        request.matchdict.get = get

        controller = RegisterController(request)
        response = controller.activate()
        mgr = UserManager(request)
        user = mgr.get_by_username('sontek')

        assert not user.activated
        assert response.status_int == 404
Beispiel #7
0
    def test_register_creates_user(self):
        from pyramid_signup.views import RegisterController
        from pyramid_mailer.mailer import DummyMailer
        from pyramid_mailer.interfaces import IMailer
        from pyramid_signup.managers import UserManager

        self.config.include('pyramid_signup')
        self.config.registry.registerUtility(DummyMailer(), IMailer)

        self.config.add_route('index', '/')

        request = self.get_csrf_request(post={
            'Username': '******',
            'Password': {
                'value': 'test123',
                'confirm': 'test123',
            },
            'Email': '*****@*****.**'
        }, request_method='POST')

        request.user = Mock()
        controller = RegisterController(request)
        response = controller.register()

        assert response.status_int == 302

        mgr = UserManager(request)
        user = mgr.get_by_username('admin')

        assert user != None
Beispiel #8
0
    def login(self):
        if self.request.method == 'GET':
            if self.request.user:
                return HTTPFound(location=self.login_redirect_view)

            return {'form': self.form.render()}
        elif self.request.method == 'POST':
            try:
                controls = self.request.POST.items()
                captured = self.form.validate(controls)
            except deform.ValidationFailure, e:
                return {'form': e.render(), 'errors': e.error.children}

            username = captured['Username']
            password = captured['Password']

            mgr = UserManager(self.request)

            user = mgr.get_user(username, password)

            if user:
                if not user.activated:
                    self.request.session.flash(_(u'Your account is not active, please check your e-mail.'), 'error')
                    return {'form': self.form.render()}
                else:
                    return self.authenticated(self.request, user.pk)

            self.request.session.flash(_('Invalid username or password.'), 'error')

            return {'form': self.form.render(appstruct=captured)}
Beispiel #9
0
    def activate(self):
        code = self.request.matchdict.get('code', None)
        user_pk = self.request.matchdict.get('user_pk', None)
        act_mgr = ActivationManager(self.request)
        user_mgr = UserManager(self.request)

        activation = act_mgr.get_by_code(code)

        if activation:
            user = user_mgr.get_by_pk(user_pk)

            if user.activation != activation:
                return HTTPNotFound()

            if user:
                self.db.delete(activation)
                user.activated = True
                self.db.add(user)
                self.db.flush()

                self.request.registry.notify(
                    RegistrationActivatedEvent(self.request, user, activation)
                )

                self.request.session.flash(_('Your e-mail address has been verified.'), 'success')
                return HTTPFound(location=self.activate_redirect_view)

        return HTTPNotFound()
    def test_get_user_activation(self):
        from pyramid_signup.models import Activation
        from pyramid_signup.managers import ActivationManager
        from pyramid_signup.managers import UserManager
        from pyramid_signup.models import User

        user1 = User(username="******", password="******", email="*****@*****.**")
        user2 = User(username="******", password="******", email="*****@*****.**")

        activation = Activation()
        user2.activation = activation

        self.session.add(user1)
        self.session.add(user2)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = ActivationManager(request)
        user_mgr = UserManager(request)

        new_user = user_mgr.get_by_username("sontek2")

        new_activation = mgr.get_by_code(activation.code)

        assert activation == new_activation
        assert new_user.activation == new_activation
Beispiel #11
0
    def post(self):
        if self.request.method == 'POST':
            try:
                controls = self.request.POST.items()
                captured = self.form.validate(controls)
            except deform.ValidationFailure, e:
                return {'form': e.render(), 'errors': e.error.children}

            email = captured['Email']
            username = captured['Username']
            password = captured['Password']


            mgr = UserManager(self.request)
            user = mgr.get_by_username(username)

            if user:
                self.request.session.flash(_('That username is already used.'), 'error')
                return {'form': self.form.render(self.request.POST)}

            activation = None

            try:
                user = User(username=username, password=password, email=email)

                if self.require_activation:
                    activation = Activation()
                    self.db.add(activation)

                    user.activation = activation

                    body = pystache.render(_("Please activate your e-mail address by visiting {{ link }}"),
                        {
                            'link': route_url('activate', self.request, code=user.activation.code)
                        }
                    )

                    subject = _("Please active your e-mail address!")

                    message = Message(subject=subject, recipients=[user.email], body=body)
                    self.mailer.send(message)

                    self.request.session.flash(_('Please check your E-mail for an activation link'), 'success')
                else:
                    user.activated = True

                self.db.add(user)
                self.db.flush()
            except Exception as exc:
                self.request.session.flash(exc.message, 'error')
                return {'form': self.form.render()}

            self.request.registry.notify(
                NewRegistrationEvent(self.request, user, activation, controls)
            )

            return HTTPFound(location=self.register_redirect_view)
Beispiel #12
0
    def test_profile_update_password(self):
        from pyramid_signup.views import ProfileController
        from pyramid_signup.managers import UserManager
        from pyramid_signup.interfaces import ISUSession
        from pyramid_signup.events import ProfileUpdatedEvent
        from pyramid_signup.models import crypt

        self.config.add_route('index', '/')
        self.config.include('pyramid_signup')

        from pyramid_signup.models import User

        user = User(username='******', password='******', email='*****@*****.**',
            activated=True)

        self.session.add(user)
        self.session.flush()

        def handle_profile_updated(event):
            request = event.request
            session = request.registry.getUtility(ISUSession)
            session.commit()

        self.config.add_subscriber(handle_profile_updated, ProfileUpdatedEvent)


        request = self.get_csrf_request(post={
            'First_Name': 'John',
            'Last_Name': 'Anderson',
            'Email': '*****@*****.**',
            'Password': {
                'value': 'test123',
                'confirm': 'test123',
            },
        }, request_method='POST')

        request.user = user

        request.matchdict = Mock()
        get = Mock()
        get.return_value = user.pk
        request.matchdict.get = get


        flash = Mock()
        request.session.flash = flash

        view = ProfileController(request)

        view.profile()
        mgr = UserManager(request)
        new_user = mgr.get_by_pk(user.pk)

        assert new_user.first_name == 'John'
        assert new_user.last_name == 'Anderson'
        assert new_user.email == '*****@*****.**'
        assert not crypt.check(user.password, 'temp' + user.salt)
Beispiel #13
0
    def __getitem__(self, key):
        mgr = UserManager(self.request)

        user = mgr.get_by_pk(key)

        if user:
            user.__parent__ = self
            user.__name__ = key

        return user
Beispiel #14
0
    def profile(self):
        pk = self.request.matchdict.get('user_pk', None)

        mgr = UserManager(self.request)

        user = mgr.get_by_pk(pk)

        if not user:
            return HTTPNotFound()

        return {'user': user}
Beispiel #15
0
    def edit_profile(self):
        pk = self.request.matchdict.get('user_pk', None)

        mgr = UserManager(self.request)

        user = mgr.get_by_pk(pk)

        if not user:
            return HTTPNotFound()

        if self.request.method == 'GET':
            username = user.username
            first_name = user.first_name
            last_name = user.last_name
            email = user.email

            return {
                    'form': self.form.render(
                        appstruct= dict(
                            Username=username,
                            First_Name=first_name if first_name else '',
                            Last_Name=last_name if last_name else '',
                            Email=email if email else '',
                        )
                    )
                }
        elif self.request.method == 'POST':
            try:
                controls = self.request.POST.items()
                captured = self.form.validate(controls)
            except deform.ValidationFailure, e:
                # We pre-populate username
                e.cstruct['Username'] = user.username
                return {'form': e.render(), 'errors': e.error.children}

            user.first_name = captured.get('First_Name', '')
            user.last_name = captured.get('Last_Name', '')
            user.email = captured.get('Email', '')

            password = captured.get('Password')

            if password:
                user.password = password

            self.request.session.flash(_('Profile successfully updated.'), 'success')

            self.db.add(user)

            self.request.registry.notify(
                ProfileUpdatedEvent(self.request, user, captured)
            )

            return HTTPFound(location=self.request.url)
Beispiel #16
0
def presenter_widget(node, kw):
    request = kw.get('request')

    choices = [
        ('', '- None -'),
    ]

    mgr = UserManager(request)

    for user in mgr.get_all():
        choices.append((str(user.pk), user.display_name))

    return deform.widget.SelectWidget(values=choices)
Beispiel #17
0
    def test_get_user_by_username(self):
        from pyramid_signup.models import User
        from pyramid_signup.managers import UserManager

        user = User(username='******', password='******')
        self.session.add(user)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        new_user = mgr.get_by_username('sontek')

        assert new_user == user
Beispiel #18
0
    def test_get_user_by_invalid_username(self):
        from pyramid_signup.models import User
        from pyramid_signup.managers import UserManager

        user = User(username='******', password='******')
        self.session.add(user)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        new_user = mgr.get_by_username('sontek1')

        assert new_user == None
    def test_get_user_by_email(self):
        from pyramid_signup.models import User
        from pyramid_signup.managers import UserManager

        user = User(username="******", password="******", email="*****@*****.**")
        self.session.add(user)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        new_user = mgr.get_by_email(user.email)

        assert new_user == user
    def test_get_user_by_invalid_username(self):
        from pyramid_signup.models import User
        from pyramid_signup.managers import UserManager

        user = User(username="******", password="******")
        self.session.add(user)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        new_user = mgr.get_by_username("sontek1")

        assert new_user == None
Beispiel #21
0
    def test_get_all_users(self):
        from pyramid_signup.models import User
        from pyramid_signup.managers import UserManager

        user = User(username='******', password='******')
        user2 = User(username='******', password='******')
        self.session.add(user)
        self.session.add(user2)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        users = mgr.get_all()

        assert len(users) == 2
Beispiel #22
0
    def test_get_user_by_email(self):
        from pyramid_signup.models import User
        from pyramid_signup.managers import UserManager

        user = User(username='******',
                    password='******',
                    email='*****@*****.**')
        self.session.add(user)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        new_user = mgr.get_by_email(user.email)

        assert new_user == user
Beispiel #23
0
    def test_register_no_email_validation(self):
        from pyramid_signup.views import RegisterController
        from pyramid_mailer.mailer import DummyMailer
        from pyramid_mailer.interfaces import IMailer
        from pyramid_signup.managers import UserManager
        from pyramid_signup.interfaces import ISUSession
        from pyramid_signup.events import NewRegistrationEvent

        self.config.include('pyramid_signup')
        self.config.registry.registerUtility(DummyMailer(), IMailer)

        self.config.add_route('index', '/')
        self.config.registry.settings['su.require_activation'] = False

        def handle_registration(event):
            request = event.request
            session = request.registry.getUtility(ISUSession)
            session.commit()

        self.config.add_subscriber(handle_registration, NewRegistrationEvent)


        request = self.get_csrf_request(post={
            'Username': '******',
            'Password': {
                'value': 'test123',
                'confirm': 'test123',
            },
            'Email': '*****@*****.**'
        }, request_method='POST')

        flash = Mock()
        request.session.flash = flash

        request.user = Mock()

        controller = RegisterController(request)
        response = controller.register()

        assert response.status_int == 302

        mgr = UserManager(request)
        user = mgr.get_by_username('admin')

        assert user.activated == True
        flash.assert_called_with('You have been registered, you may login now!', 'success')
Beispiel #24
0
    def reset_password(self):
        schema = self.request.registry.getUtility(ISUResetPasswordSchema)
        schema = schema().bind(request=self.request)

        form = self.request.registry.getUtility(ISUResetPasswordForm)
        form = form(schema)

        code = self.request.matchdict.get('code', None)
        act_mgr = ActivationManager(self.request)
        user_mgr = UserManager(self.request)

        activation = act_mgr.get_by_code(code)

        if activation:
            user = user_mgr.get_by_activation(activation)

            if user:
                if self.request.method == 'GET':
                        return {
                            'form': form.render(
                                appstruct=dict(
                                    Username=user.username
                                )
                            )
                        }

                elif self.request.method == 'POST':
                    try:
                        controls = self.request.POST.items()
                        captured = form.validate(controls)
                    except deform.ValidationFailure, e:
                        return {'form': e.render(), 'errors': e.error.children}

                    password = captured['Password']

                    user.password = password
                    self.db.add(user)
                    self.db.delete(activation)

                    self.request.registry.notify(
                        PasswordResetEvent(self.request, user, password)
                    )

                    self.request.session.flash(_('Your password has been reset!'), 'success')

                    return HTTPFound(location=self.reset_password_redirect_view)
Beispiel #25
0
    def test_get_user_by_activation(self):
        from pyramid_signup.models import User
        from pyramid_signup.models import Activation
        from pyramid_signup.managers import UserManager

        user = User(username='******', password='******', email='*****@*****.**')
        activation = Activation()
        user.activation = activation

        self.session.add(user)
        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        new_user = mgr.get_by_activation(activation)

        assert new_user == user
Beispiel #26
0
    def test_activate_invalid_user(self):
        from pyramid_signup.views import RegisterController
        from pyramid_signup.models import User
        from pyramid_signup.models import Activation
        from pyramid_mailer.interfaces import IMailer
        from pyramid_mailer.mailer import DummyMailer
        from pyramid_signup.managers import UserManager
        self.config.include('pyramid_signup')
        self.config.add_route('index', '/')

        self.config.registry.registerUtility(DummyMailer(), IMailer)

        bad_act = Activation()

        user = User(username='******', password='******')
        user.activation = Activation()

        user2 = User(username='******', password='******')
        user2.activation = bad_act

        self.session.add(user)
        self.session.add(user2)
        self.session.flush()

        request = testing.DummyRequest()
        request.matchdict = Mock()

        def get(val, ret):
            if val == 'code':
                return bad_act.code
            elif val == 'user_pk':
                return user.pk

        request.matchdict.get = get

        controller = RegisterController(request)
        response = controller.activate()
        mgr = UserManager(request)
        new_user1 = mgr.get_by_username('sontek')
        new_user2 = mgr.get_by_username('jessie')

        assert not new_user1.activated
        assert not new_user2.activated
        assert response.status_int == 404
Beispiel #27
0
    def test_activate_multiple_users(self):
        from pyramid_signup.views import RegisterController
        from pyramid_signup.models import User
        from pyramid_signup.models import Activation
        from pyramid_mailer.interfaces import IMailer
        from pyramid_mailer.mailer import DummyMailer
        from pyramid_signup.managers import UserManager
        from pyramid_signup.managers import ActivationManager
        self.config.include('pyramid_signup')
        self.config.add_route('index', '/')

        self.config.registry.registerUtility(DummyMailer(), IMailer)

        user = User(username='******', password='******')
        user.activation = Activation()
        user1 = User(username='******', password='******')
        user1.activation = Activation()

        self.session.add(user)
        self.session.add(user1)
        self.session.flush()

        request = testing.DummyRequest()
        request.matchdict = Mock()

        def get(key, default):
            if key == 'code':
                return user1.activation.code
            else:
                return user1.pk

        request.matchdict.get = get

        controller = RegisterController(request)
        response = controller.activate()
        mgr = UserManager(request)
        user = mgr.get_by_username('sontek1')

        act_mgr = ActivationManager(request)
        activations = act_mgr.get_all()

        assert len(activations) == 1
        assert user.activated
        assert response.status_int == 302
Beispiel #28
0
    def test_register_no_email_validation(self):
        from pyramid_signup.views import RegisterController
        from pyramid_mailer.mailer import DummyMailer
        from pyramid_mailer.interfaces import IMailer
        from pyramid_signup.managers import UserManager
        from pyramid_signup.interfaces import ISUSession
        from pyramid_signup.events import NewRegistrationEvent

        self.config.include('pyramid_signup')
        self.config.registry.registerUtility(DummyMailer(), IMailer)

        self.config.add_route('index', '/')
        self.config.registry.settings['su.require_activation'] = False

        def handle_registration(event):
            request = event.request
            session = request.registry.getUtility(ISUSession)
            session.commit()

        self.config.add_subscriber(handle_registration, NewRegistrationEvent)

        request = self.get_csrf_request(post={
            'Username': '******',
            'Password': {
                'value': 'test123',
                'confirm': 'test123',
            },
            'Email': '*****@*****.**'
        },
                                        request_method='POST')

        request.user = Mock()
        controller = RegisterController(request)
        response = controller.post()

        assert response.status_int == 302

        mgr = UserManager(request)
        user = mgr.get_by_username('admin')

        assert user.activated == True
Beispiel #29
0
    def forgot_password(self):
        schema = self.request.registry.getUtility(ISUForgotPasswordSchema)
        schema = schema().bind(request=self.request)

        form = self.request.registry.getUtility(ISUForgotPasswordForm)
        form = form(schema)

        if self.request.method == 'GET':
            if self.request.user:
                return HTTPFound(location=self.forgot_password_redirect_view)

            return {'form': form.render()}

        elif self.request.method == 'POST':
            try:
                controls = self.request.POST.items()
                captured = form.validate(controls)
            except deform.ValidationFailure, e:
                return {'form': e.render(), 'errors': e.error.children}

            email = captured['Email']

            mgr = UserManager(self.request)
            user = mgr.get_by_email(email)
            activation = Activation()
            self.db.add(activation)

            user.activation = activation

            if user:
                mailer = get_mailer(self.request)
                body = pystache.render(_("Someone has tried to reset your password, if this was you click here: {{ link }}"),
                    {
                        'link': route_url('reset_password', self.request, code=user.activation.code)
                    }
                )

                subject = _("Do you want to reset your password?")

                message = Message(subject=subject, recipients=[user.email], body=body)
                mailer.send(message)
    def test_get_user_by_activation_with_multiple_users(self):
        from pyramid_signup.models import User
        from pyramid_signup.models import Activation
        from pyramid_signup.managers import UserManager

        user1 = User(username="******", password="******", email="*****@*****.**")
        user2 = User(username="******", password="******", email="*****@*****.**")

        activation = Activation()
        user2.activation = activation

        self.session.add(user1)
        self.session.add(user2)

        self.session.commit()

        request = testing.DummyRequest()
        mgr = UserManager(request)

        new_user = mgr.get_by_activation(activation)

        assert new_user == user2
Beispiel #31
0
    def activate(self):
        code = self.request.matchdict.get('code', None)
        act_mgr = ActivationManager(self.request)
        user_mgr = UserManager(self.request)

        activation = act_mgr.get_by_code(code)

        if activation:
            user = user_mgr.get_by_activation(activation)

            if user:
                self.db.delete(activation)
                user.activated = True
                self.db.add(user)
                self.db.flush()

                self.request.registry.notify(
                    RegistrationActivatedEvent(self.request, user, activation))

                self.request.session.flash(
                    _('Your e-mail address has been verified.'), 'success')
                return HTTPFound(location=self.activate_redirect_view)

        return HTTPNotFound()
Beispiel #32
0
def admin(request):

    mgr = UserManager(request)
    return {'users': mgr.get_all()}
Beispiel #33
0
    def user(self):
        pk = unauthenticated_userid(self)

        if pk is not None:
            mgr = UserManager(self)
            return mgr.get_by_pk(pk)
Beispiel #34
0
    def user(self):
        pk = unauthenticated_userid(self)

        if pk is not None:
            mgr = UserManager(self)
            return mgr.get_by_pk(pk)
Beispiel #35
0
    def post(self):
        if self.request.method == 'POST':
            try:
                controls = self.request.POST.items()
                captured = self.form.validate(controls)
            except deform.ValidationFailure, e:
                return {'form': e.render(), 'errors': e.error.children}

            email = captured['Email']
            username = captured['Username']
            password = captured['Password']

            mgr = UserManager(self.request)
            user = mgr.get_by_username(username)

            if user:
                self.request.session.flash(_('That username is already used.'),
                                           'error')
                return {'form': self.form.render(self.request.POST)}

            activation = None

            try:
                user = User(username=username, password=password, email=email)

                if self.require_activation:
                    activation = Activation()
                    self.db.add(activation)

                    user.activation = activation

                    body = pystache.render(
                        _("Please activate your e-mail address by visiting {{ link }}"
                          ), {
                              'link':
                              route_url('activate',
                                        self.request,
                                        code=user.activation.code)
                          })

                    subject = _("Please active your e-mail address!")

                    message = Message(subject=subject,
                                      recipients=[user.email],
                                      body=body)
                    self.mailer.send(message)

                    self.request.session.flash(
                        _('Please check your E-mail for an activation link'),
                        'success')
                else:
                    user.activated = True

                self.db.add(user)
                self.db.flush()
            except Exception as exc:
                self.request.session.flash(exc.message, 'error')
                return {'form': self.form.render()}

            self.request.registry.notify(
                NewRegistrationEvent(self.request, user, activation, controls))

            return HTTPFound(location=self.register_redirect_view)