def get_comment(request,oid): try: user = authenticate_api_request(request) authorize_user(user, request, "get") comment = Discussion.objects.get(id=oid) except ReadJSONError as e: return server_error(e.value) except (AuthenticationFailed, AuthorizationError) as e: return server_error(e.value) data = get_comment_data(comment,user) return server_data(data)
def edit_comment(request,oid): try: user = authenticate_api_request(request) authorize_user(user, request, "edit") content = get_json_post_or_error('commentContent', request) except ReadJSONError as e: return server_error(e.value) except (AuthenticationFailed, AuthorizationError) as e: return server_error(e.value) comment = CategoryDiscussion.objects.create(id=oid,content=content) data = get_comment_data(comment,user) return server_data(data)
def rate_comment(request,oid): try: user = authenticate_api_request(request) authorize_user(user, request, "rate") rating = get_json_get_or_error('rating', request) comment = Discussion.objects.get(id=oid) except ReadJSONError as e: return server_error(e.value) except (AuthenticationFailed, AuthorizationError) as e: return server_error(e.value) except: return server_error('Comment with id '+str(oid)+'not found') CommentRating.objects.create(user=user,rating=rating,comment=comment) data = get_comment_data(comment,user) return server_data(data)
def add_comment(request): try: user = authenticate_api_request(request) oid = get_json_get_or_error('commentBaseID', request) commentType = get_json_get_or_error('type', request) content = get_json_post_or_error('commentContent', request) except ReadJSONError as e: return server_error(e.value) except (AuthenticationFailed, AuthorizationError) as e: return server_error(e.value) try: if 'replyTo' in request.GET: replyToID = request.GET['replyTo'] replyComment = Discussion.objects.get(id=replyToID) else: replyComment = None except: return server_error("No comment found with id "+str(replyToID)) if commentType == 'business': try: bus = Business.objects.get(id=oid) except: return server_error("Business with ID "+str(oid)+ " does not exist") comment = BusinessDiscussion.objects.create(user=user,reply_to=replyComment,content=content,business=bus) elif commentType == 'category': try: btag = BusinessTag.objects.get(id=oid) except: return server_error("Category with ID "+str(oid)+ " does not exist") comment = CategoryDiscussion.objects.create(user=user,reply_to=replyComment,content=content,businesstag=btag) elif commentType == 'photo': try: photo = Photo.objects.get(id=oid) except: return server_error("Photo with ID "+str(oid)+ " does not exist") comment = PhotoDiscussion.objects.create(user=user,reply_to=replyComment,content=content,photo=photo) else: return server_error("Invalid commentType "+str(commentType)) data = get_comment_data(comment,user) return server_data(data)