def submit_workout(): exercises = request.data if(len(exercises) == 0): result = {"Result":"Failure: No JSON Data sent."} return jsonify(result) jsonExercises = json.loads(exercises) name = jsonExercises['name'] description = jsonExercises['description'] level = jsonExercises['level'] isPublic = jsonExercises['isPublic'] if isPublic == 'true': isPublic = True else: isPublic = False isLikeable = jsonExercises['isLikeable'] if isLikeable == 'true': isLikeable = True else: isLikeable = False isCommentable = jsonExercises['isCommentable'] if isCommentable == 'true': isCommentable = True else: isCommentable = False user = g.user if user.is_anonymous(): user = User.find_by_username("Guest") else: user.add_newsfeed("Has added a workout: " + name + "") if user is None: user = User.find_by_username("Guest") if user is None: user = User(username="******", password="******", email=None, firstname=None, lastname=None, location=None, sex=None, date_of_birth=None, avatar=None, about_me=None, last_seen=None) User.save_to_db(user) newWorkout = Workout(user_id=user.id, parent_user_id=user.id, name=name, level=level, is_public=isPublic, is_likeable=isLikeable, is_commentable=isCommentable, description=description) Workout.save_to_db(newWorkout) logging.warning("saving workout") for exercise in jsonExercises['exercises']: order = exercise['order'] type = exercise['type'] unit = exercise['unit'] exerciseTag = ExerciseTag.find_by_name(type) if exerciseTag is None: exerciseTag = ExerciseTag(type, unit) ExerciseTag.save_to_db(exerciseTag) amount = exercise['amount'] additionalInfo = exercise['additionalInfo'] newExercise = Exercise(newWorkout.id, exerciseTag.id, order, amount, additionalInfo) Exercise.save_to_db(newExercise) result = {"Result":"Success"} return jsonify(result)
def test_find_by_name(self): exercise3 = ExerciseTag(name = "Exercise 3", unit = "reps") exercise4 = ExerciseTag(name = "Exercise 4", unit = "reps") db.session.add(exercise3) db.session.add(exercise4) db.session.commit() foundEntry = ExerciseTag.find_by_name('Exercise 3') assert foundEntry.name == 'Exercise 3' foundEntry = ExerciseTag.find_by_name('Exercise 4') assert foundEntry.name == 'Exercise 4'
def test_find_by_id(self): exercise3 = ExerciseTag(name = "Exercise 3", unit = "reps") db.session.add(exercise3) db.session.commit() exerciseFound = ExerciseTag.find_by_id(exercise3.id) assert exerciseFound is not None
def get_exercise_tag_unit(): exerciseTagName = request.args.get('name', '') exercise = ExerciseTag.find_by_name(exerciseTagName) if exercise is None: result = {"unit":"minutes"} return jsonify(result) else: result = {"unit":exercise.unit} return jsonify(result)
def test_all(self): exercise1 = ExerciseTag(name = "Exercise 1", unit = "reps") exercise2 = ExerciseTag(name = "Exercise 2", unit = "reps") db.session.add(exercise1) db.session.add(exercise2) db.session.commit() foundEntries = ExerciseTag.all() assert foundEntries != None
def get_exercise_types(): exerciseTags = ExerciseTag.all() output = "" if len(exerciseTags) == 0: running = ExerciseTag(name = "Running", unit = "Minutes") walking = ExerciseTag(name = "Walking", unit = "Minutes") biking = ExerciseTag(name = "Biking", unit = "Minutes") bicepCurls = ExerciseTag(name = "Bicep Curls", unit = "Reps") ExerciseTag.save_to_db(running); ExerciseTag.save_to_db(walking); ExerciseTag.save_to_db(biking); ExerciseTag.save_to_db(bicepCurls); exerciseTags = ExerciseTag.all() for exercise_tag in exerciseTags: output += exercise_tag.name + "," return jsonify(names = output)
def test_save_to_db(self): exerciseTag1 = ExerciseTag("running", "minutes") exerciseTag2 = ExerciseTag("walking", "minutes") ExerciseTag.save_to_db(exerciseTag1); ExerciseTag.save_to_db(exerciseTag2); exerciseTagFound = ExerciseTag.find_by_name("running") assert exerciseTagFound != None exerciseTagFound = ExerciseTag.find_by_name("walking") assert exerciseTagFound != None
def to_json(self): exerciseTag = ExerciseTag.find_by_id(self.eTagId) ret = { "id": self.id, "workOutId": self.workOutId, "eTagName": exerciseTag.name, "eTagUnit": exerciseTag.unit, "order": self.order, "amount": self.amount, "additionalInfo": self.additionalInfo} return json.dumps(ret)
def edit_workout(): exercises = request.data if(len(exercises) == 0): result = {"Result":"Failure"} return jsonify(result) jsonExercises = json.loads(exercises) if jsonExercises == None: result = {"Result":"Failure"} return jsonify(result) else: if len(jsonExercises) == 0: result = {"Result":"Failure"} return jsonify(result) else: name = jsonExercises['name'] description = jsonExercises['description'] level = jsonExercises['level'] isPublic = jsonExercises['isPublic'] if isPublic == 'true': isPublic = True else: isPublic = False isLikeable = jsonExercises['isLikeable'] if isLikeable == 'true': isLikeable = True else: isLikeable = False isCommentable = jsonExercises['isCommentable'] if isCommentable == 'true': isCommentable = True else: isCommentable = False currentWorkout = Workout.find_single_workout_by_name_(name) currentWorkout.level = level currentWorkout.is_public = isPublic currentWorkout.is_likeable = isLikeable currentWorkout.is_commentable = isCommentable currentWorkout.description = description Workout.update_workout_info(currentWorkout) for exercise in jsonExercises['exercises']: order = exercise['order'] type = exercise['type'] unit = exercise['unit'] exerciseTag = ExerciseTag.find_by_name(type) if exerciseTag is None: exerciseTag = ExerciseTag(type, unit) ExerciseTag.save_to_db(exerciseTag) amount = exercise['amount'] additionalInfo = exercise['additionalInfo'] currentExercise = Exercise.find_by_workOutId_and_order(currentWorkout.id, order) if currentExercise is None: newExercise = Exercise(currentWorkout.id, exerciseTag.id, order, amount, additionalInfo) Exercise.save_to_db(newExercise) else: currentExercise.eTagId = exerciseTag.id currentExercise.amount = amount currentExercise.additionalInfo = additionalInfo Exercise.update_exercise_info(currentExercise) result = {"Result":"Success"} return jsonify(result)
def get_workouts(): """ { "Result":"OK", "Records":[ {"WorkoutId":1,"Name":"Blah"}, {"WorkoutId":2,"Name":"Lazy bones"}, ] } """ result = [] workout_ids = [] workouts = [] if 'workout' in request.form: workouts = Workout.find_by_name(request.form['workout']) if workouts is not None: for workout in workouts: result.append(workout.to_search_query_hash()) elif 'description' in request.form: workouts = Workout.find_by_description(request.form['description']) if workouts is not None: for workout in workouts: result.append(workout.to_search_query_hash()) elif 'tag_auto' in request.form: tags = ExerciseTag.find_all_by_name(request.form['tag_auto']) if tags is not None: for tag in tags: for exercise in tag.exercises: workout_ids.append(exercise.workOutId) #remove dupes workout_ids = set(workout_ids) for id in workout_ids: workouts.append(Workout.find_by_id(id)) for workout in workouts: result.append(workout.to_search_query_hash()) elif 'exercise' in request.form: tag = ExerciseTag.find_by_name(request.form['exercise']) if tag is not None: for exercise in tag.exercises: workout_ids.append(exercise.workOutId) #remove dupes workout_ids = set(workout_ids) for id in workout_ids: workouts.append(Workout.find_by_id(id)) for workout in workouts: result.append(workout.to_search_query_hash()) elif 'author' in request.form: authors = User.find_all_by_username(request.form['author']) if authors is not None: for author in authors: works = Workout.find_by_userid(author.id) if works is not None: for work in works: workouts.append(work) for workout in workouts: result.append(workout.to_search_query_hash()) w = {"Result":"OK", "Records": result} return jsonify(w)