def snippet_new(request): if request.method == "POST": snippet = Snippet() if request.user.is_authenticated(): snippet.author = request.user snippet_form = SnippetForm(request.POST, request.FILES, request=request, instance=snippet) if snippet_form.is_valid(): request, new_snippet = snippet_form.save() return redirect(new_snippet) else: snippet_form = SnippetForm(request=request) recent = Snippet.objects.public()[:10] context = { 'snippet_form': snippet_form, 'recent_snippets' : recent, } return render(request, 'snippet/snippet_new.djhtml', context)
def snippet_details(request, snippet_id, template_name='snippet/snippet_details.djhtml', is_raw=False): snippet = get_object_or_404(Snippet, secret_id=snippet_id) snippet.num_views += 1 snippet.save() tree = snippet.get_root() tree = tree.get_descendants(include_self=True) if snippet.title.startswith('Re: '): reply_title = snippet.title else: reply_title = 'Re: %s' % snippet.title new_snippet_initial = { 'content': snippet.content, 'lexer': snippet.lexer, 'title': reply_title, } if request.method == "POST": snippet_form = SnippetForm(request.POST, request.FILES, request=request, initial=new_snippet_initial) if snippet_form.is_valid(): request, new_snippet = snippet_form.save(parent=snippet) return redirect(new_snippet) else: snippet_form = SnippetForm(initial=new_snippet_initial, request=request) if request.user.is_authenticated(): default_pygments_style = request.user.get_profile().pygments_style else: default_pygments_style = PygmentsStyle.objects.get(pk=1) context = { 'snippet_form': snippet_form, 'snippet': snippet, 'lines': range(snippet.get_linecount()), 'tree': tree, 'language': dict(LEXER_LIST)[snippet.lexer], 'pygments_styles': PygmentsStyle.objects.all(), 'default_style': default_pygments_style, 'no_descendants': len(tree) == 1, } response = render(request, template_name, context) if is_raw: response['Content-Type'] = 'text/plain' return response else: return response