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 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 get (self, request, tablature_id, comment_id): ''' Get comment. Return 404 if comment doesn't exist. Return 200 on success. ''' #Get the model commentmodel = database.get_comment(comment_id) if commentmodel is None: error = ErrorModel("The comment "+ comment_id+ " is not in the archive").serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) #Serialize and modify the result so the sender is linked to its url comment = {} comment["comment"] = commentmodel.serialize() senderurl = reverse("user",(commentmodel.user_nickname,), request=request) comment['user'] = {'user_nickname':commentmodel.user_nickname, 'link':{'rel':'self','href':senderurl}} #If replyto exists, include the url of the reply comment replytocomment_url = None if commentmodel.reply_to != "": replytocomment_url = reverse("comment", (commentmodel.tablature_id, commentmodel.reply_to), request=request) comment['reply_to'] = {'rel':'self','href':replytocomment_url } return Response(comment, status=status.HTTP_200_OK)