Ejemplo n.º 1
0
def profile(request):
    """show profile"""
    if not valid_token(request):
        return token_splash_page(request)

    user_ = request.user
    profile_ = user_.get_profile()
    restore_profile_from_session(request, profile_)

    # show the page in the user's language
    response = display_in_native_language(request, profile_)
    if response:
        return response

    template_path = 'user/profile.html'
    template_vars = {}
    template_vars['title'] = __('My Profile')

    if request.method == "POST":
        if settings.VERBOSE:
            print "POST on the profile. this is a Drop-attempt"
        password = request.POST.get("loginphrase", "")
        dropped_fid = request.POST.get('dropped_fid', None)
        reason = request.POST.get('reason', "")
        comment = request.POST.get('comment', "")
        reason_map = {
            "unresponsive":"nice",
            "differences":"middle",
            "rude":"bad",
        }
        if settings.VERBOSE:
            print "Trying to drop %s for reason %s with comment\n%s" \
                % (dropped_fid, reason, comment)

        if user.check_password(password):
            if dropped_fid is not None and reason in reason_map:
                reason = reason_map[reason]
                dropped = profile_.defriend(dropped_fid, reason, comment)
                if dropped:
                    populate_session(request, password)
                    if dropped_fid in request.session:
                        del request.session[dropped_fid]
                    template_vars['droppedFriend'] = dropped_fid
                    template_vars['message'] = \
                        __('Your contact has been dropped. '
                            'We will find you another one soon.')
                    if settings.VERBOSE:
                        print "Drop successful."

        elif settings.VERBOSE:
            print "Drop failed: Wrong password"

    template_vars['user'] = user_
    template_vars['profile'] = profile_
    template_vars['localFriends'] = \
        profile_.friend_list(request.session, friend_type="local") + \
        request.session.get("display_local", [])
    template_vars['remoteFriends'] = \
        profile_.friend_list(request.session, friend_type="remote") + \
        request.session.get("display_remote", [])
    template_vars['ownUniqueLanguages'] = \
        set(profile_.get_unique_languages())
    template_vars['newFriends'] = request.session.get("new_friends", [])
    template_vars['use_maps_multiple'] = True

    if request.session.get("changes_saved", False):
        template_vars['success_message'] = __("Changes saved.")
        del request.session['changes_saved']

    if settings.VERBOSE:
        print "============SESSION DUMP============"
        for key, value in request.session.items():
            print key, smart_str(value)

    return render_to_response(
        template_path, template_vars,
        context_instance=RequestContext(request)
    )
Ejemplo n.º 2
0
def new_login(request):
    """ 
    Careful - this function does not use django's login/logout functions
    intelligently. Bugs are likely to arise from here.
    """
    if not valid_token(request):
        return token_splash_page(request)

    if match(settings.ID_REGEX, request.user.username):
        # the user already has the new passphrase. Don't let him change it.
        return HttpResponseRedirect(reverse('user.views.profileForwarding'))

    template_path = 'registration/new_login.html'
    template_vars = {}
    template_vars['title'] = _('IMPORTANT')

    # Restore the user's session. If he tries to visit any other page,
    # he will remain logged out.
    if "savedSession" in request.session:
        for key, value in request.session["savedSession"].items(): 
            request.session[key] = value
        if SESSION_KEY in request.session:
            request.user = User.objects.get(id=request.session[SESSION_KEY])
        del request.session["savedSession"]
    user = request.user

    # Don't let people visit this page without logging in.
    # Copied from django.contrib.auth.decorators.user_passes_test
    if not user.is_authenticated():
        login_url = None
        path = request.build_absolute_uri()
        # If the login url is the same scheme and net location then just
        # use the path as the "next" url.
        login_scheme, login_netloc = urlparse.urlparse(login_url or
                                                       settings.LOGIN_URL)[:2]
        current_scheme, current_netloc = urlparse.urlparse(path)[:2]
        if ((not login_scheme or login_scheme == current_scheme) and
            (not login_netloc or login_netloc == current_netloc)):
            path = request.get_full_path()
        return redirect_to_login(path, login_url, REDIRECT_FIELD_NAME)

    user_profile = user.get_profile()
    if request.method == "POST":
        loginphrase_entered = request.POST.get("loginphrase","").upper()
        loginphrase = request.session.get("temploginphrase","")
        if loginphrase_entered == loginphrase:
            # The user received the passphrase and has hopefully written it
            # down. Because now we are changing his credentials.
            user.username = user_profile.id
            password = get_password_from_loginphrase(loginphrase)
            encrypt_unencrypted_profile(user_profile, password)
            user.email = user_profile.get_anonymous_email()
            user.set_password(password)
            user.save()
            populate_session(request, password)
            return HttpResponseRedirect(
                reverse('user.views.profileForwarding')
            )

    loginphrase = user_profile.generateNewLoginPhrase()
    template_vars['loginphrase'] = loginphrase
    request.session['temploginphrase'] = loginphrase

    # Delete the user's session: Stop the user from going to his profile pages
    # before he has entered the passphrase.
    saved_session = {}
    for key, value in request.session._session.items(): 
        saved_session[key] = value
    auth_logout(request)
    request.session["savedSession"] = saved_session
    template_vars['savedSession'] = saved_session

    return render_to_response(template_path, template_vars,
                              context_instance=RequestContext(request))
Ejemplo n.º 3
0
def edit(request, clicked):
    """edit profile"""
    if not valid_token(request):
        return token_splash_page(request)

    template_vars = {}
    if request.method == "POST":
        loginphrase = request.POST.get("password","")
        password = get_password_from_loginphrase(loginphrase)
        if request.user.check_password(password):
            if edit_captcha_ok(request):
                validation_errors =  contactinfo_errors(request) or \
                                     aboutme_errors(request) or \
                                     location_errors(request) or \
                                     language_errors(request) or \
                                     email_errors(request) or None
                if validation_errors:
                    validation_errors = ([contactinfo_errors(request)] + \
                                         [aboutme_errors(request)] + \
                                         [location_errors(request)] + \
                                         [language_errors(request)] + \
                                         [email_errors(request)])
                    for val_error in validation_errors:
                        if val_error is not None:
                            template_vars[val_error[-1]] = val_error[2]
                else:
                    save_edited_info(request)
                    # Get the newly encrypted data into the session again
                    populate_session(request, password)
                    return HttpResponseRedirect(
                        reverse('user.views.profile_forwarding')
                    )
            else:
                template_vars["captchaerror"] = True
                template_vars["captcha"] = CaptchaTestForm(request.POST)
        else:
            template_vars["passworderror"] = __("Wrong passphrase.")
    else:
        if edit_needs_captcha(request):
            template_vars["captcha"] = CaptchaTestForm()
            
    template_vars['contact_info'] = request.POST.get("contact_info", False)
    template_vars['about_me'] = request.POST.get("about_me", False)
    template_vars['email'] = request.POST.get("email", False)
    template_vars['user'] = request.user
    profile_ = request.user.get_profile()
    restore_profile_from_session(request, profile_)
    template_vars['profile'] = profile_

    for num, lang in enumerate(profile_.get_langs_spoken()):
        template_vars['lang_'+str(num)] = \
            request.POST.get("lang_" + str(num), lang)

    template_vars['use_maps_single'] = True
    template_vars['cityDict'] = {
        'latitude' : request.POST.get("latitude", profile_.latitude),
        'longitude' : request.POST.get("longitude", profile_.longitude),
    }
    template_vars["clicked"] = clicked
    template_vars['title'] = __('Edit the information about you')
    template_path = "user/edit_profile.html"

    return render_to_response(template_path, template_vars,
                              context_instance=RequestContext(request))