Пример #1
0
def paste(request, template="pastebin/paste.html"):

    if request.method == 'POST' and request.POST:
        if "url" in request.POST:
            url = request.POST['url']
            snippet = Snippet.objects.get(url__exact=url)
            
            if snippet.locked:
                messages.error(request, "Error: This snippet is locked.")
                return redirect('detail', url=url)

            form = SnippetForm(request.POST, instance=snippet)
            if form.is_valid():
                snippet = form.save()
                messages.success(request, "Snippet updated!")
                
                return redirect('detail', url=url)
            else: return redirect('detail', url=url)

        form = SnippetForm(request.POST)
        if form.is_valid():
            snippet = form.save()
            messages.success(request, "Snippet saved!")
    
            return redirect('detail', url=snippet.url)
    else:
        form = SnippetForm(initial=request.POST)
    
    context = {
        'form': form,
    }

    return render(request, template, context)
Пример #2
0
def paste(request, template="paste.html"):

    if request.method == 'POST' and request.POST:
        if "url" in request.POST:
            url = request.POST['url']
            snippet = Snippet.objects.get(url__exact=url)

            if snippet.locked:
                messages.error(request, "Error: This snippet is locked.")
                return redirect('detail', url=url)

            form = SnippetForm(request.POST, instance=snippet)
            if form.is_valid():
                snippet = form.save()
                messages.success(request, "Snippet updated!")

                return redirect('detail', url=url)
            else:
                return redirect('detail', url=url)

        form = SnippetForm(request.POST)
        if form.is_valid():
            snippet = form.save()
            messages.success(request, "Snippet saved!")

            return redirect('detail', url=snippet.url)
    else:
        form = SnippetForm(initial=request.POST)

    context = {
        'form': form,
    }

    return render(request, template, context)
Пример #3
0
    def post(self, request):
        snippet_form = SnippetForm(data=request.POST, request=request)
        if snippet_form.is_valid():
            request, new_snippet = snippet_form.save()
            return HttpResponseRedirect(new_snippet.get_absolute_url())

        return self._render_response(request, snippet_form)
Пример #4
0
    def post(self, request):
        snippet_form = SnippetForm(data=request.POST, request=request)
        if snippet_form.is_valid():
            request, new_snippet = snippet_form.save()
            return HttpResponseRedirect(new_snippet.get_absolute_url())

        return self._render_response(request, snippet_form)
Пример #5
0
def snippet_details(request, snippet_id, template_name='pastebin/snippet_details.html', is_raw=False):

    # housekeeping
    _clean_expired_snippets()

    try:
        snippet = Snippet.objects.get(secret_id=snippet_id)
    except MultipleObjectsReturned:
        raise Http404('Multiple snippets exist for this slug. This should never '
                      'happen but its likely that you are a spam bot, so I dont '
                      'care.')
    except ObjectDoesNotExist:
        raise Http404('This snippet does not exist anymore. Its likely that its '
                      'lifetime is expired.')

    new_snippet_initial = {
        'content': snippet.content,
        'lexer': snippet.lexer,
    }

    if request.method == "POST":
        snippet_form = SnippetForm(data=request.POST, request=request, initial=new_snippet_initial)
        if snippet_form.is_valid():
            request, new_snippet = snippet_form.save(parent=snippet)
            return HttpResponseRedirect(new_snippet.get_absolute_url())
    else:
        snippet_form = SnippetForm(initial=new_snippet_initial, request=request)

    snippet_list_ = _get_snippet_list(no_content=True)
    template_context = {
        'snippet_list': snippet_list_,
        'snippet_form': snippet_form,
        'snippet': snippet,
        'lines': range(snippet.get_linecount()),
    }

    response = render_to_response(
        template_name,
        template_context,
        RequestContext(request)
    )

    if is_raw:
        response['Content-Type'] = 'text/plain;charset=UTF-8'
        return response
    else:
        return response
Пример #6
0
def snippet_new(request, template_name='pastebin/snippet_new.html'):

    if request.method == "POST":
        snippet_form = SnippetForm(data=request.POST, request=request)
        if snippet_form.is_valid():
            request, new_snippet = snippet_form.save()
            return HttpResponseRedirect(new_snippet.get_absolute_url())
    else:
        # housekeeping
        _clean_expired_snippets()
        snippet_form = SnippetForm(request=request)

    snippet_list_ = _get_snippet_list(no_content=True)

    template_context = {
        'snippet_form': snippet_form,
        'snippet_list': snippet_list_,
    }

    return render_to_response(
        template_name,
        template_context,
        RequestContext(request)
    )