def comment(request,snippet_id,comment_id): try: snip = Snippet.objects.get(pk = snippet_id) comment = Comment.objects.get(pk = comment_id, snippet = snip) except: raise Http404 if request.method == 'GET': if not auth.allow(request,comment,'view'): raise HttpPermissionDenied result = { "comment":serializers.serialize("json",[comment],ensure_ascii=False,fields=exportCommentFields), "status":"Ok" } if auth.allow(request,comment,'view_access_token'): result['access_token']=comment.access_token return HttpJSONResponse( result ) elif request.method == 'PUT': utils.coerce_post(request) if not auth.allow(request, comment, 'change'): raise HttpPermissionDenied comment.text = request.PUT["text"] comment.start = int(request.PUT["start"]) comment.end = int(request.PUT["end"]) comment.save() result = { "comment":serializers.serialize("json",[comment],ensure_ascii=False,fields=exportCommentFields), "status":"Ok" } return HttpJSONResponse( result ) elif request.method == 'DELETE': if not auth.allow(request,comment,'delete'): raise HttpPermissionDenied comment.delete() return HttpJSONResponse( {'status':'Ok', 'deletedID':comment_id} ) elif request.method == 'POST': if not auth.allow(request,snip,'add_comment'): raise HttpPermissionDenied reply_comment = commentFromRequest(request,snip) reply_comment.replyto = comment reply_comment.save() request.session['nick'] = utils.getNick( request ) result = { "comment":serializers.serialize("json",[reply_comment],ensure_ascii=False,fields=exportCommentFields), "clientid":int(request.POST["id"]), 'access_token':request.session['comment_access_token'], "status":"Ok" } return HttpJSONResponse( result ) else: raise HttpNotImplemented
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
def comments(request,snippet_id): try: snip = Snippet.objects.get(pk = snippet_id) except Snippet.DoesNotExist: raise Http404 if request.method == 'GET': try: from datetime import datetime created_after = datetime.fromtimestamp(int(float(request.GET["created_after"]))) except: created_after = datetime.fromtimestamp(0) try: inlinecomments = Comment.objects.filter(snippet = snip, creation_date__gte = created_after, inlinecomment = True) othercomments = Comment.objects.filter(snippet = snip, creation_date__gte = created_after, inlinecomment = False) result = { "comments":serializers.serialize("json",inlinecomments,ensure_ascii=False,fields=exportCommentFields), "othercomments":serializers.serialize("json",othercomments,ensure_ascii=False,fields=exportCommentFields), "status":"Ok" } except: result = { "status":"Fail" } return HttpJSONResponse( result ) elif request.method == 'POST': if not auth.allow(request,snip,'add_comment'): raise HttpPermissionDenied comment = commentFromRequest(request, snip) comment.save() request.session['nick'] = utils.getNick( request ) result = { "comment":serializers.serialize("json",[comment],ensure_ascii=False,fields=exportCommentFields), "clientid":int(request.POST["id"]), 'access_token':request.session['comment_access_token'], "status":"Ok" } return HttpJSONResponse( result ) else: raise HttpNotImplemented