def put(self, request, tablature_id, comment_id): ''' Modify comment. Requires authorization. ''' #request.DATA contains the request body already deserialized in a #python dictionary if not request.DATA: error = ErrorModel('The the body of the comment\ cannot be empty').serialize() return Response(error, status=status.HTTP_400_BAD_REQUEST) if not database.contains_comment(comment_id): error = ErrorModel("The comment "+ comment_id+ " is not in the archive").serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) commentmodel = database.get_comment(comment_id) commentmodel.body = request.DATA["body"] authorization = '' try: authorization = request.user.username #request.META["HTTP_AUTHORIZATION"] try: if sys.argv[1] == "test": authorization = request.META["HTTP_AUTHORIZATION"] except IndexError: pass except KeyError: pass if self._modifyisauthorized(commentmodel, authorization): return self._modifycomment(commentmodel, request) else: return Response(status=status.HTTP_401_UNAUTHORIZED)
def delete(self, request, tablature_id, comment_id): ''' Delete comment. Requires authorization. Returns 401 on authorized request. Returns 404 if comment doesn't exist. ''' authorization = '' try: authorization = request.user.username #request.META["HTTP_AUTHORIZATION"] try: if sys.argv[1] == "test": authorization = request.META["HTTP_AUTHORIZATION"] except IndexError: pass except KeyError: pass if not database.contains_comment(comment_id): error = ErrorModel("The comment " + str(comment_id) + " is not in the archive").serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) commentmodel = database.get_comment(comment_id) if self._modifyisauthorized(commentmodel, authorization): return self._deletecomment(commentmodel.comment_id, request) else: return Response(status=status.HTTP_401_UNAUTHORIZED)
def post(self, request, tablature_id, comment_id): ''' Post reply to an existing comment. ''' #request.DATA contains the request body already deserialized in #a python dictionary if not request.DATA: error = ErrorModel('The body of the comment\ cannot be empty').serialize() return Response(error, status=status.HTTP_400_BAD_REQUEST) if not database.contains_comment(comment_id): error = ErrorModel("The comment "+comment_id+ " is not in the archive").serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) commentmodel = None try: commentmodel = CommentModel('', raw_data=request.DATA) except Exception as e: print "Could not add the data "+ str(e) traceback.print_exc() return Response(status = 400) commentmodel.reply_to = comment_id commentmodel.tablature_id = tablature_id authorization = '' try: authorization = request.user.username #request.META["HTTP_AUTHORIZATION"] try: if sys.argv[1] == "test": authorization = request.META["HTTP_AUTHORIZATION"] except IndexError: pass except KeyError: pass if self._isauthorized(commentmodel.user_nickname, authorization): return self._createreply(commentmodel, request) else: return Response(status=status.HTTP_401_UNAUTHORIZED)