예제 #1
0
def index(request):
  initial_values = NewSnippetForm.initial
  initial_values['nick'] = request.session.get('nick','anonymous') or 'anonymous'
  form = NewSnippetForm(request.POST or None, initial=initial_values)
  if request.method == 'POST':
    if form.is_valid():
      snip = form.save()
      append_token='';
      if request.user.is_authenticated():
	forms.user = request.user
      else:
	snip.user=None
	request.session['nick'] = snip.nick
	if not snip.visible_to_public or not snip.public_comments:
	  append_token='?access_token='+snip.access_token;
	if snip.access_token == '':
	  snip.access_token = auth.randomToken()
        request.session['snippet_access_token'] = snip.access_token
      if snip.title == 'Enter a title...':
	snip.title = ""
      snip.save()
      
      return HttpResponseRedirect(reverseurl('commentbin.views.snippet',args=[snip.id])+append_token)
  elif request.method != 'GET':
    raise HttpNotImplemented

  snippets = Snippet.objects.filter(visible_to_public=True).order_by('-creation_date')[:10]
  result = {'latest_snippets_list':snippets,
            'form':form,
            'nick':request.session.get('nick','anonymous')
            }
  utils.add_timestamp(result)
  return render_to_response('index.html',result,context_instance=RequestContext(request))
예제 #2
0
def snippet(request,snippet_id):
  try:
    snip = Snippet.objects.get(pk = snippet_id)
  except Snippet.DoesNotExist:
    raise Http404
  
  if snip.formatted_html is None:
    snip.format_code()
  
  if request.method == 'GET':
    params = {'snippet':snip,
              'nick':request.session.get('nick','anonymous') }
    utils.add_timestamp(params)
    
    if not auth.allow(request,snip,'view'):
      if 'access_token' in request.GET:
	params['wrong_password']=True
      return render_to_response('snippet-password.html',params,context_instance=RequestContext(request))

    
    try:
      inlinecomments = Comment.objects.filter(snippet = snip,inlinecomment=True)
      othercomments = Comment.objects.filter(snippet = snip,inlinecomment=False)
      params['comments'] = serializers.serialize("json",inlinecomments,ensure_ascii=False,fields=exportCommentFields);
      params['json-othercomments'] = serializers.serialize("json",othercomments,ensure_ascii=False,fields=exportCommentFields);
      params['threadedcomments'] = flatten_thread(thread((othercomments)))
    except Comment.DoesNotExist:
      pass

    params['show_delete_link'] = auth.allow(request,snip,'delete')
    params['show_comment_interface'] = auth.allow(request,snip,'add_comment')
    params['show_access_token'] = auth.allow(request,snip,'view_access_token')
    params['nick'] = request.session.get('nick','anonymous');
    
    return render_to_response('snippet.html',params,context_instance=RequestContext(request))
  elif request.method == 'DELETE':
    if not auth.allow(request,snip,'delete'):
      raise HttpPermissionDenied
    else:
      snip.delete()
      result = { "status":"Ok",
                 "redirect":reverseurl('commentbin.views.index') }
      return HttpJSONResponse(result)
  else:
    raise HttpNotImplemented
예제 #3
0
def login(request):
  return HttpResponseRedirect(reverseurl('commentbin.views.index'))
예제 #4
0
def logout(request):
  djangoauth.logout(request)
  return HttpResponseRedirect(reverseurl('commentbin.views.index'))