def get(self): self.template_path = os.path.join(os.path.dirname(__file__), 'workout_create_or_edit.html') workout_key_string = self.request.get('edit') exercises = Exercise.all() self.template_vars['exercises'] = exercises #this is set if we are editing a workout if workout_key_string: try: workout = Workout.get(db.Key(workout_key_string)) except db.BadKeyError: self.errors.append('The specified workout is invalid') self.dispatch() return #make sure they aren't trying to edit #someone elses workout if workout.user != self.user: raise "PossibleHackingException" workout_exercises = WorkoutExercise.gql('WHERE workout = :1', workout) self.template_vars['workout'] = workout self.template_vars['workout_exercises'] = workout_exercises self.dispatch()
def post(self): self.template_path = os.path.join(os.path.dirname(__file__), 'workout_create_or_edit.html') exercise_key_strings = self.request.get('exercise_keys', allow_multiple=True) workout_name = self.request.get('workout_name') tag_strings = filter(reps_util.emptyFilter, self.request.get('tags',allow_multiple=True)) workout_to_edit_key = self.request.get('edit_workout') workout = None workout_exercises_to_delete = [] exercises = [] if len(exercise_key_strings) == 0 \ or len(workout_name) < 4: raise "WorkoutCreateException" for exercise_key_string in exercise_key_strings: exercises.append(Exercise.get(db.Key(exercise_key_string))) if workout_to_edit_key: try: workout = Workout.get(db.Key(workout_to_edit_key)) if workout.user != self.user: raise db.BadKeyError except db.BadKeyError: raise "PossibleHackingException" try: existing_workout = Workout.gql('WHERE user = :1 AND name = :2', self.user, workout_name).fetch(1)[0] if (existing_workout and not workout) or (workout.key() != existing_workout.key()): self.errors.append('A workout already exists with the name <b>' + workout_util.workout_edit_link(existing_workout, existing_workout.name) + '</b>. Click the link to edit it.') self.get() return except IndexError: pass #it's ok to be here, means that the workout with the same name doesn't exist #generate our tag properties from the strings tags = [] for tag_string in tag_strings: tags.append(db.Category(tag_string)) if workout: #we are editing the existing workout workout.name = workout_name workout.tags = tags workout_exercises_to_delete = WorkoutExercise.gql('WHERE workout = :1', workout) else: #we are creating a new workout workout = Workout(user=self.user,name=workout_name,tags=tags) try: db.run_in_transaction(self.__save_workout, workout, exercises, workout_exercises_to_delete) except db.Rollback: self.errors.append('There was an error saving the workout') self.get() return self.messages.append('Your workout was successfully saved!') self.get()
def post(self): self.response.headers['Content-Type'] = 'plain/text' exercise_key_string = self.request.get('exercise_key') exercise = None html = '' try: exercise = Exercise.get(db.Key(exercise_key_string)) except db.BadKeyError: html = 'error' self.response.out.write(html) return html = "<tr><td>" + exercise.name + "</td><td><input type='text' /></td></tr>" self.response.out.write(html)
def get(self): exercise_key_str = self.request.get('exercise') try: exercise = Exercise.get(db.Key(exercise_key_str)) except db.BadKeyError: #Yes - we are raising the same exception we are catching #We want to error out, and we want to programmatically check #When we get around to implementing custom exceptions, #this will be replaced raise db.BadKeyError exercise_muscles = exercise_util.get_exercise_muscles(exercise) db.run_in_transaction(self.__delete_exercise, exercise, exercise_muscles) self.redirect(self.request.headers['Referer'])
def get(self): exercise_to_edit = self.request.get('edit') if exercise_to_edit: try: exercise = Exercise.get(db.Key(exercise_to_edit)) exercise_muscles = ExerciseMuscle.gql('WHERE exercise = :1', exercise) self.template_vars['exercise'] = exercise self.template_vars['exercise_muscles'] = exercise_muscles except db.BadKeyError: self.errors.append('The specified exercise could not be found.') muscleQuery = Muscle.all() self.template_vars['muscles'] = muscleQuery self.template_path = os.path.join(os.path.dirname(__file__), 'exercise_create_or_edit.html') self.dispatch()
def get(self): exercise_key_str = self.request.get('exercise') try: exercise = Exercise.get(db.Key(exercise_key_str)) except db.BadKeyError: #Yes - we are raising the same exception we are catching #We want to error out, and we want to programmatically check #When we get around to implementing custom exceptions, #this will be replaced raise db.BadKeyError exercise_muscles = exercise_util.get_exercise_muscles(exercise) self.template_vars['exercise'] = exercise self.template_vars['exercise_muscles'] = exercise_muscles self.template_path = os.path.join(os.path.dirname(__file__), 'exercise_view.html') self.dispatch()
def post(self): muscles = self.request.get('muscles', allow_multiple=True) muscle_roles = filter(reps_util.emptyFilter, self.request.get('roles', allow_multiple=True)) exercise_name = self.request.get('exercise_name') exercise_type_str = self.request.get('exercise_type') edit_exercise = self.request.get('edit_exercise') old_exercise_muscles = [] #data validation, we aren't going to give #feedback since validation with feedback is #done on the client. if len(muscles) == 0 or len(muscles) != len(muscle_roles) \ or len(exercise_name) < 3 \ or len(exercise_name) > 30 \ or len(exercise_type_str) == 0: raise "ExerciseValidationError" try: muscle_roles.index('Target') except ValueError: raise "ExerciseValidationError" #validation complete #wait until after validation to assign this. #it will throw it's own exception if you pass #a null value to the Category constructor exercise_type_cat = db.Category(exercise_type_str) if edit_exercise: #we are editing the exercise try: exercise = Exercise.get(db.Key(edit_exercise)) except db.BadKeyError: #Yes - we are raising the same exception we are catching #We want to error out, and we want to programmatically check #When we get around to implementing custom exceptions, #this will be replaced raise db.BadKeyError exercise.name = exercise_name exercise.type = exercise_type_cat #the following var used to store the ExerciseMuscle entities #related to the Exercise entity (if we are editing) #these are then passed to the save method so that #we can delete them safely within a transaction old_exercise_muscles = exercise_util.get_exercise_muscles(exercise) else: #check to see if an exercise already exists with this name #if not, create the new exercise try: existing_exercise = Exercise.gql('WHERE name = :1',exercise_name).fetch(1)[0] if existing_exercise: self.errors.append('An exercise already exists with the name ' + exercise_util.exercise_edit_link(existing_exercise) + '. Click the link to edit it.') self.get() return except IndexError: #this is what we want - it means the #exercise name doesn't exist in the datastore pass #create the new exercise exercise = Exercise(name=exercise_name,type=exercise_type_cat) #todo: this should be put in the transaction below #when the app engine bug preventing root entities #and their decendants from being created in the #same transaction exercise.put() #server side validation found_target = False new_exercise_muscles = [] for muscle_key, muscle_role in zip(muscles,muscle_roles): muscle = Muscle.get(db.Key(muscle_key)) if muscle_role == 'Target': if found_target == True: #uh oh, more than one target defined, crap out raise "ExerciseValidationError" #yeah, no fancy feedback. All that is done on the client. just die. else: found_target = True exercise.target_location = muscle.location exercise_muscle = ExerciseMuscle(exercise=exercise,muscle=muscle,role=muscle_role,parent=exercise) new_exercise_muscles.append(exercise_muscle) #if we didn't have a target muscle for the exercise #once again, crap out with no feedback if found_target == False: raise "ExerciseValidationError" #put the exercise again, so that we now save the target_location #super TODO - put this all in a transaction. exercise.put() #and now we're ready to commit the Exercise and ExerciseMuscle entities #it is all isolated within a transaction try: db.run_in_transaction(self.__save_exercise_muscles, new_exercise_muscles, old_exercise_muscles) self.messages.append('Exercise <b>' + exercise.name + '</b> (' + exercise_util.exercise_edit_link(exercise) + ') has been successfully saved.') except db.Rollback: self.errors.append('A database error occured. The exercise was not saved.') self.get()
def get(self): exercises = Exercise.all() self.template_vars['exercises'] = exercises self.template_path = os.path.join(os.path.dirname(__file__), 'exercises.html') self.dispatch()