Esempio n. 1
0
def save_edited_info(request):
    """
    Post-processes and saves the information that was entered by the user when
    he edits his profile. Sends change-notifications to the appropriate
    recipients 
    """
    profile = request.user.get_profile()
    restore_profile_from_session(request, profile)

    # Get old values for comparison
    old_lat, old_lon = profile.latitude, profile.longitude
    old_email = profile.email
    old_allow_email = profile.allow_email
    old_timezone = profile.get_timezone()
    old_unique_langs = set(profile.get_unique_languages())
    old_global_langs_needed = set(profile.get_global_langs_needed())
    
    # Randomize the entered coordinates
    longitude = as_float(request.POST.get('longitude', profile.longitude))
    latitude = as_float(request.POST.get('latitude', profile.latitude))
    fudged_latitude, fudged_longitude = randomize_location(latitude, longitude)

    languages = []
    # this could be more than len(old Langs)
    for i in range(settings.NUM_REMOTE_FRIENDS):
        lang_i = request.POST.get("lang_" + str(i), None)
        if lang_i is not None and check_for_language(lang_i):
            languages.append(lang_i)

    profile.set_langs_spoken(languages)
    profile.about_me = request.POST.get("about_me", profile.about_me)
    profile.contact_info = \
        request.POST.get("contact_info", profile.contact_info)
    profile.email = request.POST.get("email", old_email)
    profile.allow_email = request.POST.get("allow_email", False)

    position_changed = \
        (abs(old_lat-latitude) > 1e-8 or abs(old_lon-longitude) > 1e-8)
    if position_changed:
        profile.latitude = fudged_latitude
        profile.longitude = fudged_longitude

    unique_langs_changed = \
        (old_unique_langs != set(profile.get_unique_languages()))
    global_langs_needed_changed = \
        (old_global_langs_needed != set(profile.get_global_langs_needed()))
    timezone_changed = (old_timezone != profile.get_timezone())
    email_changed = (old_email != profile.email)
    allow_email_changed = (old_allow_email != profile.allow_email)

    if settings.VERBOSE:
        print "***", profile.allow_email, allow_email_changed, old_allow_email

    if allow_email_changed:
        if profile.allow_email:
            profile.notify_mailmanager(new_email=profile.email, old_email="")
        else:
            profile.notify_mailmanager(new_email="", old_email=old_email)
            profile.email = ""
    elif profile.allow_email and \
        (unique_langs_changed or timezone_changed or email_changed):
        profile.notify_mailmanager(
            new_email=profile.email, old_email=old_email
        )

    # We don't always need to send a message to the graph if a language token
    # changes. No message needed when there is a global friend still sitting
    # on that token. Thus globalLangsNeededChanged and not simply
    # languagesChanged.
    if position_changed or unique_langs_changed or global_langs_needed_changed:
        profile.send_friend_request()

    profile.save()
    request.session["changes_saved"] = True
Esempio n. 2
0
def new_user(request, should_validate):
    """new user"""
    template_vars = {}
    email = request.POST.get('email',"")
    allow_email = request.POST.get('allow_email', False)

    # This test will only work for users that registered in the pre-encrypted
    # time and have not logged in yet. The other users will have their emails
    # encrypted and we can not test anymore if their email is registered or not.
    if is_inactive_user(email):
        if should_validate:
            failed_field, _, message = \
                validate_registration(request)
            if failed_field:
                template_vars['error_message'] = \
                    __('There was an error with field: ') \
                    + failed_field + ' -- ' + message
                template_path = 'registration/register.html'
                set_register_vars(request, template_vars)
                return (template_path, template_vars, None)

        # VALIDATION PASSED
        # at this point we know we are going to register a user, unless the
        # passphrase miraculously is the same as the one given to somebody
        # else.
        uid = generate_unused_dbkey()
        username = uid
        template_path = 'registration/register_submit.html'

        loginphrase = request.session.get("loginphrase","")
        password = get_password_from_loginphrase (loginphrase)
        if not password:
            raise Http404
        # Do not let people have the same passphrase!
        try:
            User.objects.get(password = password_hash(password))
            # If we get to here, the password already exists.
            # Abort the registration.
            raise Http404
        except ObjectDoesNotExist:
            pass

        overwrite_inactive_user = False
        # True In case the email was registered before, but not activated.
        try:
            test_user = User.objects.get(email=email)
            if not test_user.is_active:
                # The email has been registered before, but has not been
                # activated - thus it can be registered again.
                overwrite_inactive_user = True
                raise ObjectDoesNotExist
        except ObjectDoesNotExist:
            if overwrite_inactive_user:
                user = User.objects.get(email=email)
                old_profile = user.get_profile()
                old_profile.delete()
                user.delete()
                # note that the token used to register that email cannot
                # be used again.

            user = User.objects.create_user(username, uid+"@fo.wl", password) 
            user.is_active = True
            user.save()

            # Need to translate the id to an integer due to django restrictions.
            # For example A A 1234 -> 65 65 1234
            id_int = integer_id(uid)

            # Now let's get our hands dirty with some direct SQL querying...
            cursor = connection.cursor()
            cursor.execute(
                "UPDATE auth_user SET id = %(id)s WHERE username = "******"'%(username)s'" % { "id":id_int, "username":username }
            )
            # Removing the timestamps..
            cursor.execute(
                "UPDATE auth_user SET last_login = '******' WHERE username = "******"'%(username)s'" % { "username":username }
            )
            cursor.execute(
                "UPDATE auth_user SET date_joined = '" \
                + settings.MISC_LAST_LOGIN + "' WHERE username = "******"'%(username)s'" % { "username":username })
            # Data modifying operation - commit required
            transaction.commit_unless_managed()

            # Reload the user object
            user = User.objects.get(username=username)

             # Now make sure that if the profile creation fails, we don't have
             # dead users in our database. try->except
            try:
                lang_0 = request.POST['lang_0']
                lang_1 = request.POST['lang_1']
                lang_2 = request.POST['lang_2']
                lang_3 = request.POST['lang_3']
                lang_4 = request.POST['lang_4']
                lang_5 = request.POST['lang_5']
                languages = [lang_0, lang_1, lang_2, lang_3, lang_4, lang_5]

                longitude = as_float(request.POST['longitude'])
                latitude = as_float(request.POST['latitude'])

                fudged_latitude, fudged_longitude = \
                    randomize_location(latitude, longitude)
                about_me = request.POST['about_me'].strip()
                contact_info = request.POST['contact_info'].strip()
                display_language = get_language()

                user_profile = Profile()
                user_profile.init(
                    user, email, fudged_longitude, fudged_latitude,
                    languages, about_me, contact_info, display_language,
                    allow_email, passphrase=password)

                template_vars['user'] = user
                template_vars['profile'] = user_profile
                template_vars['title'] = __('Thanks for joining')
                template_vars['useremail'] = email
                template_vars['forceLoginLink'] = True
                template_vars['domain'] = settings.DOMAIN
                # no reason to return Profile actually, it's never used.
                return (template_path, template_vars, user_profile)

            except Exception:
                user = User.objects.get(username=username)
                user.delete()
                raise

    else:
        # do not tell user that the email is already registered
        template_path = 'registration/register_submit.html'
        template_vars['title'] = __('Thanks for joining')
        template_vars['useremail'] = email
        template_vars['forceLoginLink'] = True
        template_vars['domain'] = settings.DOMAIN

        return (template_path, template_vars, None)