Пример #1
0
def post_account():
    # Flask apparently throws 400 errors if POST form data isn't present.
    # Full name
    user_id = session['user_id']
    location = settings.get_user(user_id)['feed_location']
    user = User(local_url=location)

    if request.form.get('full_name_changed') == 'true':
        user.full_name = request.form['full_name']
    # Username
    if request.form.get('username_changed') == 'true':
        username= request.form['username']
        if len(username) > 0:
            user.username = username
    # Bio
    if request.form.get('bio_changed') == 'true':
        user.description = request.form['bio']
    # Email
    if request.form.get('email_changed') == 'true':
        email = request.form['email']
        user_dict = settings.get_user(user_id)
        user_dict['email'] = email
        settings.add_user(user_dict)
    # Password
    if request.form.get('password_changed') == 'true':
        password = request.form['password']
        password_confirm = request.form['password_confirm']
        if password == password_confirm:
            user_dict = settings.get_user(user_id)
            user_dict['pwd_hash'] = generate_password_hash(password)
            settings.add_user(user_dict)
    # Language
    if request.form.get('language_changed') == 'true':
        user.language = request.form['language']
    return render_template('account.html', user=user, error='Your settings have been saved.')
Пример #2
0
def home():
    """ Shows the user's timeline or if no user is logged in it
    will redirect to the user's public timeline (their most
    recent posts) for public viewing. """
    # Not logged in and multi-user mode.
    if 'user_id' not in session and not settings.get('single_user_mode'):
        return render_template('welcome.html')
    # Not logged in and single-user mode.
    elif 'user_id' not in session and settings.get('single_user_mode'):
        user_id = settings.get('single_user_id')
        username = settings.get_user(user_id)['username']
        return redirect(url_for('get_user_profile', username=username))
    # Logged in
    elif 'user_id' in session:
        user_id = session['user_id']
    # Single-user mode, but no user yet.
    elif settings.get('single_user_mode') and settings.get('single_user_id') is None:
        return redirect(url_for('get_register'))
    # Single-user mode, user exists.
    else:
        user_id = settings.get('single_user_id')
    link = settings.get('registered_users').get(user_id).get('feed_location')
    user = User(local_url=link)
    posts = user.home_timeline()
    auth = True if 'user_id' in session else False
    return render_template('timeline.html', posts=posts, user=user,
            page_type='timeline', auth=auth)
Пример #3
0
def post_status():
    """ Adds a new post to the feed. """
    form = request.form
    status = Status()
    status.description = form['status_text']
    # Reply or Repost
    repost = form.get('repost')
    reply = form.get('reply')
    if repost or reply:
        status.reposted_user_id = form['user_id']
        status.reposted_user_link = form['user_link']
        if repost:
            status.reposted_status_id = form['status_id']
            status.reposted_status_pubdate = form['status_pubdate']

    my_user_id = session['user_id']
    my_user_dict = settings.get_user(my_user_id)
    user = User(local_url=my_user_dict['feed_location'])

    guid = uuid4().hex[-12:]
    reply_url = '{0}/status/{1}/reply'.format(user.profile, guid)
    status = Status({
        'description': status_text,
        'pubdate': datetime.now(pytz.UTC),
        'guid': guid,
        'reply': reply_url
        })
    user.add_post(status)
    return redirect(url_for('home'))
Пример #4
0
def post_block():
    """ Adds a new follow to the user's list. """
    user_link = request.form['follow-url']
    if user_link != '':
        user_id = session.get('user_id')
        user_dict = settings.get_user(user_id)
        user = User(local_url=user_dict['feed_location'])
        user.block(user_link=user_link)
    return redirect(url_for('home'))
Пример #5
0
def post_unblock():
    """ Unfollows a given user. """
    user_link = request.form['user_link']
    username = request.form['username']
    user_id = request.form['user_id']

    my_user_id = session.get('user_id')
    user_dict = settings.get_user(my_user_id)
    user = User(local_url=user_dict['feed_location'])
    user.unblock(user_link=user_link, user_name=username, user_id=user_id)
    return redirect(url_for('home'))
Пример #6
0
def do_login():
    """ Logs the user in. """
    error = ''
    username = request.form['username']
    password = request.form['password']
    username_and_ids = { ud['username']: uid for uid, ud in settings.get('registered_users').iteritems() }

    if username not in username_and_ids.keys():
        error = 'Invalid username'

    pwd_hash =  settings.get_user(username_and_ids[username]).get('pwd_hash')
    if not check_password_hash(pwd_hash, password):
        error = 'Invalid password'
    else:
        session['user_id'] = username_and_ids[username]
        return redirect(url_for('home'))
    return render_template('login.html', error=error)
Пример #7
0
def get_account():
    """ Allows the user to make changes to their profile. """
    user_id = session['user_id']
    location = settings.get_user(user_id)['feed_location']
    user = User(local_url=location)
    return render_template('account.html', user=user)