Exemplo n.º 1
0
def register():
    """Registers the user."""
    if g.user:
        return redirect(url_for('timeline'))
    error = None
    if request.method == 'POST':
        if not request.form['username']:
            error = 'You have to enter a username'
        elif not request.form['email'] or \
                '@' not in request.form['email']:
            error = 'You have to enter a valid email address'
        elif not request.form['password']:
            error = 'You have to enter a password'
        elif request.form['password'] != request.form['password2']:
            error = 'The two passwords do not match'
        elif len(User.objects.filter(username=request.form['username'])):
            error = 'The username is already taken'
        else:
            user = User(username=request.form['username'],
                          email=request.form['email'],
                          password=request.form['password'])
            user.save()
            flash('You were successfully registered and can login now')
            return redirect(url_for('login'))
    return render_template('register.html', error=error)
Exemplo n.º 2
0
def unfollow_user(username):
    """Removes the current user as follower of the given user."""
    if not g.user:
        abort(401)
    profile_user = User.get_by_username_or_abort(username)
    Follow.objects.get(who=g.user, whom=profile_user).delete()
    flash('You are no longer following "%s"' % username)
    return redirect(url_for('user_timeline', username=username))
Exemplo n.º 3
0
def follow_user(username):
    """Adds the current user as follower of the given user."""
    if not g.user:
        abort(401)
    profile_user = User.get_by_username_or_abort(username)
    Follow(who=g.user, whom=profile_user).save()
    flash('You are now following "%s"' % username)
    return redirect(url_for('user_timeline', username=username))
Exemplo n.º 4
0
def user_timeline(username):
    """Display's a users tweets."""
    profile_user = User.get_by_username_or_abort(username)
    followed = g.user.is_follows(profile_user) if g.user else False
    return render_template('timeline.html',
                           messages=Message.objects.filter(author=profile_user)[:PER_PAGE],
                           followed=followed,
                           profile_user=profile_user)