Ejemplo n.º 1
0
def user_register(request):
    """
    a user registers with the system
    """
    DEBUG = False
    form = Form(request, RegistrationSchema)
    #mailer = get_mailer(request)

    # create a random string for email verification procedure
    # http://stackoverflow.com/questions/2257441/
    #   python-random-string-generation-with-upper-case-letters-and-digits
    N = 6
    randomstring = ''.join(random.choice(string.ascii_uppercase
                                         + string.digits) for x in range(N))
    #print " -- the random string: " + randomstring

    URL = "localhost:6543"
    # ToDo XXX change this to be more generic

    if 'form.submitted' in request.POST and not form.validate():
        # form didn't validate
        request.session.flash('form does not validate!')
        if DEBUG:  # pragma: no cover
            print "submitted, but not validated"
    else:  # pragma: NO COVER # just for debugging, RLY
        if DEBUG:
            print "form.submitted was not seen"
        pass

    if 'form.submitted' in request.POST and form.validate():
        # ready for registration!
        #request.session.flash('form validated!')
        username = unicode(form.data['username'])

        message = Message(
            subject="C3S: confirm your email address",
            sender="*****@*****.**",
            recipients=[form.data['email']],
            body="Hello, " + form.data['surname'] + ", \n"
            "Please confirm your email address by clicking this link: \n"
            "http://" + URL + "/user/confirm/" + randomstring + "/"
            + form.data['username'] + " \n"
            "Thanks!")
        msg_accountants = Message(
            subject="[C3S] new member registration",
            sender="*****@*****.**",
            recipients=['*****@*****.**'],
            body="Hello \n"
            "A new member has registered with your site: \n"
            "Username: "******" \n"
            "First name: " + form.data['surname'] + " \n"
            "Last name: " + form.data['lastname'] + " \n"
            "Email: " + form.data['email'] + " \n"
            "Thanks!")

        user = User(
            username=username,
            password=unicode(form.data['password']),
            surname=unicode(form.data['surname']),
            lastname=unicode(form.data['lastname']),
            email=unicode(form.data['email']),
            email_is_confirmed=False,
            email_confirm_code=unicode(randomstring),
            phone=unicode(form.data['phone']),
            fax=unicode(form.data['fax']),
            )
        user.set_address(street=unicode(form.data['street']),
                         number=unicode(form.data['number']),
                         postcode=unicode(form.data['postcode']),
                         city=unicode(form.data['city']),
                         country=unicode(form.data['country']),
                         )

        user_group = Group.get_Users_group()
        user.groups = [user_group]

        # dbsession.add(user)
        dbsession.flush(user)

        #
        # boto stuff: creating a bucket for that user
        # don't do that -- we better have one bucket for all tracks...
        #
        # from boto.exception import S3CreateError, BotoServerError
        # try:
        #     c3sI2Conn.create_named_bucket(username)
        #     request.session.flash(u'created bucket for ' + username)
        # except BotoServerError, e:
        #     print("There was an error: " + str(e) )
        # except S3CreateError, e:
        #     print("There was an error: " + str(e) )
        #
        # send email
        try:
            if DEBUG:  # pragma: no cover
                print("sending email........")
            else:
                pass
            #mailer.send(message)
            #mailer.send(msg_accountants)

            # instead of sending mails, we inform in-browser
            request.session.flash(
                'DEBUG: not sending email. to test email confirmation view, '
                'append this to URL to confirm email: /user/confirm/'
                + randomstring + '/'
                + str(user.username) + '/' + str(form.data['email']))
        except:  # pragma: no cover
            print "could not send email. no mail configured?"

        # remember who this was == sign in user == log her in
        headers = remember(request, username)

        redirect_url = route_url('home', request)

        return HTTPFound(location=redirect_url, headers=headers)

    return {'form': FormRenderer(form), }