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)
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)
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
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 = '\n'.join([ '%s: %s' % (k, v.as_text()) for k, v in snippet_form.errors.items() ]) raise SnippetValidationError(errors) self._snippet_form = snippet_form
def get(self, request, snippet_id): # load snippet try: snippet = self._fetch_snippet(snippet_id) except SnippetNotFoundError as exc: # 404 response with custom message context = dict(message=exc) return TemplateResponse(request, 'errors/404.html', context=context, status=404) new_snippet_initial = dict(content=snippet.content, lexer=snippet.lexer) 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()), } return render(request, self.template_name, template_context)
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
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)
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) )
def detail(request, url, raw=False, template="detail.html"): snippet = get_object_or_404(Snippet, url__exact=url) if snippet.is_expired(): messages.error(request, "Snippet has expired =(") return redirect('paste') if raw: response = HttpResponse(snippet.code, content_type="text/plain") return response form = SnippetForm(instance=snippet) lines = range(1, snippet.line_count() + 1) context = { 'snippet': snippet, 'lines': lines, 'form': form, } return render(request, template, context)
def get(self, request): snippet_form = SnippetForm(request=request) return self._render_response(request, snippet_form)