Ejemplo n.º 1
0
def edit_snippet(request,
                 snippet_id=None,
                 template_name='cab/edit_snippet.html'):
    if not request.user.is_active:
        return HttpResponseForbidden()

    if snippet_id:
        snippet = get_object_or_404(Snippet, pk=snippet_id)
        if request.user.id != snippet.author.id:
            return HttpResponseForbidden()
    else:
        template_name = 'cab/add_snippet.html'
        snippet = Snippet(author=request.user,
                          language=Language.objects.get(name='Python'))

    if request.method == 'POST':
        form = SnippetForm(instance=snippet, data=request.POST)
        if form.is_valid():
            snippet = form.save()
            messages.info(request, 'Your snippet has been saved')
            return redirect(snippet)
    else:
        form = SnippetForm(instance=snippet)

    return render(request, template_name, {'form': form})
Ejemplo n.º 2
0
def edit_snippet(request, snippet_id=None, template_name='cab/edit_snippet.html'):
    if snippet_id:
        snippet = get_object_or_404(Snippet, pk=snippet_id)
        if request.user.id != snippet.author.id:
            return HttpResponseForbidden()
    else:
        template_name = 'cab/add_snippet.html'
        snippet = Snippet(author=request.user, language=Language.objects.get(name='Python'))
    if request.method == 'POST':
        form = SnippetForm(instance=snippet, data=request.POST)
        if form.is_valid():
            snippet = form.save()
            return HttpResponseRedirect(snippet.get_absolute_url())
    else:
        form = SnippetForm(instance=snippet)
    return render_to_response(template_name,
        {'form': form}, context_instance=RequestContext(request))
Ejemplo n.º 3
0
def edit_snippet(request, snippet_id=None,
                 template_name='cab/edit_snippet.html'):
    if not request.user.is_active:
        return HttpResponseForbidden()

    if snippet_id:
        snippet = get_object_or_404(Snippet, pk=snippet_id)
        if request.user.id != snippet.author.id:
            return HttpResponseForbidden()
    else:
        template_name = 'cab/add_snippet.html'
        snippet = Snippet(author=request.user,
                          language=haskell())

    if request.method == 'POST':
        form = SnippetForm(instance=snippet, data=request.POST)
        if form.is_valid():
            snippet = form.save()
            messages.info(request, 'Your snippet has been saved')
            return redirect(snippet)
    else:
        form = SnippetForm(instance=snippet)

    return render(request, template_name, {'form': form})