Exemplo n.º 1
0
def post_detail(request, blog_name=None, post_key_name=None):
    post = BlogPost.get_by_key_name(post_key_name,
                                    parent=db.Key.from_path('Blog', blog_name))
    if not post:
        return HttpResponse404()
    return render_to_response('post_detail.html', {'post': post},
                              RequestContext(request))
Exemplo n.º 2
0
def edit_post(request, blog_name=None, post_key_name=None):
    user = users.get_current_user()
    if not user:
        return HttpResponseForbidden()
    if user.nickname() != blog_name:
        return HttpResponseForbidden()
    post = BlogPost.get_by_key_name(post_key_name,
                                    parent=db.Key.from_path('Blog', blog_name))
    if not post:
        return HttpResponse404()
    return render_to_response('post_form.html', {'post': post},
                              RequestContext(request))
Exemplo n.º 3
0
def delete_post(request, blog_name=None, post_key_name=None):
    user = users.get_current_user()
    if not user:
        return HttpResponseForbidden()
    if user.nickname() != blog_name:
        return HttpResponseForbidden()
    post = BlogPost.get_by_key_name(post_key_name,
                                    parent=db.Key.from_path('Blog', blog_name))
    if not post:
        return HttpResponse404()
    try:
        post.delete()
    except Exception, e:
        return HttpResponseServerError()
Exemplo n.º 4
0
def insert_post_with_unique_key(post_dict):
    """
    First we try to add the blog post with key name equal to the slug of the
    post's title.
    If this key is already taken we append a random two characters and repeat
    until a free key is generated. The blog post is then saved with this new
    key.

    In all queries we specify the parent as the blog with name the author's
    nickname. This means that all queries run on a single entity group so we
    don't have to use a cross-group transaction. Also this means two users can
    be posting at the same time as the transaction only locks the entity group
    corresponding to the current logged in user.
    """
    key_name_base = unicode(slugify(post_dict['title']))
    key_name = key_name_base
    parent_key = blog_key(post_dict['author'])
    while BlogPost.get_by_key_name(key_name, parent=parent_key):
        key_name = '%s-%s' % (key_name_base, ''.join([random.choice(
            string.ascii_letters + string.digits) for i in range(2)]))
    post = BlogPost(parent=parent_key, key_name=key_name, **post_dict)
    post.put()
    return post