def get(self, audioFileType, id=None): try: if audioFileType == 'song': if id: audios = Song.objects(id=id).to_json() else: audios = Song.objects().to_json() elif audioFileType == 'podcast': if id: audios = Podcast.objects(id=id).to_json() else: audios = Podcast.objects().to_json() elif audioFileType == 'audiobook': if id: audios = AudioBook.objects(id=id).to_json() else: audios = AudioBook.objects().to_json() else: context = {"error":"Invalid Audio type"} return (context, status.HTTP_400_BAD_REQUEST) if audios == '[]': context = {'error':'Audio matching ID does not exist'} return (context, status.HTTP_400_BAD_REQUEST) return Response(audios, mimetype="application/json", status=status.HTTP_200_OK) except Exception as e: context = {'error':str(e)} return (context, status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_Delete_Songs(self): headers = {'Content-type': 'application/json'} id = Song.objects()[0].id response = self.tester.delete('/deleteaudio/song/' + str(id) + '/') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, b'{"message":"Successfully Deleted"}\n')
def song(text,chat): s = Song.is_song(text) if s not None: c = Chat.get_chat(chat) t = Tracker.find_tracker(c,s) return t.next_line(text)
def test_Update_Songs(self): headers = {'Content-type': 'application/json'} id = Song.objects()[0].id test_data = json.dumps({ "name": "new_name", "duration": "4", "uploaded_time": "2021-02-19 11:11", }) response = self.tester.put('/updateaudio/song/' + str(id) + '/', data=test_data, headers=headers) self.assertEqual(response.status_code, 200) #print("r", response.status_code, response.data) self.assertEqual(response.data, b'{"message":"Successfully Updated"}\n')
def post(self): try: data = request.json validated = self.validate_this(data) if not validated[0]: return (validated[1], status.HTTP_400_BAD_REQUEST) audio_type = data['audioFileType'].lower() audio_data = data['audioFileMetadata'] if audio_type == 'song': try: s = SongSerializer().load(audio_data) audio = Song(**s).save() id = audio.id return ({'id': id}, status.HTTP_200_OK) except ValidationError as e: context = dict(e.messages) return (context, status.HTTP_400_BAD_REQUEST) except Exception as e: context = {'error':str(e)} return (context, status.HTTP_400_BAD_REQUEST) elif audio_type == 'podcast': try: p = PodcastSerializer().load(audio_data) audio = Podcast(**p).save() id = audio.id return ({'id': id}, status.HTTP_200_OK) except ValidationError as e: context = dict(e.messages) return (context, status.HTTP_400_BAD_REQUEST) except Exception as e: context = {'error':str(e)} return (context, status.HTTP_400_BAD_REQUEST) elif audio_type == 'audiobook': try: a = AudioBookSerializer().load(audio_data) audio = AudioBook(**a).save() id = audio.id return ({'id': id}, status.HTTP_200_OK) except ValidationError as e: context = dict(e.messages) return (context, status.HTTP_400_BAD_REQUEST) except Exception as e: context = {'error':str(e)} return (context, status.HTTP_400_BAD_REQUEST) else: context = {"error":"Invalid Audio type"} return (context, status.HTTP_400_BAD_REQUEST) except Exception as e: context = {'error':str(e)} return (context, status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_Read_Songs(self): id = Song.objects()[0].id response = self.tester.get('/audio/song/' + str(id) + '/') self.assertEqual(response.status_code, 200) self.assertEqual(response.get_json()[0]['_id'], id)
def get_songs(): songs = Song.objects().to_json() return Response(movies, mimetype="application/json", status=200)
app.config['MONGODB_SETTINGS'] = { 'host': 'mongodb://localhost/song-crate' } db = initialize_db(app) # index @app.route('/songs') def get_songs(): songs = Song.objects().to_json() return Response(movies, mimetype="application/json", status=200) # create @app.route('/songs', methods=['POST']) body = request.get_json() song = Song(**body).save() id = song.id return {'id': str(id)}, 200 # read @app.route('/movies/<id>') def get_song(id): movies = Movie.objects.get(id=id).to_json() return Response(movies, mimetype="application/json", status=200) # Update def update_movie(id): body = request.get_json() Movie.objects.get(id=id).update(**body) return '', 200