def getAlbum(): try: response = {} if 'id' in request.args: album = Album.query.get(int(request.args['id'])) if album is not None: song_list = [] songs = Song.query.filter_by(album_id=album.id) for song in songs: userSong = User_Song.query.filter_by( User_Song.song_id == song.id).first() song_list.append( song.export_data(userSong.user_id == g.user_id)) response['album'] = album.export_data() response['song_list'] = song_list return create_success_response(response) return create_error_response("No data found") except Exception as e: return create_error_response(str(e))
def getArtist(): try: response = {} if 'id' in request.args: artist = Artist.query.get(int(request.args['id'])) if artist is not None: album_list = [] albums = Album.query.filter_by(artist_id=artist.id) for album in albums: album_list.append(album.export_data()) response['artist'] = artist.export_data() response['album_list'] = album_list return create_success_response(response) return create_error_response("No data found") except Exception as e: return create_error_response(str(e))
def addSong(): try: if 'song_id' not in request.args: create_error_response("Missing song_id") song = Song.query.get(request.args['song_id']) if song is None: return create_error_response("No such song exists") entry = User_Song() data = {} data['user_id'] = g.user_id data['song_id'] = request.args['song_id'] entry.import_data(data) db.session.add(entry) db.session.commit() return create_success_response() except Exception as e: db.session.rollback() return create_error_response(str(e))
def me(): try: user = User.query.filter_by(id=g.user_id).first() ret = user.export_data() ret['response'] = 'success' return create_success_response(ret) except Exception as e: return create_error_response(str(e))
def getSong(): try: response = {} if 'id' in request.args: song = Song.query.get(int(request.args['id'])) if song is not None: response['song'] = song.export_data() return create_success_response(response) return create_error_response("No data found") except Exception as e: return create_error_response(str(e))
def addSong(): try: data = request.json song = Song() song.import_data(data) db.session.add(song) db.session.commit() return create_success_response(song.export_data()) except Exception as e: db.session.rollback() return create_error_response(str(e))
def addAlbum(): try: data = request.json album = Album() album.import_data(data) db.session.add(album) db.session.commit() return create_success_response(album.export_data()) except Exception as e: db.session.rollback() return create_error_response(str(e))
def addArtist(): try: data = request.json artist = Artist() artist.import_data(data) db.session.add(artist) db.session.commit() return create_success_response(artist.export_data()) except Exception as e: db.session.rollback() return create_error_response(str(e))
def android_google(): try: if 'google_token' not in request.args: return create_error_response("Missing google_token") url = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" + str( request.args['google_token']) response = requests.get(url) data = json.loads(response.text) if response.status_code == 200: email = data.get('email', None) name = data.get('name', None) image_url = data.get('picture', None) profile_sub = data.get('sub', 0) user = User.query.filter_by(email=email).first() # if user has registered with same email before, update if user is not None: user.name = name user.image_url = image_url db.session.add(user) # if user does not exist, create else: user = User() data = {"name": name, "image_url": image_url, "email": email} user.import_data(data) db.session.add(user) db.session.flush() token = create_token(user) db.session.commit() response = {"token": token, "user_detail": user.export_data()} return create_success_response(response) else: return create_error_response( data.get('error_description', 'Failed to login. Please try again.')) except Exception as e: db.session.rollback() return create_error_response(str(e))