def delete(self, request, tablature_id): ''' Delete tablature. Requires authorization. If not authorized return 401. ''' if not database.contains_tablature(tablature_id): error = ErrorModel("The tablature "+ tablature_id+ " is not in the archive").serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) 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(tablature_id, authorization): return self._deletetablature(tablature_id) else: return Response(status=status.HTTP_401_UNAUTHORIZED)
def post(self, request, tablature_id): ''' Give your own rating to a tablature. Rating is incremented by the value given and rating count is incremented by 1. ''' #request.DATA contains the request body already deserialized in a #python dictionary if not request.DATA: error = ErrorModel('The artist_id, song_id and body of the tablature\ cannot be empty').serialize() return Response(error, status=status.HTTP_400_BAD_REQUEST) if not database.contains_tablature(tablature_id): error = ErrorModel("The tablature "+ tablature_id+ " is not in the archive").serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) #Deserialize and modify the data in the tablature. try: tablature_id = tablature_id newrating = request.DATA['rating'] rating = database.add_rating(tablature_id, newrating) except Exception: return Response(status=status.HTTP_400_BAD_REQUEST) return Response((rating[0], rating[1]), status=status.HTTP_200_OK)
def post(self, request, tablature_id): ''' Post a comment to tablature by id. Requires authorization. Returns 400 on bad request. ''' if not request.DATA: error = ErrorModel('The artist_id, song_id and the body of the tablature\ cannot be empty').serialize() return Response(error, status=status.HTTP_400_BAD_REQUEST) if not database.contains_tablature(tablature_id): error = ErrorModel('Tablature was not found.').serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) commentmodel = None try: commentmodel = CommentModel(None, raw_data=request.DATA) user_nickname = commentmodel.user_nickname commentmodel.tablature_id = tablature_id except Exception as e: print "Could not add the data "+ str(e) traceback.print_exc() return Response(status = 400) 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(user_nickname, authorization): return self._createcomment(commentmodel, request) else: return Response(status=status.HTTP_401_UNAUTHORIZED)
def put(self, request, tablature_id): ''' Modify tablature. Requires authorization. ''' #request.DATA contains the request body already deserialized in a #python dictionary if not request.DATA: error = ErrorModel('The artist_id, song_id and body of the tablature\ cannot be empty').serialize() return Response(error, status=status.HTTP_400_BAD_REQUEST) if not database.contains_tablature(tablature_id): error = ErrorModel("The tablature "+ tablature_id+ " is not in the archive").serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) #request.DATA contains a dictionary with the entity body input. #Deserialize the data and modify the model tablaturemodel = TablatureModel(tablature_id) try: #EXTRACT THE PRIVATE DATA if request.DATA.has_key("body"): body = request.DATA['body'] else: body = None if request.DATA.has_key("artist_id"): artist_id = request.DATA["artist_id"] else: artist_id = None if request.DATA.has_key("song_id"): song_id = request.DATA['song_id'] else: song_id = None #SET VALUES TO USER tablaturemodel.tablature_id = tablature_id tablaturemodel.body = body tablaturemodel.artist_id = artist_id tablaturemodel.song_id = song_id except Exception as e: print "Could not add the data "+ str(e) traceback.print_exc() return Response(status = 400) 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(tablature_id, authorization): return self._modifytablature(tablaturemodel, request) else: return Response(status=status.HTTP_401_UNAUTHORIZED)