Exemplo n.º 1
0
    def _validate_against_snippet_form(self):
        self._preprocess_data()

        snippet_form = SnippetForm(request=self._request, data=self._data)
        # SnippetForm allows only a subset of lexer choices except the user enables all lexers
        # in her session but since we don't have a session here, we allow all unconditionally
        snippet_form.fields['lexer'].choices = LEXER_LIST_ALL
        # validate
        if not snippet_form.is_valid():
            errors = u'\n'.join([u'%s: %s' % (k, v.as_text()) for k, v in snippet_form.errors.items()])
            raise SnippetValidationError(errors)
        self._snippet_form = snippet_form
Exemplo n.º 2
0
def snippet_details(request, snippet_id, template_name='dpaste/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.')

    tree = snippet.get_root()
    tree = tree.get_descendants(include_self=True)

    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()
    template_context = {
        'snippet_list': snippet_list,
        'snippet_form': snippet_form,
        'snippet': snippet,
        'lines': range(snippet.get_linecount()),
        'tree': tree,
    }

    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
Exemplo n.º 3
0
def snippet_new(request, template_name='dpaste/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:
        snippet_form = SnippetForm(request=request)

    template_context = {
        'snippet_form': snippet_form,
    }

    return render_to_response(
        template_name,
        template_context,
        RequestContext(request)
    )
Exemplo n.º 4
0
def snippet_details(request, snippet_id, template_name='dpaste/snippet_details.html', is_raw=False):

    snippet = get_object_or_404(Snippet, secret_id=snippet_id)

    tree = snippet.get_root()
    tree = tree.get_descendants(include_self=True)

    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)

    template_context = {
        'snippet_form': snippet_form,
        'snippet': snippet,
        'lines': range(snippet.get_linecount()),
        'tree': tree,
    }

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

    if is_raw:
        response['Content-Type'] = 'text/plain'
        return response
    else:
        return response