Example #1
0
    def test_profiles(self):
        """ Test the various api.profiles functions.
        """
        # Create a dummy user with a random username and password.  This should
        # also create a dummy identity within the 3taps identity API.

        username = utils.random_username()
        password = utils.random_password()

        user = users.create(username=username, password=password)

        # Attempt to log in as this user.

        session_token = users.login(username=username,
                                    password=password)

        sessionHandler.validate(session_token)

        # Retrieve the new user's profile, and make sure that it is empty.

        profile = profiles.get(session_token)
        self.assertItemsEqual(profile.keys(), [])

        # Set the user's profile, including uploading a photo.

        photo_file = file(os.path.join(os.path.dirname(__file__),
                                       "data", "photo.jpg"), "rb")

        ignore = profiles.update(session_token,
                                 photo=photo_file,
                                 name="Test User",
                                 email="*****@*****.**")

        # Check that the profile has been updated.

        profile = profiles.get(session_token)
        self.assertItemsEqual(profile.keys(), ["photo_url_48x48",
                                               "photo_url_72x72",
                                               "photo_url_128x128",
                                               "name",
                                               "email"])

        # Finally, log out again.

        users.logout(session_token)
Example #2
0
def home(request):
    """ Respond to the "/web/home" URL.

        We display the home page for a logged in user.  If the current user is
        not logged in, we redirect the user back to the welcome page.
    """
    if not webHelpers.is_logged_in(request):
        return redirect("/")

    # Calculate the environment we're running in.  We use this to load the
    # appropriate version of the widget javascript for the environment.

    if ("127.0.0.1" in request.get_host()) or ("local" in request.get_host()):
        environment = "dev"
    elif "stage" in request.get_host():
        environment = "stage"
    else:
        environment = "prod"

    # Get the name of the widget javascript to use for this environment.

    if environment == "dev":
        widget_script = "widget-dev.js"
    elif environment == "stage":
        widget_script = "widget-stage.js"
    elif environment == "prod":
        widget_script = "widget.js"

    # Get the base URL to use to access this server.

    if request.is_secure():
        base_url = "https://" + request.get_host()
    else:
        base_url = "http://" + request.get_host()

    # Get the details of the current user.

    session = request.COOKIES['mm_session']
    user    = users.get(session)

    # If the user doesn't have a username, something is wrong -- we can't
    # proceed.  In this case, delete the session cookie and redirect the user
    # back to the "welcome" page.

    if "username" not in user:
        response = redirect("/")
        response.delete_cookie("mm_session")
        return response

    # Ask the 3taps Identity API for the user's profile.

    profile = profiles.get(session)

    # Get a list of the user's topics.

    topic_list = []
    for topic in topics.list(session):
        if not topic['active']: continue

        topic_info = {}
        topic_info['id']            = topic['id']
        topic_info['hide_username'] = topic['hide_username']

        if topic.get("name") in [None, ""]:
            # Default topic.
            topic_info['url']        = base_url + "/" + user['username']
            topic_info['hidden_url'] = base_url + "/!" + topic['hidden_url']
            topic_info['default']    = True
        else:
            # A named topic.
            topic_info['url']        = base_url + "/" + user['username'] \
                                     + "/" + topic['name']
            topic_info['hidden_url'] = base_url + "/!" + topic['hidden_url'] \
                                     + "/" + topic['name']
            topic_info['default']    = False

        topic_info['num_views']         = topic['num_views']
        topic_info['num_conversations'] = topic['num_conversations']
        topic_info['num_messages']      = topic['num_messages']
        topic_info['num_via_sms']       = topic['num_via_sms']
        topic_info['embed_code']        = linkGenerator.generate_link_html(
                                                request, topic)

        topic_list.append(topic_info)

    # Get the user's account details.

    phone_number = user['phone_number']

    rate_limit        = rateLimiter.get_phone_number_limit(phone_number)
    period            = settings.SMS_RATE_LIMIT_PERIOD_SIZE
    num_sms_in_period = rateLimiter.get_phone_number_usage(phone_number)

    account = {}
    account['rate_limited']      = True # Hardwired for now.
    account['rate_limit']        = rate_limit
    account['period']            = period.lower()
    account['num_sms_in_period'] = num_sms_in_period

    # Finally, show the home page.

    if "n=t" in request.get_full_path():
        # We're showing the home page for the first time -> display the
        # "welcome" message.
        show_welcome = True
    else:
        show_welcome = False

    return render(request, "home.html",
                  {'user'         : user,
                   'profile'      : profile,
                   'topics'       : topic_list,
                   'account'      : account,
                   'show_welcome' : show_welcome,
                   'base_url'     : base_url})