Example #1
0
	def GET(self, userId = None):
		sts_db.requestDatabaseAccess()
		user = getUser(userId)
		if user is None:
			page = render.login(sts_db.getUsers())
		else:
			page = navigateTo('workout')
		sts_db.releaseDatabaseAccess()
		return page
Example #2
0
def getUser(id = None):
	if id is None:
		id = web.cookies().get(COOKIE_USERID)
	if(id is None or id == ''):
		return None
	sts_db.requestDatabaseAccess()
	user = sts_db.getUser(int(id))
	sts_db.releaseDatabaseAccess()
	if user is None:
		return None
	return user
Example #3
0
def getUserStatus(offset = 0):
	user = getUser()
	if user is None:
		return None, None, None
	sts_db.requestDatabaseAccess();
	plan = user.getCurrentPlan()
	sts_db.releaseDatabaseAccess();
	if plan is None:
		return user, None, None
	sts_db.requestDatabaseAccess()
	totalDays = plan.getTotalDays()
	planDay = user.getPlanDay(plan) + offset
	workout = plan.getWorkout(planDay)
	sts_db.releaseDatabaseAccess()
	return user, plan, workout
Example #4
0
	def GET(self, liftNumber = None, offset = None, setNumber = None):
		c = time.clock()
		try:
			if offset is None:
				offset = getWorkoutOffset()
			else:
				setWorkoutOffset(offset)
			
			if liftNumber is None:
				liftNumber = getLiftNumber()
			
			if setNumber is None:
				setNumber = getSetNumber()

			user, plan, workout = getUserStatus(offset)
			
			if user is None:
				logout()
			if plan is None or workout is None:
				return navigateTo('workout')

			sts_db.requestDatabaseAccess()

			liftCount = workout.getLiftCount()
			if liftCount == 0:
				sts_db.releaseDatabaseAccess()
				return navigateTo('workout')
			liftNumber = max(0, min(liftNumber, liftCount - 1))
			saveLiftNumber(liftNumber)
			lift = workout.getLift(liftNumber)

			finishedSets = user.getCompletedSets(lift, plan, workout)
			setNumber = max(0, min(setNumber, finishedSets))
			saveSetNumber(setNumber)

			if setNumber >= finishedSets:
				lastWeight = user.getLatestWeight(lift)
			else:
				lastWeight = user.getWeight(lift, plan, workout, setNumber)

			page = render.lift(workout, user, lift, liftNumber, liftCount, setNumber, finishedSets, lastWeight)

			sts_db.releaseDatabaseAccess()

			return page
		finally:
			if DEBUG:
				print int((time.clock() - c) * 1000), 'ms to GET page'
Example #5
0
	def GET(self, offset = None):
		c = time.clock()
		try:
			if offset is None:
				offset = getWorkoutOffset()
			else:
				setWorkoutOffset(offset)

			user, plan, workout = getUserStatus(offset)
			if user is None:
				logout()
			if plan is None:
				if DEBUG:
					print 'plan is none, restting to today'
				if offset != 0:
					return self.GET(0)
				return logout()
			lifts = []
			workoutDay = 0

			sts_db.requestDatabaseAccess()

			if workout is not None:
				lifts = workout.getLifts()

				#get workoutDay (0-41 for STS)
				workoutDay = plan.getDay(workout)
			lastWeights = {}
			sets = {}
			suggestedReps = {}
			suggestedSets = {}
			for l in lifts:
				lastWeights[l.id] = user.getLatestWeight(l)
				sets[l.id] = user.getCompletedSets(l, plan, workout)
				reps = workout.getRepsOf(l)
				if DEBUG:
					print reps[0], '-', reps[1]
				suggestedReps[l.id] = workout.getRepsOf(l)
				suggestedSets[l.id] = workout.getSetsOf(l)

			page = render.workout(workout, offset, user, lifts, lastWeights, sets, workoutDay, suggestedSets, suggestedReps)
			sts_db.releaseDatabaseAccess()
			return page
		finally:
			if DEBUG:
				print int((time.clock() - c) * 1000), 'ms to GET page'
Example #6
0
	def POST(self):
		userId = None
		if 'userID' in web.input():
			userId = web.input()['userID']
			web.setcookie(COOKIE_USERID, userId, 24*7*60*60)
		elif 'newUser' in web.input() and 'newUserName' in web.input():
			username = web.input()['newUserName']
			if username != '':
				sts_db.requestDatabaseAccess()

				u = sts_db.newUser()
				u.addPlan(sts_db.getPlanByName('STS'))
				u.setName(username)

				sts_db.releaseDatabaseAccess()

				userId = u.id
		return self.GET(userId)
Example #7
0
	def deleteSet(self):
		#get the workout
		user, plan, workout = getUserStatus(getWorkoutOffset())
		
		sts_db.requestDatabaseAccess()

		#get the lift
		liftNumber = min(getLiftNumber(), workout.getLiftCount()) - 1
		lift = workout.getLift(liftNumber)

		#get the set
		setNumber = min(getSetNumber(), user.getCompletedSets(lift, plan, workout))

		#remove the record
		user.removeLiftRecord(lift, plan, workout, setNumber)

		sts_db.releaseDatabaseAccess()

		#reload
		return self.GET()
Example #8
0
	def done(self):
		#get the weight and reps
		weight = 0
		if 'weight' in web.input():
			try:
				weight = float(web.input()['weight'])
			except:
				weight = 0
		reps = 0
		if 'reps' in web.input():
			try:
				reps = int(web.input()['reps'])
			except:
				reps = 0
		
		#get the workout
		user, plan, workout = getUserStatus(getWorkoutOffset())
		
		if user is None:
			logout()
		if plan is None or workout is None:
			return navigateTo('workout')

		sts_db.requestDatabaseAccess()
		
		#get the lift
		liftNumber = min(getLiftNumber(), workout.getLiftCount())
		lift = workout.getLift(liftNumber)

		#get the set
		finishedSets = user.getCompletedSets(lift, plan, workout)
		setNumber = max(0, min(getSetNumber(), finishedSets))
		saveSetNumber(setNumber)

		#if we're on a set that hasn't been added yet, add it
		if setNumber >= finishedSets:
			#add the weight to the lift
			user.addSet(lift, plan, workout, weight, reps)

			#increment the set number
			setNumber += 1

			#if we've finished the number of sets, advance to the next lift
			if setNumber == workout.getSetsOf(lift):
				#reset the set number
				saveSetNumber(0)

				#if we finished the last lift, back out to the workout
				if liftNumber >= workout.getLiftCount() - 1:
					sts_db.releaseDatabaseAccess()
					return self.back()

				#otherwise, just go to the next lift
				sts_db.releaseDatabaseAccess()
				return self.next()
		else:
			#otherwise, save any changes made to this
			user.saveSet(lift, plan, workout, weight, reps, setNumber)

		sts_db.releaseDatabaseAccess()
		#just refresh the page
		return self.GET(liftNumber = liftNumber, setNumber = setNumber)