def test_user_exercise_model(self): """Test adding exercises to a user's exercises functionality.""" # Create test exercises e1 = Exercise(name="Bench Press", description="Chest exercise", category_id=10) e1.id = 500 e2 = Exercise(name="Triceps Pushdown", description="Triceps exercise", category_id=11) e2.id = 501 # Create test user u = User.register("testing1", "password", "*****@*****.**", "John", "Doe", None) u.id = 777 db.session.add_all([e1, e2]) ue1 = UserExercise(user_id=u.id, exercise_id=e1.id) ue2 = UserExercise(user_id=u.id, exercise_id=e2.id) db.session.add_all([ue1, ue2]) db.session.commit() exc = UserExercise.query.filter_by(user_id=u.id) self.assertEqual(exc.count(), 2) self.assertEqual(exc[0].exercise_id, e1.id) self.assertEqual(exc[1].exercise_id, e2.id)
def exercise_progress_graph_context(user_data_student): if not user_data_student: return {} user = user_data_student.user exercise_data = {} exercises = Exercise.get_all_use_cache() user_exercises = UserExercise.get_for_user_use_cache(user) dict_user_exercises = {} for user_exercise in user_exercises: dict_user_exercises[user_exercise.exercise] = user_exercise for exercise in exercises: chart_link ="" status = "" color = "transparent" exercise_display = Exercise.to_display_name(exercise.name) ex_link = "/exercises?exid="+exercise.name hover = "<b>%s</b><br/><em><nobr>Status: %s</nobr></em><br/><em>Streak: %s</em><br/><em>Problems attempted: %s</em>" % ( exercise_display, "Not Started", 0, 0) chart_link = "/profile/graph/exerciseproblems?student_email=%s&exercise_name=%s" % (user.email(), exercise.name) user_exercise = dict_user_exercises[exercise.name] if dict_user_exercises.has_key(exercise.name) else None if user_data_student.is_proficient_at(exercise.name): status = "Proficient" color = "proficient" if not user_data_student.is_explicitly_proficient_at(exercise.name): status = "Proficient (due to proficiency in a more advanced module)" elif user_exercise is not None and UserExercise.is_struggling_with(user_exercise, exercise): status = "Struggling" color = "struggling" elif user_exercise is not None and user_exercise.total_done > 0: status = "Started" color = "started" if len(status) > 0: hover = "<b>%s</b><br/><em><nobr>Status: %s</nobr></em><br/><em>Streak: %s</em><br/><em>Problems attempted: %s</em>" % (exercise_display, status, user_exercise.streak if user_exercise is not None else 0, user_exercise.total_done if user_exercise is not None else 0) exercise_data[exercise.name] = { "short_name": exercise.short_name(), "chart_link": chart_link, "ex_link": ex_link, "hover": hover, "color": color } return { 'exercises': exercises, 'exercise_data': exercise_data, }
def exercise_progress_graph_context(user_data_student): if not user_data_student: return {} exercise_data = {} exercises = Exercise.get_all_use_cache() user_exercise_graph = UserExerciseGraph.get(user_data_student) review_exercise_names = user_exercise_graph.review_exercise_names() for exercise in exercises: chart_link ="" status = "" color = "transparent" exercise_display = Exercise.to_display_name(exercise.name) hover = "<b>%s</b><br/><em><nobr>Status: %s</nobr></em><br/><em>Progress: %s</em><br/><em>Problems attempted: %s</em>" % ( exercise_display, "Not Started", '0%', 0) chart_link = "/profile/graph/exerciseproblems?student_email=%s&exercise_name=%s" % (user_data_student.email, exercise.name) graph_dict = user_exercise_graph.graph_dict(exercise.name) if graph_dict["proficient"]: if exercise.name in review_exercise_names: status = "Review" color = "review light" else: status = "Proficient" color = "proficient" if not graph_dict["explicitly_proficient"]: status = "Proficient (due to proficiency in a more advanced module)" elif graph_dict["struggling"]: status = "Struggling" color = "struggling" elif graph_dict["total_done"] > 0: status = "Started" color = "started" if len(status) > 0: hover = "<b>%s</b><br/><em><nobr>Status: %s</nobr></em><br/><em>Progress: %s</em><br/><em>Problems attempted: %s</em>" % (exercise_display, status, UserExercise.to_progress_display(graph_dict["progress"]), graph_dict["total_done"]) exercise_data[exercise.name] = { "short_name": exercise.short_name(), "chart_link": chart_link, "ex_link": exercise.relative_url, "hover": hover, "color": color } return { 'exercises': exercises, 'exercise_data': exercise_data, }
def favorite_exercise(username): """Handle favoriting an exercise.""" # Make sure the logged in user is the authorized user. if "username" not in session or username != session['username']: raise Unauthorized() user = User.query.filter_by(username=username).first() # Get the exercise id from the form. exercise_id = request.form['data'] # Create a UserExercise instance user_exercise = UserExercise(user_id=user.id, exercise_id=exercise_id) db.session.add(user_exercise) db.session.commit() return redirect(request.referrer)
def get(self): from exercises import attempt_problem login_user = UserData.current() exercises_list = [exercise for exercise in Exercise.all()] videos_list = [video for video in Video.all()] user_count = self.request_int('users', 5) for user_id in xrange(0, user_count): # Create a new user first_name = random.choice(CreateRandomGoalData.first_names) last_name = random.choice(CreateRandomGoalData.last_names) nickname = "%s %s" % (first_name, last_name) email = 'test_%i@automatedrandomdata' % user_id user = users.User(email) logging.info("Creating user %s: (%i/%i)" % (nickname, user_id + 1, user_count)) user_data = UserData.get_or_insert( key_name="test_user_%i" % user_id, user=user, current_user=user, user_id=str(user_id), moderator=False, last_login=datetime.now(), proficient_exercises=[], suggested_exercises=[], need_to_reassess=True, points=0, coaches=[login_user.user_email], user_email=email, user_nickname=nickname, ) user_data.put() # Delete user exercise & video progress query = UserExercise.all() query.filter('user = '******'user = '******'type': 'GoalObjectiveExerciseProficiency', 'exercise': random.choice(exercises_list)}) for objective in xrange(1, random.randint(2, 4)): obj_descriptors.append({ 'type': 'GoalObjectiveWatchVideo', 'video': random.choice(videos_list)}) title = first_name + "'s Goal #" + str(goal_idx) logging.info("Creating goal " + title) objectives = GoalObjective.from_descriptors(obj_descriptors, user_data) goal = Goal(parent=user_data, title=title, objectives=objectives) user_data.save_goal(goal) for objective in obj_descriptors: if objective['type'] == 'GoalObjectiveExerciseProficiency': user_exercise = user_data.get_or_insert_exercise( objective['exercise']) chooser = random.randint(1, 120) if chooser >= 60: continue elif chooser > 15: count = 1 hints = 0 elif chooser < 7: count = 20 hints = 0 else: count = 25 hints = 1 logging.info( "Starting exercise: %s (%i problems, %i hints)" % (objective['exercise'].name, count, hints * count)) for i in xrange(1, count): attempt_problem(user_data, user_exercise, i, 1, 'TEST', 'TEST', 'TEST', True, hints, 0, False, "TEST", 'TEST', '0.0.0.0') elif objective['type'] == 'GoalObjectiveWatchVideo': seconds = random.randint(1, 1200) logging.info("Watching %i seconds of video %s" % (seconds, objective['video'].title)) VideoLog.add_entry(user_data, objective['video'], seconds, 0, detect_cheat=False) self.response.out.write('OK')
def get(self): user = util.get_current_user() student = user student_email = self.request.get('student_email') if user: pass else: student = users.User(email=student_email) user = student if student_email and student_email != user.email(): #logging.info("user is a coach trying to look at data for student") student = users.User(email=student_email) user_data = UserData.get_or_insert_for(student) else: #logging.info("user is a student looking at their own data") user_data = UserData.get_or_insert_for(user) user_data_dict = { 'email': user_data.user.email(), 'moderator': user_data.moderator, 'joined': self.datetime_to_str(user_data.joined), 'last_login': self.datetime_to_str(user_data.last_login), 'proficient_exercises': user_data.proficient_exercises, 'all_proficient_exercises': user_data.all_proficient_exercises, 'suggested_exercises': user_data.suggested_exercises, 'assigned_exercises': user_data.assigned_exercises, 'need_to_reassess': user_data.need_to_reassess, 'points': user_data.points, 'coaches': user_data.coaches, } user_exercises = [] for ue in UserExercise.all().filter('user ='******'exercise': ue.exercise, 'streak': ue.streak, 'longest_streak': ue.longest_streak, 'first_done': self.datetime_to_str(ue.first_done), 'last_done': self.datetime_to_str(ue.last_done), 'total_done': ue.total_done, 'last_review': self.datetime_to_str(ue.last_review), 'review_interval_secs': ue.review_interval_secs, 'proficient_date': self.datetime_to_str(ue.proficient_date), } user_exercises.append(ue_dict) user_videos = [] for uv in UserVideo.all().filter('user ='******'video': uv.video.youtube_id, 'seconds_watched': uv.seconds_watched, 'last_second_watched': uv.last_second_watched, 'last_watched': self.datetime_to_str(uv.last_watched), } user_videos.append(uv_dict) problems = [] for problem in ProblemLog.all().filter('user ='******'exercise': problem.exercise, 'correct': problem.correct, 'time_done': self.datetime_to_str(problem.time_done), 'time_taken': problem.time_taken, 'problem_number': problem.problem_number, 'hint_used': problem.hint_used } problems.append(problem_dict) export_dict = {'UserData': user_data_dict, 'UserExercise': user_exercises, 'ProblemLog': problems, 'UserVideo': user_videos} self.response.out.write(json.dumps(export_dict, indent=4))
def post(self): user = util.get_current_user() student = user if not user: self.response.out.write("please login first") elif App.is_dev_server: student_email = self.request.get('student_email') if student_email and student_email != user.email(): #logging.info("user is a coach trying to look at data for student") student = users.User(email=student_email) user_data = UserData.get_or_insert_for(student) if users.is_current_user_admin(): pass # allow admin to import to anyone's account elif user.email() not in user_data.coaches and user.email().lower() not in user_data.coaches: raise Exception('Student '+ student_email + ' does not have you as their coach') else: #logging.info("user is a student looking at their own data") user_data = UserData.get_or_insert_for(user) file_contents = self.request.POST.get('userdata').file.read() import_dict = json.loads(file_contents) user_data_dict = import_dict['UserData'] user_exercises = import_dict['UserExercise'] problems_unsorted = import_dict['ProblemLog'] user_videos = import_dict['UserVideo'] user_data.moderator = user_data_dict['moderator'] user_data.joined = self.datetime_from_str(user_data_dict['joined']) user_data.last_login = self.datetime_from_str(user_data_dict['last_login']) user_data.proficient_exercises = user_data_dict['proficient_exercises'] user_data.all_proficient_exercises = user_data_dict['all_proficient_exercises'] user_data.suggested_exercises = user_data_dict['suggested_exercises'] user_data.assigned_exercises = user_data_dict['assigned_exercises'] user_data.need_to_reassess = user_data_dict['need_to_reassess'] user_data.points = user_data_dict['points'] user_data.coaches = user_data_dict['coaches'] user_data.put() proficient_dates = {} correct_in_a_row = {} for problem in ProblemLog.all().filter('user ='******'time_done'])) for problem in problems: problem_log = ProblemLog() problem_log.user = student problem_log.exercise = problem['exercise'] problem_log.time_done = self.datetime_from_str(problem['time_done']) problem_log.correct = problem['correct'] if problem_log.correct: if problem_log.exercise not in correct_in_a_row: correct_in_a_row[problem_log.exercise] = 1 else: correct_in_a_row[problem_log.exercise] += 1 if not problem_log.exercise in proficient_dates and correct_in_a_row[problem_log.exercise] == 10: proficient_dates[problem_log.exercise] = problem_log.time_done #for coach in user_data.coaches: # class_data = coaches.Class.get_or_insert(coach, coach=coach) # class_data.compute_stats(user_data, problem_log.time_done.date()) problem_log.time_taken = problem['time_taken'] if problem.has_key('problem_number'): problem_log.problem_number = problem['problem_number'] if problem.has_key('hint_used'): problem_log.hint_used = problem['hint_used'] problem_log.put() for user_exercise in UserExercise.all().filter('user ='******'exercise'] user_exercise.parent = user_data user_exercise.user = student user_exercise.exercise = ue['exercise'] user_exercise.streak = ue['streak'] user_exercise.longest_streak = ue['longest_streak'] user_exercise.first_done = self.datetime_from_str(ue['first_done']) user_exercise.last_done = self.datetime_from_str(ue['last_done']) user_exercise.total_done = ue['total_done'] last_review = self.datetime_from_str(ue['last_review']) if last_review: user_exercise.last_review = last_review user_exercise.review_interval_secs = ue['review_interval_secs'] if user_exercise.exercise in proficient_dates: user_exercise.proficient_date = proficient_dates[user_exercise.exercise] else: user_exercise.proficient_date = self.datetime_from_str(ue['proficient_date']) user_exercise.put() for user_video in UserVideo.all().filter('user ='******'/individualreport?student_email='+student.email()) else: self.response.out.write("import is not supported on the live site")
def get(self): user = util.get_current_user() student = user if user: student_email = self.request.get('student_email') if student_email and student_email != user.email(): #logging.info("user is a coach trying to look at data for student") student = users.User(email=student_email) user_data = UserData.get_or_insert_for(student) if users.is_current_user_admin(): pass # allow admin to export anyone's data elif user.email() not in user_data.coaches and user.email( ).lower() not in user_data.coaches: raise Exception('Student ' + student_email + ' does not have you as their coach') else: #logging.info("user is a student looking at their own data") user_data = UserData.get_or_insert_for(user) user_data_dict = { 'email': user_data.user.email(), 'moderator': user_data.moderator, 'joined': self.datetime_to_str(user_data.joined), 'last_login': self.datetime_to_str(user_data.last_login), 'proficient_exercises': user_data.proficient_exercises, 'all_proficient_exercises': user_data.all_proficient_exercises, 'suggested_exercises': user_data.suggested_exercises, 'assigned_exercises': user_data.assigned_exercises, 'need_to_reassess': user_data.need_to_reassess, 'points': user_data.points, 'coaches': user_data.coaches, } user_exercises = [] for ue in UserExercise.all().filter('user ='******'exercise': ue.exercise, 'streak': ue.streak, 'longest_streak': ue.longest_streak, 'first_done': self.datetime_to_str(ue.first_done), 'last_done': self.datetime_to_str(ue.last_done), 'total_done': ue.total_done, 'last_review': self.datetime_to_str(ue.last_review), 'review_interval_secs': ue.review_interval_secs, 'proficient_date': self.datetime_to_str(ue.proficient_date), } user_exercises.append(ue_dict) user_videos = [] for uv in UserVideo.all().filter('user ='******'video': uv.video.youtube_id, 'seconds_watched': uv.seconds_watched, 'last_second_watched': uv.last_second_watched, 'last_watched': self.datetime_to_str(uv.last_watched), } user_videos.append(uv_dict) problems = [] for problem in ProblemLog.all().filter('user ='******'exercise': problem.exercise, 'correct': problem.correct, 'time_done': self.datetime_to_str(problem.time_done), 'time_taken': problem.time_taken, 'problem_number': problem.problem_number, 'hint_used': problem.hint_used } problems.append(problem_dict) export_dict = { 'UserData': user_data_dict, 'UserExercise': user_exercises, 'ProblemLog': problems, 'UserVideo': user_videos } self.response.out.write(json.dumps(export_dict, indent=4)) else: self.redirect(util.create_login_url(self.request.uri))
def post(self): user = util.get_current_user() student = user if not user: self.response.out.write("please login first") elif App.is_dev_server: student_email = self.request.get('student_email') if student_email and student_email != user.email(): #logging.info("user is a coach trying to look at data for student") student = users.User(email=student_email) user_data = UserData.get_or_insert_for(student) if users.is_current_user_admin(): pass # allow admin to import to anyone's account elif user.email() not in user_data.coaches and user.email( ).lower() not in user_data.coaches: raise Exception('Student ' + student_email + ' does not have you as their coach') else: #logging.info("user is a student looking at their own data") user_data = UserData.get_or_insert_for(user) file_contents = self.request.POST.get('userdata').file.read() import_dict = json.loads(file_contents) user_data_dict = import_dict['UserData'] user_exercises = import_dict['UserExercise'] problems_unsorted = import_dict['ProblemLog'] user_videos = import_dict['UserVideo'] user_data.moderator = user_data_dict['moderator'] user_data.joined = self.datetime_from_str(user_data_dict['joined']) user_data.last_login = self.datetime_from_str( user_data_dict['last_login']) user_data.proficient_exercises = user_data_dict[ 'proficient_exercises'] user_data.all_proficient_exercises = user_data_dict[ 'all_proficient_exercises'] user_data.suggested_exercises = user_data_dict[ 'suggested_exercises'] user_data.assigned_exercises = user_data_dict['assigned_exercises'] user_data.need_to_reassess = user_data_dict['need_to_reassess'] user_data.points = user_data_dict['points'] user_data.coaches = user_data_dict['coaches'] user_data.put() proficient_dates = {} correct_in_a_row = {} for problem in ProblemLog.all().filter('user ='******'time_done'])) for problem in problems: problem_log = ProblemLog() problem_log.user = student problem_log.exercise = problem['exercise'] problem_log.time_done = self.datetime_from_str( problem['time_done']) problem_log.correct = problem['correct'] if problem_log.correct: if problem_log.exercise not in correct_in_a_row: correct_in_a_row[problem_log.exercise] = 1 else: correct_in_a_row[problem_log.exercise] += 1 if not problem_log.exercise in proficient_dates and correct_in_a_row[ problem_log.exercise] == 10: proficient_dates[ problem_log.exercise] = problem_log.time_done #for coach in user_data.coaches: # class_data = coaches.Class.get_or_insert(coach, coach=coach) # class_data.compute_stats(user_data, problem_log.time_done.date()) problem_log.time_taken = problem['time_taken'] if problem.has_key('problem_number'): problem_log.problem_number = problem['problem_number'] if problem.has_key('hint_used'): problem_log.hint_used = problem['hint_used'] problem_log.put() for user_exercise in UserExercise.all().filter('user ='******'exercise'] user_exercise.parent = user_data user_exercise.user = student user_exercise.exercise = ue['exercise'] user_exercise.streak = ue['streak'] user_exercise.longest_streak = ue['longest_streak'] user_exercise.first_done = self.datetime_from_str( ue['first_done']) user_exercise.last_done = self.datetime_from_str( ue['last_done']) user_exercise.total_done = ue['total_done'] last_review = self.datetime_from_str(ue['last_review']) if last_review: user_exercise.last_review = last_review user_exercise.review_interval_secs = ue['review_interval_secs'] if user_exercise.exercise in proficient_dates: user_exercise.proficient_date = proficient_dates[ user_exercise.exercise] else: user_exercise.proficient_date = self.datetime_from_str( ue['proficient_date']) user_exercise.put() for user_video in UserVideo.all().filter('user ='******'/individualreport?student_email=' + student.email()) else: self.response.out.write("import is not supported on the live site")
def get(self): from exercises import attempt_problem login_user = UserData.current() exercises_list = [exercise for exercise in Exercise.all()] videos_list = [video for video in Video.all()] user_count = self.request_int('users', 5) for user_id in xrange(0, user_count): # Create a new user first_name = random.choice(CreateRandomGoalData.first_names) last_name = random.choice(CreateRandomGoalData.last_names) nickname = "%s %s" % (first_name, last_name) email = 'test_%i@automatedrandomdata' % user_id user = users.User(email) logging.info("Creating user %s: (%i/%i)" % (nickname, user_id + 1, user_count)) user_data = UserData.get_or_insert( key_name="test_user_%i" % user_id, user=user, current_user=user, user_id=str(user_id), moderator=False, last_login=datetime.now(), proficient_exercises=[], suggested_exercises=[], need_to_reassess=True, points=0, coaches=[login_user.user_email], user_email=email, user_nickname=nickname, ) user_data.put() # Delete user exercise & video progress query = UserExercise.all() query.filter('user = '******'user = '******'type': 'GoalObjectiveExerciseProficiency', 'exercise': random.choice(exercises_list)}) for objective in xrange(1, random.randint(2, 4)): obj_descriptors.append({ 'type': 'GoalObjectiveWatchVideo', 'video': random.choice(videos_list)}) title = first_name + "'s Goal #" + str(goal_idx) logging.info("Creating goal " + title) objectives = GoalObjective.from_descriptors(obj_descriptors, user_data) goal = Goal(parent=user_data, title=title, objectives=objectives) user_data.save_goal(goal) for objective in obj_descriptors: if objective['type'] == 'GoalObjectiveExerciseProficiency': user_exercise = user_data.get_or_insert_exercise( objective['exercise']) chooser = random.randint(1, 120) if chooser >= 60: continue elif chooser > 15: count = 1 hints = 0 elif chooser < 7: count = 20 hints = 0 else: count = 25 hints = 1 logging.info( "Starting exercise: %s (%i problems, %i hints)" % (objective['exercise'].name, count, hints * count)) for i in xrange(1, count): attempt_problem(user_data, user_exercise, i, 1, 'TEST', 'TEST', 'TEST', True, hints, 0, "TEST", 'TEST', '0.0.0.0') elif objective['type'] == 'GoalObjectiveWatchVideo': seconds = random.randint(1, 1200) logging.info("Watching %i seconds of video %s" % (seconds, objective['video'].title)) VideoLog.add_entry(user_data, objective['video'], seconds, 0, detect_cheat=False) self.response.out.write('OK')
def exercise_progress_graph_context(user_data_student): if not user_data_student: return {} exercise_data = {} exercises = Exercise.get_all_use_cache() user_exercise_graph = UserExerciseGraph.get(user_data_student) review_exercise_names = user_exercise_graph.review_exercise_names() for exercise in exercises: chart_link = "" status = "" color = "transparent" exercise_display = Exercise.to_display_name(exercise.name) hover = "<b>%s</b><br/><em><nobr>Status: %s</nobr></em><br/><em>Progress: %s</em><br/><em>Problems attempted: %s</em>" % ( exercise_display, "Not Started", '0%', 0) chart_link = "/profile/graph/exerciseproblems?student_email=%s&exercise_name=%s" % ( user_data_student.email, exercise.name) graph_dict = user_exercise_graph.graph_dict(exercise.name) if graph_dict["proficient"]: if exercise.name in review_exercise_names: status = "Review" color = "review light" else: status = "Proficient" color = "proficient" if not graph_dict["explicitly_proficient"]: status = "Proficient (due to proficiency in a more advanced module)" elif graph_dict["struggling"]: status = "Struggling" color = "struggling" elif graph_dict["total_done"] > 0: status = "Started" color = "started" if len(status) > 0: hover = "<b>%s</b><br/><em><nobr>Status: %s</nobr></em><br/><em>Progress: %s</em><br/><em>Problems attempted: %s</em>" % ( exercise_display, status, UserExercise.to_progress_display( graph_dict["progress"]), graph_dict["total_done"]) exercise_data[exercise.name] = { "short_name": exercise.short_name(), "chart_link": chart_link, "ex_link": exercise.relative_url, "hover": hover, "color": color } return { 'exercises': exercises, 'exercise_data': exercise_data, }
def get(self): if not hasattr(secrets, 'ka_api_consumer_key') or \ not hasattr(secrets, 'ka_api_consumer_secret') or \ not hasattr(secrets_dev, 'ka_api_token_key') or \ not hasattr(secrets_dev, 'ka_api_token_secret'): return self.redirect("/") self.setup_oauth() self.email = self.request_string("email") if not self.email: raise "Must supply email for user to import" params = copy.copy(self._default_kinds) params.update(self.request.params) # get proper user from addition 1 userexercise user_id_json = json.loads( self.api("/api/v1/user/exercises/addition_1")) user = users.User(user_id_json['user']) # UserData user_data_json_raw = self.api("/api/v1/user") user_data = UserData.from_json(json.loads(user_data_json_raw), user=user) self.output('user_data', user_data, user_data_json_raw) user_data.put() if 'UserVideo' in params: user_videos_json = json.loads(self.api("/api/v1/user/videos")) user_videos = [] for user_video_json in user_videos_json[:params['UserVideo']]: user_video = UserVideo.from_json(user_video_json, user_data=user_data) user_videos.append(user_video) self.output('user_video', user_video, jsonify(user_video_json)) video_logs = defaultdict(list) if 'VideoLog' in params: for user_video in user_videos: ytid = user_video.video.youtube_id video_logs_json = json.loads( self.api("/api/v1/user/videos/%s/log" % ytid)) for video_log_json in video_logs_json[:params[ 'ProblemLog']]: video_log = VideoLog.from_json(video_log_json, user_video.video, user) video_logs[user_video].append(video_log) self.output("video_log", video_log, jsonify(video_log_json)) # delete old video logs query = VideoLog.all(keys_only=True) query.filter('user ='******'UserExercise' in params: user_exercises_json = json.loads( self.api("/api/v1/user/exercises")) user_exercises = [] for user_exercise_json in user_exercises_json[:params[ 'UserExercise']]: user_exercise = UserExercise.from_json(user_exercise_json, user_data) if user_exercise: user_exercises.append(user_exercise) self.output("user_exercise", user_exercise, jsonify(user_exercise_json)) problem_logs = defaultdict(list) if 'ProblemLog' in params: for user_exercise in user_exercises: problem_logs_json = json.loads( self.api("/api/v1/user/exercises/%s/log" % user_exercise.exercise)) for problem_log_json in problem_logs_json[:params[ 'ProblemLog']]: problem_log = ProblemLog.from_json( problem_log_json, user_data=user_data, exercise=user_exercise.exercise_model) problem_logs[user_exercise].append(problem_log) self.output("problem_log", problem_log, jsonify(problem_log_json)) db.put(user_exercises) for k, v in problem_logs.iteritems(): db.put(v) if 'Goal' in params: with AutoNowDisabled(Goal): goals_json = json.loads(self.api("/api/v1/user/goals")) goals = [] for goal_json in goals_json[:params['Goal']]: goal = Goal.from_json(goal_json, user_data=user_data) goals.append(goal) self.output("goal", goal, jsonify(goal_json)) db.put(goals) # need to tell the userdata that it has goals user_data.has_current_goals = not all( [g.completed for g in goals]) user_data.put()
def get(self): if not hasattr(secrets, 'ka_api_consumer_key') or \ not hasattr(secrets, 'ka_api_consumer_secret') or \ not hasattr(secrets_dev, 'ka_api_token_key') or \ not hasattr(secrets_dev, 'ka_api_token_secret'): return self.redirect("/") self.setup_oauth() self.email = self.request_string("email") if not self.email: raise "Must supply email for user to import" params = copy.copy(self._default_kinds) params.update(self.request.params) # get proper user from addition 1 userexercise user_id_json = json.loads(self.api("/api/v1/user/exercises/addition_1")) user = users.User(user_id_json['user']) # UserData user_data_json_raw = self.api("/api/v1/user") user_data = UserData.from_json(json.loads(user_data_json_raw), user=user) self.output('user_data', user_data, user_data_json_raw) user_data.put() if 'UserVideo' in params: user_videos_json = json.loads(self.api("/api/v1/user/videos")) user_videos = [] for user_video_json in user_videos_json[:params['UserVideo']]: user_video = UserVideo.from_json(user_video_json, user_data=user_data) user_videos.append(user_video) self.output('user_video', user_video, jsonify(user_video_json)) video_logs = defaultdict(list) if 'VideoLog' in params: for user_video in user_videos: ytid = user_video.video.youtube_id video_logs_json = json.loads( self.api("/api/v1/user/videos/%s/log" % ytid)) for video_log_json in video_logs_json[:params['ProblemLog']]: video_log = VideoLog.from_json(video_log_json, user_video.video, user) video_logs[user_video].append(video_log) self.output("video_log", video_log, jsonify(video_log_json)) # delete old video logs query = VideoLog.all(keys_only=True) query.filter('user ='******'UserExercise' in params: user_exercises_json = json.loads(self.api("/api/v1/user/exercises")) user_exercises = [] for user_exercise_json in user_exercises_json[:params['UserExercise']]: user_exercise = UserExercise.from_json(user_exercise_json, user_data) if user_exercise: user_exercises.append(user_exercise) self.output("user_exercise", user_exercise, jsonify(user_exercise_json)) problem_logs = defaultdict(list) if 'ProblemLog' in params: for user_exercise in user_exercises: problem_logs_json = json.loads(self.api( "/api/v1/user/exercises/%s/log" % user_exercise.exercise)) for problem_log_json in problem_logs_json[:params['ProblemLog']]: problem_log = ProblemLog.from_json(problem_log_json, user_data=user_data, exercise=user_exercise.exercise_model) problem_logs[user_exercise].append(problem_log) self.output("problem_log", problem_log, jsonify(problem_log_json)) db.put(user_exercises) for k, v in problem_logs.iteritems(): db.put(v) if 'Goal' in params: with AutoNowDisabled(Goal): goals_json = json.loads(self.api("/api/v1/user/goals")) goals = [] for goal_json in goals_json[:params['Goal']]: goal = Goal.from_json(goal_json, user_data=user_data) goals.append(goal) self.output("goal", goal, jsonify(goal_json)) db.put(goals) # need to tell the userdata that it has goals user_data.has_current_goals = not all([g.completed for g in goals]) user_data.put()