def create_user_goal(): user_data = models.UserData.current() if not user_data: return api_invalid_param_response("User is not logged in.") user_override = request.request_user_data("email") if user_data.developer and user_override and user_override.key_email != user_data.key_email: user_data = user_override json = request.json title = json.get('title') if not title: return api_invalid_param_response('Title is invalid.') objective_descriptors = [] goal_exercises = GoalList.exercises_in_current_goals(user_data) goal_videos = GoalList.videos_in_current_goals(user_data) if json: for obj in json['objectives']: if obj['type'] == 'GoalObjectiveAnyExerciseProficiency': objective_descriptors.append(obj) if obj['type'] == 'GoalObjectiveAnyVideo': objective_descriptors.append(obj) if obj['type'] == 'GoalObjectiveExerciseProficiency': obj['exercise'] = models.Exercise.get_by_name(obj['internal_id']) if not obj['exercise'] or not obj['exercise'].is_visible_to_current_user(): return api_invalid_param_response("Internal error: Could not find exercise.") if user_data.is_proficient_at(obj['exercise'].name): return api_invalid_param_response("Exercise has already been completed.") if obj['exercise'].name in goal_exercises: return api_invalid_param_response("Exercise is already an objective in a current goal.") objective_descriptors.append(obj) if obj['type'] == 'GoalObjectiveWatchVideo': obj['video'] = models.Video.get_for_readable_id(obj['internal_id']) if not obj['video']: return api_invalid_param_response("Internal error: Could not find video.") user_video = models.UserVideo.get_for_video_and_user_data(obj['video'], user_data) if user_video and user_video.completed: return api_invalid_param_response("Video has already been watched.") if obj['video'].readable_id in goal_videos: return api_invalid_param_response("Video is already an objective in a current goal.") objective_descriptors.append(obj) if objective_descriptors: objectives = GoalObjective.from_descriptors(objective_descriptors, user_data) goal = Goal(parent=user_data, title=title, objectives=objectives) user_data.save_goal(goal) return goal.get_visible_data(None) else: return api_invalid_param_response("No objectives specified.")