As the title suggests I am trying to recursively call one of my class functions as a COMPLETELY NEW request. When I do the following it will recursively call the function correctly but it is called from within the SAME REQUEST.
return DBFunctions.addSongEnd(self, i, "1", finalSList, albumDict)
I have tried doing something similar to passing the data through requests.post but have troubled passing the data in various fashions:
- requests.post(url?varA?varB)
- requests.post(url, data={})
- requests.post(url, json={})
- requests.post(url, start=s, status=s2, albumD=albumDict, songL=songDict)
- requests.post(url_for(DBFunctions.addSongEnd(self, i, "1", finalSList, albumDict)))
I was not able to pass the data effectively using any of these which is why I have resorted to DBFunctions.addSongEnd, however I need a completely new request to be made
Do I have the right idea? Is there a better approach to go about this? Any suggestions are greatly appreciated
My class setup:
class DBFunctions(FlaskView):
default_methods = ['GET', 'POST']
def addSongEnd(self, start, status, songL, albumD):
pass
start = int(start)
# START TIMER FOR FUNCTION
startTime = time.perf_counter();
if status == "0":
finalSList = request.form['songList']
finalSList = json.loads(finalSList)
albumDict = {}
else:
finalSList = songL
albumDict = albumD
try:
sessionUser = dbSession.query(User).filter_by(username=session['username']).scalar()
except:
dbSession.rollback()
sessionUser = dbSession.query(User).filter_by(username=session['username']).scalar()
for i in range(start, len(finalSList)):
currentTime = time.perf_counter()
timeDiff = currentTime - startTime
if (timeDiff >= 10):
# IF TIMER NOT EQUAL TO 290, call addSongFun with startingIndex
# Try return redirect here
return DBFunctions.addSongEnd(self, i, "1", finalSList, albumDict)
else:
songName = finalSList[i][0]
artistName = finalSList[i][1][0]
tP = finalSList[i][1][1]
tC = finalSList[i][1][2]
songResponse = list(json.loads(getSong(artistName, songName)))
albumName = str(songResponse[0])
uri = songResponse[1]
if albumName not in albumDict.keys():
albumDict.update({albumName: [artistName, int(tP), int(tC), uri]})
else:
albumDict[albumName][1] += int(tP)
albumDict[albumName][2] += int(tC)
createdSong = Song(trackName = songName, artist = artistName, albumName = albumName, time_played = tP, clicks = tC, song_uri=uri)
sessionUser.songLibrary.append(createdSong)
try:
dbSession.commit()
except:
dbSession.rollback()
dbSession.commit()
sortedAlbumList = dict(sorted(albumDict.items(), key=lambda item: item[1][1], reverse=True))
return json.dumps(sortedAlbumList, separators=(',', ':'))