Пример #1
0
def snippet_edit(request, snippet_id, profile_callback=None):
    snippet = Snippet.get_by_id(int(snippet_id))
    if not snippet:
        raise Http404

    if request.method == "GET":
        tags = ""
        if snippet.tags:
            tags = ",".join([t.name for t in snippet.get_tags() if t])
        form = SnippetForm(
            {
                "title": snippet.title,
                "language": snippet.language,
                "code": snippet.code,
                "url": snippet.url,
                "comment": snippet.comment,
                "private": snippet.private,
                "tag": tags,
            }
        )
    if request.method == "POST":
        form = SnippetForm(request.POST)
        logging.getLogger().debug(form)
        if form.is_valid():
            updated_snippet = form.edit(int(snippet_id), profile_callback=profile_callback)
            memcache.flush_all()
            ping()
            return HttpResponseRedirect(updated_snippet.get_absolute_url())

    return render_to_response(
        "snippet/snippet_edit.html", {"form": form, "snippet": snippet}, context_instance=RequestContext(request)
    )
Пример #2
0
def snippet_add(request, profile_callback=None):
    if request.method == "GET":
        form = SnippetForm()
    if request.method == "POST":
        form = SnippetForm(request.POST)
        logging.getLogger().debug(form)
        if form.is_valid():
            new_snippet = form.save(profile_callback=profile_callback)
            memcache.flush_all()
            ping()
            return HttpResponseRedirect("/")

    return render_to_response(
        "snippet/snippet_add.html",
        {"form": form, "CURRENT_PAGE": "snippet_add"},
        context_instance=RequestContext(request),
    )