Esempio n. 1
0
	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()