def programStats(stat_id): """ GET: return the info for that particular stat POST: create a new stat for the particular program PUT: update a stat for the particular program DELETE: delete a stat for the particular program """ user = getUser() if user: if request.method == 'GET': the_stat = user.stats.find(Stat.id == stat_id).one() print the_stat return jsonify( program=the_stat.program.name, stat={ 'name': the_stat.name, 'id': the_stat.id, 'user_id': the_stat.user_id, 'value': the_stat.value } ) elif request.method == 'POST': # create a new stat for the program new_stat = store.add(Stat()) new_stat.name = request.json['name'] new_stat.user_id = user.id new_stat.program_id = request.json['program_id'] new_stat.value = unicode(request.json['value']) store.commit() stat_json = { 'program': new_stat.program.name, 'program_id': new_stat.program.id, 'id': new_stat.id, 'name':new_stat.name, 'user_id': new_stat.user_id, 'value': new_stat.value } return json.dumps(stat_json) elif request.method == 'PUT': # get the new data, find the particular stat and update it new_data = json.loads(request.data) stat = store.find(Stat, Stat.id == int(new_data['id'])).one() stat.value = new_data['value'] store.commit() return 'success' elif request.method == 'DELETE': # delete that entry from the database stat = store.find(Stat, Stat.id == stat_id).one() store.remove(stat) return 'success'
def getAllPrograms(program_id): """ GET: Return all programs for the current user POST: create a new program """ user = getUser() if user: if request.method == 'GET': program_list = [] for program in user.programs: program_list.append({ 'name': program.name, 'id': program.id }) return json.dumps(program_list) elif request.method == 'POST': # create the program and link it to the user new_program = store.add(Program(json.loads(request.data)['name'])) store.flush() link = store.add(ProgramUserLink(new_program.id, user.id)) store.commit() return jsonify(name=new_program.name, program_id=new_program.id) elif request.method == 'DELETE': # delete the program sad_program = store.find(Program, Program.id == program_id).one() store.remove(sad_program) return jsonify(status='program deleted') else: 'no user found, login'
def login(): """ GET: Render the login form POST: try to verify the user """ if request.method == 'POST': # Get the user information name = request.form['username'] passwd = request.form['password'] # Find a user with that username and compare passwords user = store.find(User, User.username == unicode(name)).one() salt = user.salt thehash = pbkdf2_hex(passwd.encode('utf-8'), salt.encode('utf-8')) # store user id in the session session['userid'] = user.id if thehash == user.hash: return redirect('/home') else: return 'login failed'
def getUser(): """ get User with user_id """ user = store.find(User, User.id == session['userid']).one() return user