Skip to content

Instantly share code, notes, and snippets.

@jweissbock
Created April 2, 2013 04:17
Show Gist options
  • Save jweissbock/5289905 to your computer and use it in GitHub Desktop.
Save jweissbock/5289905 to your computer and use it in GitHub Desktop.
1 function in my flask file
@app.route('/saveGame')
def saveGame():
n = 6# n = depending on how many items we are saving per puck
msg = "Not working yet."
# check if number - 2 % n = 0 to see if has right # parameters
if ((len(request.args) - 2) % n > 0):
json.dumps([{ 'success' : False, 'msg' : 'Invalid number of arguements.' }])
args = sorted(request.args)
#print args
# need to check if these are valid
gameID = request.args.get('gameID')
gameYear = request.args.get('gameYear')
numPucks = (len(args)-2) / n
pucks = [dict() for x in range(numPucks)]
# fill up the pucks
for i in range(numPucks):
# comment
comment = 'puck'+str(i)+'comment'
if comment not in request.args:
return json.dumps({'success': False, 'msg' : 'No puck comment for '+str(i)})
pucks[i]['comment'] = request.args.get(comment).strip()
# puck position
puckleft = 'puck'+str(i)+'left'
pucktop = 'puck'+str(i)+'top'
if pucktop not in request.args or puckleft not in request.args:
return json.dumps({'success' : False, 'msg' : 'Missing puck location data for '+str(i)})
elif not request.args.get(puckleft).isdigit():
return json.dumps({'success' : False, 'msg' : 'Puck left is not a digit for '+str(i)})
elif not request.args.get(pucktop).isdigit():
return json.dumps({'success' : False, 'msg' : 'Puck top is not a digit for '+str(i)})
pucks[i]['left'] = int(request.args.get(puckleft))
pucks[i]['top'] = int(request.args.get(pucktop))
# puck time
pucktime = 'puck'+str(i)+'time'
if pucktime not in request.args:
return json.dumps({'success' : False, 'msg' : 'Missing puck time data for '+str(i)})
elif not request.args.get(pucktime).isdigit():
return json.dumps({'success' : False, 'msg' : 'Puck time is not a digit for '+str(i)})
elif not float(request.args.get(pucktime)).is_integer():
return json.dumps({'success' : False, 'msg' : 'Puck time is not an integer for '+str(i)})
elif int(request.args.get(pucktime)) not in range(0,1201):
return json.dumps({'success' : False, 'msg' : 'Puck time is not an allowable range for '+str(i)})
pucks[i]['time'] = int(request.args.get(pucktime))
# puck period
puckperiod = 'puck'+str(i)+'period'
if puckperiod not in request.args:
return json.dumps({'success' : False, 'msg' : 'Missing puck period data for '+str(i)})
elif not request.args.get(puckperiod).isdigit():
return json.dumps({'success' : False, 'msg' : 'Puck period is not a digit for '+str(i)})
elif int(request.args.get(puckperiod)) not in range(0,4):
return json.dumps({'success' : False, 'msg' : 'Puck period is not an allowable range for '+str(i)})
pucks[i]['period'] = int(request.args.get(puckperiod))
#Puck team
puckteam = 'puck'+str(i)+'team'
if puckteam not in request.args:
return json.dumps({'success': False, 'msg' : 'No puck team for '+str(i)})
if request.args.get(puckteam) == 'Home':
pucks[i]['team'] = 0
else:
pucks[i]['team'] = 1
#print pucks
# begin transaction
# delete all events from game ID and save all new ones for now
#g.db.execute('BEGIN TRANSACTION')
try:
g.db.execute('DELETE FROM chances WHERE gameid = ?', [gameID])
g.db.commit()
for p in pucks:
g.db.execute('INSERT INTO chances (gameid, yearid, team, period, time, comment, posx, posy) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[gameID, gameYear, p['team'], p['period'], p['time'], p['comment'], p['left'], p['top']])
g.db.commit()
except:
return json.dumps({ 'success' : False, 'msg' : 'Error in saving to database'})
#g.db.execute('COMMIT')
# later, find difference between database and what submited, delete from DB insert new ones
# need to keep a log
msg = 'Saved!'
data = [{ 'success' : True, 'msg' : msg}]
return json.dumps(data)
@jweissbock
Copy link
Author

Valid data to pass to it: /saveGame?gameID=20505&gameYear=20122013&puck0team=Home&puck0period=1&puck0time=1200&puck0comment=&puck0left=415&puck0top=227&puck1team=Home&puck1period=1&puck1time=1200&puck1comment=&puck1left=412&puck1top=292&puck2team=Home&puck2period=1&puck2time=1200&puck2comment=&puck2left=375&puck2top=290

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment