Exemplo n.º 1
0
def get_user(request, session):
    user = None

    if 'username' in request.session:
        # If logged in, use that user
        user = session.query(Users).filter_by(username=request.session['username']).one()
    elif 'ddtssuser' in request.COOKIES:
        # If persistant cookie is present, get the username from there
        cookie_user = Users.from_cookie(request.COOKIES['ddtssuser'])
        if cookie_user:
            # If we found a persistant cookie, check if it's a real user from the database
            user = session.query(Users).filter_by(username=cookie_user.username).first()
        if user:
            # If found, store in session
            request.session['username'] = cookie_user.username
        else:
            # If not in database, use whatever is in cookie
            user = cookie_user

    if not user:
        # Not found anywhere, make a new user
        user = Users(username=request.META.get('REMOTE_ADDR'))
        user.logged_in = False
        user.countreviews = user.counttranslations = 0

    return user
Exemplo n.º 2
0
def get_user(request, session):
    user = None

    if 'username' in request.session:
        # If logged in, use that user
        user = session.query(Users).filter_by(username = request.session['username']).one()
    elif 'ddtssuser' in request.COOKIES:
        # If persistant cookie is present, get the username from there
        cookie_user = Users.from_cookie(request.COOKIES['ddtssuser'])
        if cookie_user:
            # If we found a persistant cookie, check if it's a real user from the database
            user = session.query(Users).filter_by(username = cookie_user.username).first()
        if user:
            # If found, store in session
            request.session['username'] = cookie_user.username
        else:
            # If not in database, use whatever is in cookie
            user = cookie_user

    if not user:
        # Not found anywhere, make a new user
        user = Users(username=request.META.get('REMOTE_ADDR'))
        user.logged_in = False
        user.countreviews = user.counttranslations = 0

    return user
Exemplo n.º 3
0
    def on_success(request, identity_url, openid_response):
        if not request.session.get('new_user_info'):
            return on_failure(request, "Failure (no new username)")

        # Setup OpenID middleware (do we really need this?)
        django_openid_consumer.views.default_on_success(
            request, identity_url, openid_response)

        user = session.query(Users).filter(
            Users.openid == request.session['openids'][0].openid).first()

        if user:
            messages.error(
                request,
                "OpenID already associated with another user, not creating. Logging in instead."
            )
            request.session['username'] = user.username
            return redirect('ddtss_index')

        # Create user and add to database
        user = Users(username=request.session['new_user_info'][0],
                     email='*',
                     realname=request.session['new_user_info'][1],
                     active=True,
                     key=generate_random_string(16),
                     lastseen=int(time.time()))

        user.md5password = "******"
        user.openid = request.session['openids'][0].openid

        session.add(user)
        session.commit()

        # Login user in
        request.session['username'] = user.username

        logging.info("Account successfully created" \
                     " - username[%s]" \
                     " openid[%s]",
                     user.username, user.openid)
        if request.session.test_cookie_worked():
            request.session.delete_test_cookie()

        messages.success(request, "Account successfully created.")
        return redirect('ddtss_index')
Exemplo n.º 4
0
    def on_success(request, identity_url, openid_response):
        if not request.session.get('new_user_info'):
            return on_failure(request, "Failure (no new username)")

        # Setup OpenID middleware (do we really need this?)
        django_openid_consumer.views.default_on_success(request, identity_url, openid_response)

        user = session.query(Users).filter(Users.openid == request.session['openids'][0].openid).first()

        if user:
            messages.error(request, "OpenID already associated with another user, not creating. Logging in instead.")
            request.session['username'] = user.username
            return redirect('ddtss_index')

        # Create user and add to database
        user = Users(username=request.session['new_user_info'][0],
                     email='*',
                     realname=request.session['new_user_info'][1],
                     active=True,
                     key=generate_random_string(16),
                     lastseen=int(time.time()))

        user.md5password = "******"
        user.openid = request.session['openids'][0].openid

        session.add(user)
        session.commit()

        # Login user in
        request.session['username'] = user.username

        logging.info("Account successfully created" \
                     " - username[%s]" \
                     " openid[%s]",
                     user.username, user.openid)
        if request.session.test_cookie_worked():
            request.session.delete_test_cookie()

        messages.success(request, "Account successfully created.")
        return redirect('ddtss_index')
Exemplo n.º 5
0
def view_create_user(session, request):
    """ Handle the user creation """
    if request.method == "POST":
        form = UserCreationForm(session=session, data=request.POST)
        if form.is_valid():
            # Create user and add to database
            user = Users(username=form.cleaned_data['username'],
                         email=form.cleaned_data['email'],
                         realname=form.cleaned_data['realname'],
                         active=False,
                         key=generate_random_string(16),
                         lastseen=int(time.time()))

            user.md5password = hashlib.md5(user.key + form.cleaned_data['password1']).hexdigest()

            session.add(user)
            session.commit()

            # Login user in
            request.session['username'] = form.cleaned_data['username']

            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()

            messages.success(request, "Account successfully created.")
            return redirect('ddtss_index')
    else:
        form = UserCreationForm(session)

    request.session.set_test_cookie()

    context = {
        'form': form,
    }
    return render_to_response("ddtss/create_user.html", context,
                              context_instance=RequestContext(request))
Exemplo n.º 6
0
def view_create_user(session, request):
    """ Handle the user creation """
    if request.method == "POST":
        form = UserCreationForm(session=session, data=request.POST)
        if form.is_valid():
            if form.cleaned_data['openid_url']:
                # What we do here is add the username to the session and then
                # run the code as if they did a login request.  Then we run the
                # code as if they did a login.  If the login succeeds we can
                # create the user account.
                request.session['new_user_info'] = (
                    form.cleaned_data['username'],
                    form.cleaned_data['realname'])

                def on_failure(request, message):
                    return render_to_response(
                        "ddtss/create_user.html", {'message': message},
                        context_instance=RequestContext(request))

                return django_openid_consumer.views.begin(
                    request,
                    on_failure=on_failure,
                    redirect_to=reverse('ddtss_create_user_complete'))
            else:
                user = session.query(Users).filter_by(
                    username=form.cleaned_data['username']).first()

                # User may exist but must not be active (forms checks for
                # that).  Allow reuse of users not active, in case email
                # gets lost.
                if not user:
                    user = Users()

                # Create user and add to database
                user.username = form.cleaned_data['username']
                user.email = form.cleaned_data['email']
                user.realname = form.cleaned_data['realname']
                user.active = False
                user.key = generate_random_string(16)
                user.lastseen = int(time.time())
                user.md5password = hashlib.md5(
                    user.key + form.cleaned_data['password1']).hexdigest()

                # User logging in with email address and password
                email = EmailMessage(
                    subject="Verify new DDTSS account",
                    from_email="*****@*****.**",
                    to=[form.cleaned_data['email']],
                    bcc=["*****@*****.**"],
                )
                confirm_url = request.build_absolute_uri(
                    reverse("ddtss_create_user_verifyemail",
                            args=[form.cleaned_data['username']]))
                email.body = """
    To confirm your account (%s) on the DDTSS, please follow this link
    %s?key=%s

    If you did not create an account, please ignore this email.
    Django-DDTSS (Debian Distributed Translation Server Satelite)
                """ % (form.cleaned_data['username'], confirm_url,
                       hashlib.md5(user.key).hexdigest())

                email.send()

                session.add(user)
                session.commit()

                messages.success(
                    request,
                    "Verification email sent. Press on contained link to activate account."
                )
                return redirect("ddtss_index")
    else:
        form = UserCreationForm(session)

    request.session.set_test_cookie()

    context = {
        'form': form,
    }
    return render_to_response("ddtss/create_user.html",
                              context,
                              context_instance=RequestContext(request))
Exemplo n.º 7
0
def view_create_user(session, request):
    """ Handle the user creation """
    if request.method == "POST":
        form = UserCreationForm(session=session, data=request.POST)
        if form.is_valid():
            if form.cleaned_data['openid_url']:
                # What we do here is add the username to the session and then
                # run the code as if they did a login request.  Then we run the
                # code as if they did a login.  If the login succeeds we can
                # create the user account.
                request.session['new_user_info'] = (form.cleaned_data['username'], form.cleaned_data['realname'])

                def on_failure(request, message):
                    return render_to_response("ddtss/create_user.html", {'message': message},
                                             context_instance=RequestContext(request))

                return django_openid_consumer.views.begin(request,
                                                          on_failure=on_failure,
                                                          redirect_to=reverse('ddtss_create_user_complete'))
            else:
                user = session.query(Users).filter_by(username=form.cleaned_data['username']).first()

                # User may exist but must not be active (forms checks for
                # that).  Allow reuse of users not active, in case email
                # gets lost.
                if not user:
                    user = Users()

                # Create user and add to database
                user.username=form.cleaned_data['username']
                user.email=form.cleaned_data['email']
                user.realname=form.cleaned_data['realname']
                user.active=False
                user.key=generate_random_string(16)
                user.lastseen=int(time.time())
                user.md5password = hashlib.md5(user.key + form.cleaned_data['password1']).hexdigest()

                # User logging in with email address and password
                email = EmailMessage(subject="Verify new DDTSS account",
                                     from_email="*****@*****.**",
                                     to=[form.cleaned_data['email']],
                                     bcc=["*****@*****.**"],
                                    )
                confirm_url = request.build_absolute_uri( reverse("ddtss_create_user_verifyemail", args=[form.cleaned_data['username']]) )
                email.body = """
    To confirm your account (%s) on the DDTSS, please follow this link
    %s?key=%s

    If you did not create an account, please ignore this email.
    Django-DDTSS (Debian Distributed Translation Server Satelite)
                """ % (form.cleaned_data['username'], confirm_url, hashlib.md5(user.key).hexdigest())

                email.send()

                session.add(user)
                session.commit()

                messages.success(request, "Verification email sent. Press on contained link to activate account.")
                return redirect("ddtss_index")
    else:
        form = UserCreationForm(session)

    request.session.set_test_cookie()

    context = {
        'form': form,
    }
    return render_to_response("ddtss/create_user.html", context,
                              context_instance=RequestContext(request))