Beispiel #1
0
def like(id):
    if not username():
        return redirect(url_for('auth.login'))

    post = Post.read(id)

    # post author can't like her own post
    if post['user_id'] == user_id():
        return redirect(url_for('.show', id=id), 403)

    # post can only be liked once
    if user_id() not in post['liked_by']:
        post['liked_by'].append(user_id())
        post['like'] += 1

    Post.put(post)

    return redirect(url_for('.show', id=id))


# [END likes]
Beispiel #2
0
def mypost():
    if not username():
        return redirect(url_for('auth.login'))

    token = request.args.get('page_token', None)
    if token:
        token = token.encode('utf-8')

    posts, next_page_token = Post.index(cursor=token, user_id=user_id())

    return __render_template('index.html',
                             posts=posts,
                             next_page_token=next_page_token)
Beispiel #3
0
def add_comment(id):
    if not username():
        return redirect(url_for('auth.login'))

    if request.method == 'POST':
        data = request.form.to_dict(flat=True)
        data['username'] = username()
        data['user_id'] = user_id()
        data['post_id'] = id

        comment = Comment.upsert(data)

        return redirect(url_for('.show', id=id))

    return __render_template('comment.html', action='Add', comment={})
Beispiel #4
0
def edit(id):
    post = Post.read(id)

    if not can_edit(post):
        return redirect(url_for('auth.login'))

    if request.method == 'POST':
        data = request.form.to_dict(flat=True)
        data['user_id'] = user_id()
        data['username'] = username()

        post = Post.upsert(data, id)

        return redirect(url_for('.show', id=post['id']))

    return __render_template('post.html', action='Edit', post=post)
Beispiel #5
0
def add():
    if not username():
        return redirect(url_for('auth.login'))

    if request.method == 'POST':
        data = request.form.to_dict(flat=True)
        data['user_id'] = user_id()
        data['username'] = username()
        data['like'] = 0
        data['liked_by'] = ['']

        post = Post.upsert(data)

        return redirect(url_for('.show', id=post['id']))

    return __render_template('post.html', action='Add', post={})
Beispiel #6
0
def edit_comment(id):
    comment = Comment.read(id)

    if not can_edit(comment):
        return redirect(url_for('auth.login'), 403)
    elif request.method == 'POST':
        data = request.form.to_dict(flat=True)
        data['username'] = username()
        data['user_id'] = user_id()
        data['post_id'] = comment['post_id']

        comment = Comment.upsert(data, id)

        return redirect(url_for('.show', id=comment['post_id']))

    return __render_template('comment.html', action='Edit', comment=comment)
Beispiel #7
0
def can_edit(obj):
    return obj['user_id'] == user_id()
Beispiel #8
0
def show(id):
    post = Post.read(id)
    return __render_template('show.html', post=post, user_id=user_id())