def view_paste(request,hash_key): """ Given its hash key, visualizes it """ hash_key = str(hash_key) entry = get_object_or_404(Paste, hash_key=hash_key) update_last_view(entry) clean_pastes() # generates syntax highlighting colorized_body = colorize_syntax(entry.body,entry.language) # creates the form for reporting the Paste to admin report_form = ReportForm() report_message = "" if request.method == 'GET': if request.GET.has_key("reported"): report_message = "Thank you for your report!" if request.GET.has_key("error_report"): report_message = "There has been an error. Please contact an administrator!" return render_to_response('view_paste.html', { 'entry': entry, 'colorized_body': colorized_body, 'report_form': report_form, 'report_message': report_message, },context_instance=RequestContext(request))
def discuss_paste(request,hash_key): """ Creates a new Paste, linking it with a previous one. In this way, Pastes may be enhanced and diffs can be performed """ paste_discussed = get_object_or_404(Paste, hash_key=hash_key) update_last_view(paste_discussed) clean_pastes() form = PasteForm(instance=paste_discussed) language = paste_discussed.language private = paste_discussed.private if request.method == 'POST': form = PasteForm(request.POST) if form.is_valid(): tempPaste = form.save(commit=False) tempPaste.hash_key = tempPaste.hash() # recompute the hash key if there is a collision while (is_collision(tempPaste.hash_key)): tempPaste.hash_key = tempPaste.hash() tempPaste.in_response_to = paste_discussed tempPaste.private = private tempPaste.save() return HttpResponseRedirect(tempPaste.get_absolute_url()) # visualize the Paste''' return render_to_response('discuss_paste.html', { 'error_message': None, 'paste_discussed': paste_discussed, 'form': form, },context_instance=RequestContext(request))
def download_paste(request,hash_key): """ Given its hash key, returns the Paste as plain text file """ hash_key = str(hash_key) entry = get_object_or_404(Paste, hash_key=hash_key) update_last_view(entry) clean_pastes() response = HttpResponse(mimetype='text/plain') # set the mimetype for a text file response.write(entry.body) # write it to the response response['Filename'] = 'incollo.com-'+ str(entry.hash_key) +'.txt' response['Content-Disposition'] = 'attachment; filename=incollo.com-'+ str(entry.hash_key) +'.txt' return response
def compare_pastes(request,first_hash_key, second_hash_key): """ Given their hash keys, retrieves two Pastes and compares them. Diff is computed using diff-match-patch by Neil Fraser (http://code.google.com/p/google-diff-match-patch/) Line numbering for snippets is computed manually, only for this view """ first_hash_key = str(first_hash_key) second_hash_key = str(second_hash_key) first_entry = get_object_or_404(Paste, hash_key=first_hash_key) second_entry = get_object_or_404(Paste, hash_key=second_hash_key) update_last_view(first_entry) update_last_view(second_entry) clean_pastes() html_diff = get_html_diff_list(first_entry,second_entry) return render_to_response('compare_pastes.html', { 'diff': html_diff, },context_instance=RequestContext(request))
def add_paste(request): """ Adds a new paste in the database """ clean_pastes() if request.method == 'POST': # If the form has been submitted... form = PasteForm(request.POST) # creates a PasteForm object with the posted data if form.is_valid(): tempPaste = form.save(commit=False) tempPaste.hash_key = tempPaste.hash() # recompute the hash key if there is a collision while (is_collision(tempPaste.hash_key)): tempPaste.hash_key = tempPaste.hash() tempPaste.save() remember_the_paste(request,tempPaste) return HttpResponseRedirect(tempPaste.get_absolute_url()) # visualize the Paste else: form = PasteForm() # A new form return render_to_response('paste_form.html', { 'error_message': None, 'form': form, },context_instance=RequestContext(request))