示例#1
0
def finish_subtitle_update(unique_key):
    oauth_util = OAuthUtil()

    query = FinishUpdateState.all()
    query.filter('unique_key =', unique_key)
    finish_state = query.get()

    query = SubtitleData.all()
    query.filter('language =', 'English')
    for sdata in query:
        if sdata.youtube_id in finish_state.videos_processed:
            continue

        try:
            usub_video = json.loads(
                oauth_util.access_usub_resource("/api/1.0/video/",
                                                sdata.youtube_id, True))
        except Exception, e:
            logging.info(
                "Unable to fetch video_id for video <%s> in playlist <%s> from universal subtitles"
                % (sdata.youtube_id, sdata.playlist))
            continue

        # A video could be in multiple playlists, so get all of them
        query2 = SubtitleData.all()
        query2.filter('language =', 'English')
        query2.filter('youtube_id =', sdata.youtube_id)
        for sdata2 in query2:
            sdata2.video_id = usub_video['video_id']
            sdata2.put()

        finish_state.videos_processed.append(sdata.youtube_id)
        finish_state.number_of_videos_processed += 1
        finish_state.put()
示例#2
0
    def get(self):
        oauth_util = OAuthUtil()
	logging.info("Accessing %s" % self.request.path)
        exercise_data = json.loads(oauth_util.access_resource("/api/v1/exercises"))

        for exercise in exercise_data:
            query = ExerciseData.all()
            query.filter('exercise_name =', exercise['name'])
            element = query.get()

            if element is None:
                element = ExerciseData()

            element.exercise_name = exercise['name']
            element.exercise_display_name = exercise['display_name']
            keywords = []
            keywords.append(exercise['name'].lower())
            for keyword in exercise['display_name'].lower().split():
                keywords.append(keyword)
                if re.sub(r"[a-zA-Z]+(s$)",match_func,keyword) == 's':
                    keywords.append(keyword[0:len(keyword)-1])
            element.keywords = keywords
            element.put()

        self.response.set_status(200)
        self.response.out.write("OK")
        return
示例#3
0
def class_data_worker(class_data):
    oauth_util = OAuthUtil()
    oauth_util.access_token = class_data.access_token

    # Remove this coach from the list of all students in our datastore, the coach will be added later on
    query = StudentData.all()
    query.filter('coaches =', class_data.coach)
    students = query.fetch(1000)
    for student in students:
        student.coaches.remove(class_data.coach)
        student.put()

    student_list = None
    get_students = True
    while get_students:
        try:
            if len(class_data.list_id
                   ) == 0 or class_data.list_id == "allstudents":
                student_list = json.loads(
                    oauth_util.access_resource(
                        "/api/v1/user/students?email=%s" % class_data.coach))
            else:
                student_list = json.loads(
                    oauth_util.access_resource(
                        "/api/v1/user/students?email=%s&list_id=%s" %
                        (class_data.coach, class_data.list_id)))
            get_students = False
        except Exception, e:
            logging.error("/api/v1/user/students failed with error: %s" % e)
示例#4
0
    def get(self):
	oauth_util = OAuthUtil()
        token = self.request.get('oauth_token')
        proxy_coach_email = self.request.get('proxy_coach_email')

        if proxy_coach_email:
	    oauth_util.set_access_token(self.request.get('access_token_key'),
			                self.request.get('access_token_secret'))
	    user = json.loads(oauth_util.access_resource("/api/v1/user?email=%s" % (proxy_coach_email)))
	    if user is None or user['email'] != proxy_coach_email:
		self.response.set_status(401)
                self.response.out.write('Not Authorized')
		return

            proxy_coach_key_email = user['key_email']
            query = UserGoals.all()
	    query.filter('coaches =', proxy_coach_key_email)
	    user_goals = query.fetch(1000)

            class_goals_report = "Student,Goal 1,Goal 2,Goal 3,Goal 4,Goal 5,Goal 6\n"
	    for goals in user_goals:
                class_goals_report += goals.user
		for goal in goals.goals:
		    class_goals_report += ",%s" % goal
		class_goals_report += "\n"

            self.response.headers['Content-Type'] = "text/csv"
	    self.response.headers['Content-Disposition'] = "attachment; filename=class_goals.csv"

	    self.response.out.write(class_goals_report)

	elif token:
            oauth_util.get_access_token(self)
	    template_values = self.authenticated_response(oauth_util)
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_key",oauth_util.access_token.key,max_age=2629743))
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_secret",oauth_util.access_token.secret,max_age=2629743))

            self.response.out.write(template.render('goalsreportadmin.html', template_values))

	else:
	    access_token_key = self.request.cookies.get('access_key')
	    access_token_secret = self.request.cookies.get('access_secret')
	    if access_token_key:
	        oauth_util.set_access_token(access_token_key, access_token_secret)
	        template_values = self.authenticated_response(oauth_util)

            else:
	        oauth_url = oauth_util.get_request_token_url(self.request.url)

                template_values = {
	            "callback" : False,
		    "done" : False,
	            "login_url" : oauth_url
	        }

            self.response.out.write(template.render('goalsreportadmin.html', template_values))
示例#5
0
    def get(self):
        logging.info("Accessing %s" % self.request.path)

        oauth_util = OAuthUtil()
        get_playlists = True
        while get_playlists:
            try:
                playlists = json.loads(
                    oauth_util.access_resource("/api/v1/playlists"))
                get_playlists = False
            except Exception, e:
                logging.error("/api/v1/playlists failed with error: %s" % e)
示例#6
0
    def get(self):
	template_values = {}
	oauth_util = OAuthUtil()
	access_token_key = self.request.cookies.get('access_key')
	token = self.request.get('oauth_token')

	if access_token_key:
	    access_token_secret = self.request.cookies.get('access_secret')
	    oauth_util.set_access_token(access_token_key, access_token_secret)
	    template_values = self.authenticated_response(oauth_util)

        elif token:
            oauth_util.get_access_token(self)
	    template_values = self.authenticated_response(oauth_util)
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_key",oauth_util.access_token.key,max_age=2629743))
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_secret",oauth_util.access_token.secret,max_age=2629743))

	else:
	    oauth_url = oauth_util.get_request_token_url(self.request.url)

            template_values = {
	        "callback" : False,
	        "done" : False,
	        "login_url" : oauth_url
	    }

        self.response.out.write(template.render('default.html', template_values))
示例#7
0
    def get(self):
	oauth_util = OAuthUtil()
	token = self.request.get('oauth_token')
	coach_email = self.request.get('coach_email')

        if coach_email:
	    template_values = {
	        "username" : coach_email,
		"logged_in" : True,
		"logout_url" : "/logout",
		"done" : True,
		"coach_email" : coach_email
	    }

        elif token:
	    oauth_util.get_access_token(self)
	    template_values = self.authenticated_response(oauth_util)
	    self.response.headers.add_header('Set-Cookie',
	        cookie_util.set_cookie_value("access_key",oauth_util.access_token.key,max_age=2629743))
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_secret",oauth_util.access_token.secret,max_age=2629743))

	else:
	    access_token_key = self.request.cookies.get('access_key')
	    access_token_secret = self.request.cookies.get('access_secret')
	    if access_token_key:
	        oauth_util.set_access_token(access_token_key, access_token_secret)
	        template_values = self.authenticated_response(oauth_util)

	    else:
	        oauth_url = oauth_util.get_request_token_url(self.request.url)

	        template_values = {
	            "callback" : False,
		    "done" : False,
		    "login_url" : oauth_url
                }

	self.response.out.write(template.render('uploadfile.html', template_values))
示例#8
0
    def get(self):
	oauth_util = OAuthUtil()
        token = self.request.get('oauth_token')
        student_email = self.request.get('student_email')

	if student_email:
	    goal_1 = self.request.get('goal_1')
	    goal_2 = self.request.get('goal_2')
	    goal_3 = self.request.get('goal_3')
	    goal_4 = self.request.get('goal_4')
	    goal_5 = self.request.get('goal_5')
	    goal_6 = self.request.get('goal_6')

            # send an email to the student with their goals
	    send_email = self.request.get('send_email')
	    if send_email == '1':
	        fromaddr = '*****@*****.**'
	        toaddr = student_email
	        subject = 'Khan Academy goals'
	        body = "Hello %s,\n\nYou have set the following exercises as your current Khan Academy goals.\n\n%s\n%s\n%s\n%s\n%s\n%s\n\nWe wish you good luck in attaining these goals\n\nCheers!\nKhan Academy Implementation team" % (student_email.split('@')[0],goal_1,goal_2,goal_3,goal_4,goal_5,goal_6)
	        mail.send_mail(sender=fromaddr,
			       to=toaddr,
			       subject=subject,
			       body=body)

	    oauth_util.set_access_token(self.request.get('access_token_key'),
			                self.request.get('access_token_secret'))
	    user = json.loads(oauth_util.access_resource("/api/v1/user"))

            query = UserGoals.all()
	    query.filter('user ='******'coaches']
	    user_goals.goals = []
	    if goal_1 is not None:
                user_goals.goals.append(goal_1)
	    if goal_2 is not None:
                user_goals.goals.append(goal_2)
	    if goal_3 is not None:
                user_goals.goals.append(goal_3)
	    if goal_4 is not None:
                user_goals.goals.append(goal_4)
	    if goal_5 is not None:
                user_goals.goals.append(goal_5)
	    if goal_6 is not None:
                user_goals.goals.append(goal_6)

	    user_goals.put()

	    template_values = {
	        "username" : student_email,
	        "logged_in" : True,
	        "logout_url" : "/logout",
	        "done" : True,
		"student_email" : student_email,
		"current_goals" : user_goals.goals
	    }

	elif token:
            oauth_util.get_access_token(self)
	    template_values = self.authenticated_response(oauth_util)
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_key",oauth_util.access_token.key,max_age=2629743))
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_secret",oauth_util.access_token.secret,max_age=2629743))

	else:
	    access_token_key = self.request.cookies.get('access_key')
	    access_token_secret = self.request.cookies.get('access_secret')
	    if access_token_key:
	        oauth_util.set_access_token(access_token_key, access_token_secret)
	        template_values = self.authenticated_response(oauth_util)

            else:
	        oauth_url = oauth_util.get_request_token_url(self.request.url)

                template_values = {
	            "callback" : False,
		    "done" : False,
	            "login_url" : oauth_url
	        }

        self.response.out.write(template.render('goals.html', template_values))
示例#9
0
    def get(self):
	oauth_util = OAuthUtil()
        token = self.request.get('oauth_token')
        coach_email = self.request.get('coach_email')

	if coach_email:
            list_id = self.request.get('list_id')
            if not list_id:
                list_id = "allstudents"
            display_only = self.request.get('display_only')
	    oauth_util.set_access_token(self.request.get('access_token_key'),
                                        self.request.get('access_token_secret'))
            if not display_only:
	        exercises = json.loads(oauth_util.access_resource("/api/v1/exercises"))
	        suggestions = []
                for exercise in exercises:
		    suggestion = self.request.get(exercise['display_name'])
		    if suggestion is not None:
		        if len(suggestion) > 0:
		            suggestions.append(suggestion)

                coach_key_email = json.loads(oauth_util.access_resource("/api/v1/user?email=%s" % coach_email))['key_email']
                query = CoachSuggestions.all()
                query.filter('coach =', coach_key_email)
                query.filter('list_id =', list_id)
                coach_suggestions = query.get()

	        if coach_suggestions is None:
		    coach_suggestions = CoachSuggestions()
		    coach_suggestions.coach = coach_key_email
                    coach_suggestions.list_id = list_id
		    coach_suggestions.suggestions = suggestions
	        else:
		    coach_suggestions.suggestions = suggestions

	        coach_suggestions.put()
            else:
                coach_key_email = json.loads(oauth_util.access_resource("/api/v1/user?email=%s" % coach_email))['key_email']
                query = CoachSuggestions.all()
                query.filter('coach =', coach_key_email)
                query.filter('list_id =', list_id)
                coach_suggestions = query.get()

                suggestions = []
                if coach_suggestions is not None:
                    suggestions = coach_suggestions.suggestions


            student_list = json.loads(oauth_util.access_resource("/api/v1/user/studentlists?email=%s" % coach_email))
            student_list.insert(0, {
                "key" : "allstudents",
                "name" : "All Students"
            })

	    template_values = {
	        "username" : coach_email,
	        "logged_in" : True,
	        "logout_url" : "/logout",
	        "done" : True,
		"coach_email" : json.dumps(coach_email),
	        "access_token_key" : json.dumps(oauth_util.access_token.key),
	        "access_token_secret" : json.dumps(oauth_util.access_token.secret),
		"coach_suggestions" : suggestions,
                "student_list" : student_list,
                "current_suggestions" : json.dumps(""),
                "filter_grade" : json.dumps(""),
                "filter_domain" : json.dumps(""),
		"list_id" : json.dumps(list_id)
	    }

	elif token:
            oauth_util.get_access_token(self)
	    template_values = self.authenticated_response(oauth_util)
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_key",oauth_util.access_token.key,max_age=2629743))
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_secret",oauth_util.access_token.secret,max_age=2629743))

	else:
	    access_token_key = self.request.cookies.get('access_key')
	    access_token_secret = self.request.cookies.get('access_secret')
	    if access_token_key:
	        oauth_util.set_access_token(access_token_key, access_token_secret)
	        template_values = self.authenticated_response(oauth_util)

            else:
	        oauth_url = oauth_util.get_request_token_url(self.request.url)

                template_values = {
	            "callback" : False,
		    "done" : False,
	            "login_url" : oauth_url
	        }

        self.response.out.write(template.render('suggestgoals.html', template_values))
示例#10
0
def write_student_data(user_data, coach, access_token, activity_log,
                       tz_offset_mins):
    oauth_util = OAuthUtil()
    oauth_util.access_token = access_token

    # Get data
    student_email = user_data['email']
    query = StudentData.all()
    query.filter('student_email =', student_email)
    student_data = query.get()

    if student_data == None:
        student_data = StudentData()
        student_data.coaches = user_data['coaches']
        student_data.student_email = student_email
        student_data.num_retries = 0

    if student_data.num_retries > 5:
        student_data.num_retries = 0
        student_data.put()
        return

    student_data.num_retries += 1
    student_data.put()

    # reset the coaches in case they have changed
    student_data.coaches = user_data['coaches']
    student_data.coaches.append(coach)
    keys = {}
    for coach in student_data.coaches:
        keys[coach] = 1
    student_data.coaches = keys.keys()

    dt_joined_utc = datetime.datetime.strptime(user_data['joined'],
                                               "%Y-%m-%dT%H:%M:%SZ")
    dt_joined_ctz = dt_joined_utc + datetime.timedelta(minutes=tz_offset_mins)
    dt_now_ctz = datetime.datetime.now()
    timedelta = dt_now_ctz - dt_joined_ctz
    student_data.member_for = timedelta.days
    student_data.energy_points = user_data['points']
    student_data.exercises_proficient = len(
        user_data['all_proficient_exercises'])

    student_data.meteorite_badges = user_data['badge_counts']['0']
    student_data.moon_badges = user_data['badge_counts']['1']
    student_data.earth_badges = user_data['badge_counts']['2']
    student_data.sun_badges = user_data['badge_counts']['3']
    student_data.blackhole_badges = user_data['badge_counts']['4']
    student_data.challenge_patches = user_data['badge_counts']['5']

    student_data.videos_watched = 0
    if activity_log:
        video_activity_in_class = {}
        video_activity_outside_class = {}

    video_data = json.loads(
        oauth_util.access_resource("/api/v1/user/videos?email=%s" %
                                   student_email))
    if video_data is not None:
        for video in video_data:
            if video['completed'] == True:
                student_data.videos_watched += 1

            if activity_log:
                youtube_id = video['video']['youtube_id']
                video_log_data = json.loads(
                    oauth_util.access_resource(
                        "/api/v1/user/videos/%s/log?email=%s" %
                        (youtube_id, student_email)))

                if video_log_data is not None:
                    for video_log in video_log_data:
                        dt_video_log_utc = datetime.datetime.strptime(
                            video_log['time_watched'], "%Y-%m-%dT%H:%M:%SZ")
                        dt_video_log_ctz = dt_video_log_utc + datetime.timedelta(
                            minutes=tz_offset_mins)
                        timedelta = dt_now_ctz - dt_video_log_ctz
                        if dt_video_log_ctz.hour > 8 and dt_video_log_ctz.hour < 16:
                            if timedelta.days in video_activity_in_class:
                                video_activity_in_class[
                                    timedelta.
                                    days] += video_log['seconds_watched']
                            else:
                                video_activity_in_class[
                                    timedelta.
                                    days] = video_log['seconds_watched']
                        else:
                            if timedelta.days in video_activity_outside_class:
                                video_activity_outside_class[
                                    timedelta.
                                    days] += video_log['seconds_watched']
                            else:
                                video_activity_outside_class[
                                    timedelta.
                                    days] = video_log['seconds_watched']

        if activity_log:
            # today
            student_data.va_ic_1 = 0
            student_data.va_oc_1 = 0
            if 0 in video_activity_in_class:
                student_data.va_ic_1 += video_activity_in_class[0]
            if 0 in video_activity_outside_class:
                student_data.va_oc_1 += video_activity_outside_class[0]
            student_data.va_1 = student_data.va_ic_1 + student_data.va_oc_1

            # last 7 days
            student_data.va_ic_7 = student_data.va_ic_1
            student_data.va_oc_7 = student_data.va_oc_1
            for i in range(1, 6):
                if i in video_activity_in_class:
                    student_data.va_ic_7 += video_activity_in_class[i]
                if i in video_activity_outside_class:
                    student_data.va_oc_7 += video_activity_outside_class[i]
            student_data.va_7 = student_data.va_ic_7 + student_data.va_oc_7

            # last 30 days
            student_data.va_ic_30 = student_data.va_ic_7
            student_data.va_oc_30 = student_data.va_oc_7
            for i in range(7, 30):
                if i in video_activity_in_class:
                    student_data.va_ic_30 += video_activity_in_class[i]
                if i in video_activity_outside_class:
                    student_data.va_oc_30 += video_activity_outside_class[i]
            student_data.va_30 = student_data.va_ic_30 + student_data.va_oc_30

            # total
            student_data.va_ic = student_data.va_ic_30
            student_data.va_oc = student_data.va_oc_30
            for i in range(31, len(video_activity_in_class)):
                if i in video_activity_in_class:
                    student_data.va_ic += video_activity_in_class[i]
                if i in video_activity_outside_class:
                    student_data.va_oc += video_activity_outside_class[i]
            student_data.va = student_data.va_ic + student_data.va_oc

    if activity_log:
        exercise_activity_in_class = {}
        exercise_activity_outside_class = {}

    topics = json.loads(oauth_util.access_resource("/api/v1/exercise_topics"))
    if topics is not None:
        for topic in topics:
            exercises = json.loads(
                oauth_util.access_resource(
                    "/api/v1/user/topic/%s/exercises?email=%s" %
                    (topic['id'], student_email)))
            if exercises is not None:
                for exercise in exercises:
                    if exercise['total_done'] == 0 and not exercise[
                            'exercise_states']['proficient']:
                        continue

                    query = StudentExerciseData.all()
                    query.filter('student_email =', student_data.student_email)
                    query.filter('name =', exercise['exercise'])
                    exercise_data = query.get()

                    if exercise_data is None:
                        exercise_data = StudentExerciseData()
                        exercise_data.student_email = student_data.student_email
                        exercise_data.name = exercise['exercise']
                        exercise_data.display_name = exercise[
                            'exercise_model']['display_name']
                        exercise_data.ekey = exercise_data.student_email + exercise_data.display_name

                    exercise_data.number_of_problems_attempted = exercise[
                        'total_done']
                    exercise_data.number_correct = exercise['total_correct']
                    exercise_data.current_streak = exercise['streak']
                    exercise_data.longest_streak = exercise['longest_streak']

                    exercise_data.days_since_proficient = -1
                    exercise_data.is_proficient = 0
                    if exercise['proficient_date'] != None and (
                            type(exercise['proficient_date']) is StringType or
                            type(exercise['proficient_date']) is UnicodeType):
                        dt_utc = datetime.datetime.strptime(
                            exercise['proficient_date'], "%Y-%m-%dT%H:%M:%SZ")
                        dt_ctz = dt_utc + datetime.timedelta(
                            minutes=tz_offset_mins)
                        timedelta = dt_now_ctz - dt_ctz
                        exercise_data.days_since_proficient = timedelta.days
                        exercise_data.is_proficient = 1

                    exercise_data.days_since_last_attempted = -1
                    if 'last_done' in exercise and (
                            type(exercise['last_done']) is StringType
                            or type(exercise['last_done']) is UnicodeType):
                        dt_utc = datetime.datetime.strptime(
                            exercise['last_done'], "%Y-%m-%dT%H:%M:%SZ")
                        dt_ctz = dt_utc + datetime.timedelta(
                            minutes=tz_offset_mins)
                        timedelta = dt_now_ctz - dt_ctz
                        exercise_data.days_since_last_attempted = timedelta.days

                    # trying to catch the exercise_states bug
                    exercise_data.status = "working"
                    try:
                        states = exercise['exercise_states']
                    except Exception, e:
                        logging.error(
                            "Exercise name: <%s>, error while reading exercise_states %s"
                            % (exercise['exercise'], e))
                    else:
                        for k in [
                                "proficient", "reviewing", "struggling",
                                "suggested"
                        ]:
                            if states[k]:
                                exercise_data.status = k
                                break

                    # Write exercise detail for this student to data store
                    exercise_data.put()

                    if activity_log:
                        exercise_log_data = json.loads(
                            oauth_util.access_resource(
                                "/api/v1/user/exercises/%s/log?email=%s" %
                                (exercise_data.name, student_email)))

                        if exercise_log_data is not None:
                            for exercise_log in exercise_log_data:
                                dt_exercise_log_utc = datetime.datetime.strptime(
                                    exercise_log['time_done'],
                                    "%Y-%m-%dT%H:%M:%SZ")
                                dt_exercise_log_ctz = dt_exercise_log_utc + datetime.timedelta(
                                    minutes=tz_offset_mins)
                                timedelta = dt_now_ctz - dt_exercise_log_ctz
                                if dt_exercise_log_ctz.hour > 8 and dt_exercise_log_ctz.hour < 16:
                                    if timedelta.days in exercise_activity_in_class:
                                        exercise_activity_in_class[
                                            timedelta.
                                            days] += exercise_log['time_taken']
                                    else:
                                        exercise_activity_in_class[
                                            timedelta.
                                            days] = exercise_log['time_taken']
                                else:
                                    if timedelta.days in exercise_activity_outside_class:
                                        exercise_activity_outside_class[
                                            timedelta.
                                            days] += exercise_log['time_taken']
                                    else:
                                        exercise_activity_outside_class[
                                            timedelta.
                                            days] = exercise_log['time_taken']

                if activity_log:
                    # today
                    student_data.ea_ic_1 = 0
                    student_data.ea_oc_1 = 0
                    if 0 in exercise_activity_in_class:
                        student_data.ea_ic_1 += exercise_activity_in_class[0]
                    if 0 in exercise_activity_outside_class:
                        student_data.ea_oc_1 += exercise_activity_outside_class[
                            0]
                    student_data.ea_1 = student_data.ea_ic_1 + student_data.ea_oc_1

                    # last 7 days
                    student_data.ea_ic_7 = student_data.ea_ic_1
                    student_data.ea_oc_7 = student_data.ea_oc_1
                    for i in range(1, 6):
                        if i in exercise_activity_in_class:
                            student_data.ea_ic_7 += exercise_activity_in_class[
                                i]
                        if i in exercise_activity_outside_class:
                            student_data.ea_oc_7 += exercise_activity_outside_class[
                                i]
                    student_data.ea_7 = student_data.ea_ic_7 + student_data.ea_oc_7

                    # last 30 days
                    student_data.ea_ic_30 = student_data.ea_ic_7
                    student_data.ea_oc_30 = student_data.ea_oc_7
                    for i in range(7, 30):
                        if i in exercise_activity_in_class:
                            student_data.ea_ic_30 += exercise_activity_in_class[
                                i]
                        if i in exercise_activity_outside_class:
                            student_data.ea_oc_30 += exercise_activity_outside_class[
                                i]
                    student_data.ea_30 = student_data.ea_ic_30 + student_data.ea_oc_30

                    # total
                    student_data.ea_ic = student_data.ea_ic_30
                    student_data.ea_oc = student_data.ea_oc_30
                    for i in range(31, len(exercise_activity_in_class)):
                        if i in exercise_activity_in_class:
                            student_data.ea_ic += exercise_activity_in_class[i]
                        if i in exercise_activity_outside_class:
                            student_data.ea_oc += exercise_activity_outside_class[
                                i]
                    student_data.ea = student_data.ea_ic + student_data.ea_oc
示例#11
0
def get_random_video_to_subtitle(playlist, language):
    oauth_util = OAuthUtil()
    if playlist == "all":
        all_playlists = json.loads(
            oauth_util.access_resource("/api/v1/playlists"))
        playlists = []
        for pl in all_playlists:
            if pl['title'] in MATH_PLAYLISTS:
                playlists.append(pl['title'])
    else:
        playlists = [playlist]

    query = SubtitleData.all()
    query.filter('language =', language)
    tmp = query.fetch(2)
    language_code = tmp[0].language_code

    for pl in playlists:
        query = SubtitleData.all()
        query.filter('language =', language)
        query.filter('playlist =', pl)
        sdata_list = query.fetch(1000)

        yid_list = []
        yid_list_all = []
        for sdata in sdata_list:
            yid_list_all.append(sdata.youtube_id)
            if sdata.percent_complete < 100:
                yid_list.append(sdata.youtube_id)

        ka_videos = json.loads(
            oauth_util.access_resource("/api/v1/playlists/%s/videos" %
                                       urllib.quote(pl)))

        klist = []
        for ka_video in ka_videos:
            klist.append(ka_video['youtube_id'])

        s = set(yid_list_all)
        l = klist
        klist = [x for x in l if x not in s]
        yid_list.extend(klist)

        random.shuffle(yid_list)
        for yid in yid_list:
            query = SubtitleData.all()
            query.filter('playlist =', pl)
            query.filter('language =', 'English')
            query.filter('youtube_id =', yid)
            english_data = query.get()

            if english_data is None:
                break

            if language != 'English':
                if english_data.percent_complete == 0:
                    continue

                # Found a random not completely subtitled video, with english done
                # Generate the URL
                url = "http://www.universalsubtitles.org/en/onsite_widget/?config={"
                url += '"videoID":"' + english_data.video_id + '"'
                url += ',"videoURL":"http://www.youtube.com/watch?v=' + yid + '"'
                url += ',"effectiveVideoURL":"http://www.youtube.com/watch?v=' + yid + '"'
                url += ',"languageCode":"' + language_code + '"'
                url += ',"originalLanguageCode":null'
                url += ',"subLanguagePK":null'
                url += ',"baseLanguagePK":"%d"}' % english_data.language_id

            else:
                # Found a random English video that isn't done
                # Generate the URL
                url = "http://www.universalsubtitles.org/en/videos/"
                url += english_data.video_id

            return url

    return None
示例#12
0
def populate_subtitle_data(playlist, pkey):
    logging.info("Updating playlist <%s>" % playlist)
    query = PlayListProcessedState.all()
    query.filter('pkey =', pkey)
    pstate = query.get()

    oauth_util = OAuthUtil()
    ka_videos = json.loads(
        oauth_util.access_resource("/api/v1/playlists/%s/videos" %
                                   urllib.quote(playlist)))
    logging.info("Fetched <%d> videos from KA for playlist <%s>" %
                 (len(ka_videos), playlist))

    query = PlayListData.all()
    query.filter('playlist =', playlist)
    pldata = query.get()
    if pldata is None:
        pldata = PlayListData()
        pldata.subtitle_languages = []
        pldata.playlist = playlist
        pldata.subtitled_in_english_count = 0

    pldata.video_count = len(ka_videos)
    lang_mapped = {}
    for ka_video in ka_videos:
        if ka_video['youtube_id'] in pstate.videos_processed:
            continue

        try:
            languages = json.loads(
                oauth_util.access_usub_resource(
                    "/api/1.0/subtitles/languages/", ka_video['youtube_id'],
                    False))
        except Exception, e:
            continue

        if type(languages) is not ListType:
            continue

        for language in languages:
            lang = string.replace(language['name'], ",", "")
            lang_in_native = lang
            if language['name'].find('(') >= 0:
                lang_in_native = language['name'].split('(')[1].split(')')[0]
                lang_in_native = string.replace(lang_in_native, ",", "")
                lang = language['name'].split('(')[0].strip()
                lang = string.replace(lang, ",", "")

            if lang not in lang_mapped:
                lang_mapped[lang] = True
                ldata = LanguageData.all().filter('language =', lang).get()
                if ldata is None:
                    ldata = LanguageData()
                    ldata.language = lang
                    ldata.lang_in_native = lang_in_native
                    ldata.translated_videos = 0
                    ldata.last_week_incremental = [0, 0, 0, 0, 0, 0, 0]
                    ldata.put()

                else:
                    if ldata.lang_in_native != lang_in_native:
                        ldata.lang_in_native = lang_in_native
                        ldata.language = lang
                        ldata.put()

            pldata.subtitle_languages.append(lang)

            query = SubtitleData.all()
            query.filter('youtube_id =', ka_video['youtube_id'])
            query.filter('language =', lang)
            query.filter('playlist =', playlist)
            sdata = query.get()
            if sdata is None:
                sdata = SubtitleData()
                sdata.youtube_id = ka_video['youtube_id']
                sdata.video = ka_video['title']
                sdata.playlist = playlist
                sdata.language = lang
                sdata.percent_complete = 0
                sdata.language_code = language['code']
                sdata.language_id = language['id']
                sdata.video_id = ""

            s = language['completion']
            try:
                string.index(s, " Line")
            except ValueError:
                try:
                    string.index(s, "%")
                except ValueError:
                    percent_complete = 0
                else:
                    percent_complete = int(string.rstrip(s, " %"))
            else:
                i = int(string.rstrip(s, " Lines"))
                if i == 0:
                    percent_complete = 0
                elif lang == 'English':
                    percent_complete = 100
                else:
                    percent_complete = 50

            if percent_complete > sdata.percent_complete:
                sdata.percent_complete = percent_complete
                sdata.language_id = language['id']

            sdata.put()

        pstate.videos_processed.append(ka_video['youtube_id'])
        pstate.count_of_videos_processed += 1
        pstate.put()

        # uniquify the list of languages interested in this playlist
        s = set(pldata.subtitle_languages)
        pldata.subtitle_languages = list(s)
        pldata.put()
示例#13
0
    def get(self):
        template_values = {}
        oauth_util = OAuthUtil()
        token = self.request.get('oauth_token')
        user_email = self.request.get('user_email')
        make_payment = self.request.get('make_payment')

        if make_payment:
            student_email = self.request.get('student_email')
            is_parent_str = self.request.get('is_parent')

            query = SummerStudent.all()
            query.filter('email = ', student_email)
            student = query.get()

            if student is None:
                output_str = 'Please <a href="/summer/application">apply</a> first' % student_email
                self.response.out.write(output_str)
                return

            query = ParentData.all()
            query.filter('email = ', student.parent_email)
            parent = query.get()

            if parent is None:
                logging.error(
                    "Unexpected NULL parent for student <%s> with parent <%s>"
                    % (student_email, student.parent_email))

            if is_parent_str == "True":
                is_parent = True
                payee = parent
            else:
                is_parent = False
                payee = student

            payee_phone_a = ""
            payee_phone_b = ""
            payee_phone_c = ""
            phone_parts = parent.phone.split("-")
            if phone_parts is not None:
                payee_phone_a = phone_parts[0]
                payee_phone_b = phone_parts[1]
                payee_phone_c = phone_parts[2]

            template_values = {
                "authenticated": True,
                "make_payment": True,
                "is_parent": is_parent,
                "is_parent_js": json.dumps(is_parent),
                "student": student,
                "student_js": json.dumps(student.to_dict()),
                "payee": payee,
                "payee_phone_a": payee_phone_a,
                "payee_phone_b": payee_phone_b,
                "payee_phone_c": payee_phone_c,
                "user_email": user_email,
                "username": user_email,
                "logged_in": True,
                "logout_url": "/logout"
            }

        elif user_email:
            first_name = self.request.get('first_name')
            student_email = self.request.get('student_email')

            query = SummerStudent.all()
            query.filter('email = ', student_email)
            student = query.get()
            if student is None:
                student = SummerStudent()
                student.email = student_email
                student.applier_email = user_email

            student.first_name = first_name
            student.last_name = self.request.get('last_name')

            student.date_of_birth = self.request.get('date_of_birth')

            if self.request.get('gender') == "Female":
                student.is_female = True
            else:
                student.is_female = False

            student.grade = self.request.get('grade')
            student.school = self.request.get('school')

            student.address_1 = self.request.get('address_1')
            student.address_2 = self.request.get('address_2')
            student.city = self.request.get('city')
            student.state = self.request.get('state')
            student.zipcode = self.request.get('zip')
            student.country = self.request.get('country')

            student.session_1 = self.request.get('session_1')
            student.session_2 = self.request.get('session_2')
            student.session_3 = self.request.get('session_3')

            session_choices = {"0": [], "1": [], "2": [], "3": []}
            session_choices[student.session_1].append("Session 1")
            session_choices[student.session_2].append("Session 2")
            session_choices[student.session_3].append("Session 3")

            student.no_choice = session_choices["0"]
            student.first_choice = session_choices["1"]
            student.second_choice = session_choices["2"]
            student.third_choice = session_choices["3"]

            student.answer_why = self.request.get('answer_why')
            student.answer_how = self.request.get('answer_how')

            student.processing_fee = self.request.get('fee')
            student.processing_fee_paid = False

            student.tuition = 'TBD'
            student.tuition_paid = False

            student.application_year = '2012'
            student.application_status = 'Processing'

            if user_email == student_email:
                is_parent = False
                student.self_applied = True
            else:
                is_parent = True
                student.self_applied = False

            student.parent_relation = self.request.get('relation')
            student.parent_email = self.request.get('parent_email')

            student.put()

            query = ParentData.all()
            query.filter('email = ', student.parent_email)
            parent = query.get()
            if parent is None:
                parent = ParentData()
                parent.email = student.parent_email

            parent.first_name = self.request.get('parent_first_name')
            parent.last_name = self.request.get('parent_last_name')
            parent.address_1 = self.request.get('parent_address_1')
            parent.address_2 = self.request.get('parent_address_2')
            parent.city = self.request.get('parent_city')
            parent.state = self.request.get('parent_state')
            parent.zipcode = self.request.get('parent_zip')
            parent.country = self.request.get('parent_country')
            parent.phone = self.request.get('parent_phone')
            parent.comments = self.request.get('parent_comments')

            if student.key() not in parent.students:
                parent.students.append(student.key())

            parent.put()

            if is_parent:
                payee = parent
            else:
                payee = student

            payee_phone_a = ""
            payee_phone_b = ""
            payee_phone_c = ""
            phone_parts = parent.phone.split("-")
            if phone_parts is not None:
                payee_phone_a = phone_parts[0]
                payee_phone_b = phone_parts[1]
                payee_phone_c = phone_parts[2]

            template_values = {
                "authenticated": True,
                "make_payment": True,
                "is_parent": is_parent,
                "is_parent_js": json.dumps(is_parent),
                "student": student,
                "student_js": json.dumps(student.to_dict()),
                "parent": parent,
                "parent_js": json.dumps(parent.to_dict()),
                "payee": payee,
                "payee_phone_a": payee_phone_a,
                "payee_phone_b": payee_phone_b,
                "payee_phone_c": payee_phone_c,
                "user_email": user_email,
                "username": user_email,
                "logged_in": True,
                "logout_url": "/logout"
            }

        elif token:
            oauth_util.get_access_token(self)
            template_values = self.authenticated_response(oauth_util)
            self.response.headers.add_header(
                'Set-Cookie',
                cookie_util.set_cookie_value("access_key",
                                             oauth_util.access_token.key,
                                             max_age=2629743))
            self.response.headers.add_header(
                'Set-Cookie',
                cookie_util.set_cookie_value("access_secret",
                                             oauth_util.access_token.secret,
                                             max_age=2629743))

        else:
            access_token_key = self.request.cookies.get('access_key')
            access_token_secret = self.request.cookies.get('access_secret')
            if access_token_key:
                oauth_util.set_access_token(access_token_key,
                                            access_token_secret)
                template_values = self.authenticated_response(oauth_util)

            else:
                oauth_url = oauth_util.get_request_token_url(self.request.url)

                template_values = {
                    "authenticated": False,
                    "apply": False,
                    "login_url": oauth_url
                }

        self.response.out.write(template.render('summer.html',
                                                template_values))
示例#14
0
    def convert_to_xls(self):
        # Create the workbook
        wb = Workbook()

        # Write the header rows
        activity_detail_ws = wb.add_sheet('activity_detail')
        ad_row = 0
        ad_col = 0
        activity_detail_ws.write(ad_row, ad_col, "Student Email")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Member For")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Energy Points")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Exercises Proficient")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Videos Watched")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Metorite Badges")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Moon Badges")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Earth Badges")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Sun Badges")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Blackhole Badges")
        ad_col += 1
        activity_detail_ws.write(ad_row, ad_col, "Challenge Patches")
        ad_col += 1
        if self.activity_log:
            activity_detail_ws.write(ad_row, ad_col, "Exercise Activity Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Exercise Activity In Class Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Exercise Activity Outside Class Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, "Video Activity Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Video Activity In Class Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Video Activity Outside Class Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, "Total Activity Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Total Activity In Class Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Total Activity Outside Class Today")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Exercise Activity Past 7 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Exercise Activity In Class Past 7 days")
            ad_col += 1
            activity_detail_ws.write(
                ad_row, ad_col, "Exercise Activity Outside Class Past 7 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Video Activity Past 7 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Video Activity In Class Past 7 days")
            ad_col += 1
            activity_detail_ws.write(
                ad_row, ad_col, "Video Activity Outside Class Past 7 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Total Activity Past 7 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Total Activity In Class Past 7 days")
            ad_col += 1
            activity_detail_ws.write(
                ad_row, ad_col, "Total Activity Outside Class Past 7 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Exercise Activity Past 30 days")
            ad_col += 1
            activity_detail_ws.write(
                ad_row, ad_col, "Exercise Activity In Class Past 30 days")
            ad_col += 1
            activity_detail_ws.write(
                ad_row, ad_col, "Exercise Activity Outside Class Past 30 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Video Activity Past 30 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Video Activity In Class Past 30 days")
            ad_col += 1
            activity_detail_ws.write(
                ad_row, ad_col, "Video Activity Outside Class Past 30 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Total Activity Past 30 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Total Activity In Class Past 30 days")
            ad_col += 1
            activity_detail_ws.write(
                ad_row, ad_col, "Total Activity Outside Class Past 30 days")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Exercise Activity To date")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Exercise Activity In Class To date")
            ad_col += 1
            activity_detail_ws.write(
                ad_row, ad_col, "Exercise Activity Outside Class To date")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, "Video Activity To date")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Video Activity In Class To date")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Video Activity Outside Class To date")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, "Total Activity To date")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Total Activity In Class To date")
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     "Total Activity Outside Class To date")
            ad_col += 1
        ad_row += 1

        exercise_detail_ws = wb.add_sheet('exercise_detail')
        ed_row = 0
        ed_col = 0
        exercise_detail_ws.write(ed_row, ed_col, "Student Email")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Exercise Name")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Exercise Display Name")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Key")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Status")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Is Proficient")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col,
                                 "Number of Problems Attempted")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Number Correct")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Current Streak")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Longest Streak")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Days Since Proficient")
        ed_col += 1
        exercise_detail_ws.write(ed_row, ed_col, "Days Since Last Attempted")
        ed_col += 1
        ed_row += 1

        student_emails = []
        if len(self.list_id) > 0 and self.list_id != "allstudents":
            oauth_util = OAuthUtil()
            oauth_util.access_token = self.access_token
            students = json.loads(
                oauth_util.access_resource(
                    "/api/v1/user/students?email=%s&list_id=%s" %
                    (self.coach, self.list_id)))
            if students is not None:
                for student in students:
                    student_emails.append(student['email'])

        query = StudentData.all()
        query.filter('coaches =', self.coach)
        if len(self.list_id) > 0 and self.list_id != "allstudents":
            query.filter('student_email IN', student_emails)
        students_data = query.fetch(1000)

        # Get activity detail and exercise detail
        for student in students_data:
            ad_col = 0
            activity_detail_ws.write(ad_row, ad_col, student.student_email)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.member_for)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.energy_points)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col,
                                     student.exercises_proficient)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.videos_watched)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.meteorite_badges)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.moon_badges)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.earth_badges)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.sun_badges)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.blackhole_badges)
            ad_col += 1
            activity_detail_ws.write(ad_row, ad_col, student.challenge_patches)
            ad_col += 1
            if self.activity_log:
                activity_detail_ws.write(
                    ad_row, ad_col, (student.ea_ic_1 + student.ea_oc_1) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.ea_ic_1 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.ea_oc_1 / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_ic_1 + student.va_oc_1) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.va_ic_1 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.va_oc_1 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col,
                                         (student.va_1 + student.ea_1) / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_ic_1 + student.ea_ic_1) / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_oc_1 + student.ea_oc_1) / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.ea_ic_7 + student.ea_oc_7) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.ea_ic_7 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.ea_oc_7 / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_ic_7 + student.va_oc_7) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.va_ic_7 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.va_oc_7 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col,
                                         (student.va_7 + student.ea_7) / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_ic_7 + student.ea_ic_7) / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_oc_7 + student.ea_oc_7) / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.ea_ic_30 + student.ea_oc_30) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.ea_ic_30 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.ea_oc_30 / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_ic_30 + student.va_oc_30) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.va_ic_30 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.va_oc_30 / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col,
                                         (student.va_30 + student.ea_30) / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_ic_30 + student.ea_ic_30) / 60)
                ad_col += 1
                activity_detail_ws.write(
                    ad_row, ad_col, (student.va_oc_30 + student.ea_oc_30) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col,
                                         (student.ea_ic + student.ea_oc) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.ea_ic / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.ea_oc / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col,
                                         (student.va_ic + student.va_oc) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.va_ic / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col, student.va_oc / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col,
                                         (student.va + student.ea) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col,
                                         (student.va_ic + student.ea_ic) / 60)
                ad_col += 1
                activity_detail_ws.write(ad_row, ad_col,
                                         (student.va_oc + student.ea_oc) / 60)
                ad_col += 1
            ad_row += 1

            query = StudentExerciseData.all()
            query.filter('student_email =', student.student_email)
            exercises_data = query.fetch(1000)

            for exercise in exercises_data:
                ed_col = 0
                exercise_detail_ws.write(ed_row, ed_col, student.student_email)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col, exercise.name)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col, exercise.display_name)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col, exercise.ekey)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col, exercise.status)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col,
                                         exercise.is_proficient)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col,
                                         exercise.number_of_problems_attempted)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col,
                                         exercise.number_correct)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col,
                                         exercise.current_streak)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col,
                                         exercise.longest_streak)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col,
                                         exercise.days_since_proficient)
                ed_col += 1
                exercise_detail_ws.write(ed_row, ed_col,
                                         exercise.days_since_last_attempted)
                ed_col += 1
                ed_row += 1

        # Get goals, if any
        if len(self.student_goals) > 0:
            goals_ws = wb.add_sheet('goals')
            csv_data = StringIO.StringIO(self.student_goals)
            reader = csv.reader(csv_data)
            row = 0
            for line in reader:
                column = 0
                for cell in line:
                    goals_ws.write(row, column, cell)
                    column += 1
                row += 1
            csv_data.close()

        self.excel_output = StringIO.StringIO()
        wb.save(self.excel_output)

        return
示例#15
0
    def get(self):
	oauth_util = OAuthUtil()
        token = self.request.get('oauth_token')
        coach_email = self.request.get('coach_email')

        if coach_email:
	    oauth_util.set_access_token(self.request.get('access_token_key'),
			                self.request.get('access_token_secret'))
	    user = json.loads(oauth_util.access_resource("/api/v1/user"))
	    coach_key_email = user['key_email']

            list_id = self.request.get('list_id')
	    student_emails = []
            if list_id and list_id != "allstudents":
                students = json.loads(oauth_util.access_resource(
                                      "/api/v1/user/students?email=%s&list_id=%s" %
                                      (coach_email, list_id)))

	        if students is not None:
	            for student in students:
	                student_emails.append(student['email'])

            query = UserGoals.all()
	    query.filter('coaches =', coach_key_email)
            if list_id and list_id != "allstudents":
                query.filter('user IN', student_emails)
	    user_goals = query.fetch(1000)

            class_goals_report = "Student,Goal 1,Goal 2,Goal 3,Goal 4,Goal 5,Goal 6\n"
	    for goals in user_goals:
                class_goals_report += goals.user
		for goal in goals.goals:
		    class_goals_report += ",%s" % goal
		class_goals_report += "\n"

            self.response.headers['Content-Type'] = "text/csv"
	    self.response.headers['Content-Disposition'] = "attachment; filename=class_goals.csv"

	    self.response.out.write(class_goals_report)

	elif token:
            oauth_util.get_access_token(self)
	    template_values = self.authenticated_response(oauth_util)
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_key",oauth_util.access_token.key,max_age=2629743))
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_secret",oauth_util.access_token.secret,max_age=2629743))

            self.response.out.write(template.render('goalsreport.html', template_values))

	else:
	    access_token_key = self.request.cookies.get('access_key')
	    access_token_secret = self.request.cookies.get('access_secret')
	    if access_token_key:
	        oauth_util.set_access_token(access_token_key, access_token_secret)
	        template_values = self.authenticated_response(oauth_util)

            else:
	        oauth_url = oauth_util.get_request_token_url(self.request.url)

                template_values = {
	            "callback" : False,
		    "done" : False,
	            "login_url" : oauth_url
	        }

            self.response.out.write(template.render('goalsreport.html', template_values))
示例#16
0
    def get(self):
	oauth_util = OAuthUtil()
        token = self.request.get('oauth_token')
        coach_email = self.request.get('coach_email')

        if coach_email:
	    oauth_util.set_access_token(self.request.get('access_token_key'),
			                self.request.get('access_token_secret'))
            class_data = ClassData()
	    class_data.coach = coach_email
	    class_data.mailto = coach_email
	    class_data.unique_key = ''.join(random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") for x in range(10))

            activity_log = self.request.get('activity_log')
	    if activity_log == '1':
                class_data.activity_log = True

            goals_data = self.request.get('goals_data')
            if goals_data == '1':
                class_data.generate_goals_dashboard = True

                user = json.loads(oauth_util.access_resource("/api/v1/user?email=%s" % coach_email))
		if user is None or user['email'] != coach_email:
                    self.response.set_status(401)
	            self.response.out.write("Not Authorized")
	            return

                coach_suggested_goals = self.request.get('coach_suggested_goals')
		coach_key_email = user['key_email']
                if coach_suggested_goals == '1':
                    list_id = self.request.get('list_id')
                    if not list_id or list_id == "allstudents":
                        students = json.loads(oauth_util.access_resource(
                                              "/api/v1/user/students?email=%s" % coach_email))
                        list_id = "allstudents"
                    else:
                        students = json.loads(oauth_util.access_resource(
                                              "/api/v1/user/students?email=%s&list_id=%s" %
                                              (coach_email, list_id)))

                    query = CoachSuggestions.all()
	            query.filter('coach =', coach_key_email)
	            query.filter('list_id =', list_id)
	            coach_suggestions = query.get()

                    class_data.student_goals = "Student,"
                    for i in range(0,len(coach_suggestions.suggestions)):
                        class_data.student_goals += "Goal %d," % (i+1)
                    class_data.student_goals += "\n"

                    for student in students:
                        class_data.student_goals += student['email']
                        if len(coach_suggestions.suggestions) > 0:
                            for suggestion in coach_suggestions.suggestions:
                                class_data.student_goals += ",%s" % suggestion
                            class_data.student_goals += "\n"
                else:
                    query = UserGoals.all()
	            query.filter('coaches =', coach_key_email)
	            user_goals = query.fetch(1000)

                    class_data.student_goals = "Student,Goal 1,Goal 2,Goal 3,Goal 4,Goal 5,Goal 6\n"
	            for goals in user_goals:
                        class_data.student_goals += goals.user
		        for goal in goals.goals:
		            class_data.student_goals += ",%s" % goal
		        class_data.student_goals += "\n"

            proxy_coach = self.request.get('proxy_coach')
	    if proxy_coach:
                class_data.mailto = proxy_coach
		coach_email = proxy_coach

	    class_data.tz_offset_mins = int(self.request.get('tz_offset'))
	    class_data.access_token = oauth_util.access_token
            class_data.consumer_key = oauth_util.consumer_key
            class_data.consumer_secret = oauth_util.consumer_secret
	    class_data.server_url = oauth_util.server_url
            class_data.list_id = self.request.get('list_id')

            class_data.run()

	    template_values = {
	        "username" : coach_email,
	        "logged_in" : True,
	        "logout_url" : "/logout",
		"done" : True,
	        "coach_email" : coach_email,
	    }

	elif token:
            oauth_util.get_access_token(self)
	    template_values = self.authenticated_response(oauth_util)
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_key",oauth_util.access_token.key,max_age=2629743))
	    self.response.headers.add_header('Set-Cookie',
		cookie_util.set_cookie_value("access_secret",oauth_util.access_token.secret,max_age=2629743))

	else:
	    access_token_key = self.request.cookies.get('access_key')
	    access_token_secret = self.request.cookies.get('access_secret')
	    if access_token_key:
	        oauth_util.set_access_token(access_token_key, access_token_secret)
		template_values = self.authenticated_response(oauth_util)

	    else:
	        oauth_url = oauth_util.get_request_token_url(self.request.url)

                template_values = {
	            "callback" : False,
		    "done" : False,
	            "login_url" : oauth_url
	        }

        self.response.out.write(template.render('classreport.html', template_values))
示例#17
0
    def get(self):
        random_video_to_subtitle = self.request.get('random')
        playlist = self.request.get('playlist')
        language = self.request.get('language')

        if not playlist or not language:
            self.redirect("/translations/subtitlestatus")

        if language == "all":
            self.redirect("/translations/subtitlestatus")

        if random_video_to_subtitle:
            url = get_random_video_to_subtitle(playlist, language)
            if url is not None:
                self.redirect(url)
                return
            else:
                if playlist == "all":
                    response = "All videos in the MATH playlists and language '"
                    response += language
                    response += "' have been subtitled, pick another playlist"
                else:
                    response = "All videos for playlist '"
                    response += playlist
                    response += "' and language '"
                    response += language
                    response += "' have been subtitled"

                self.response.set_status(200)
                self.response.out.write(response)
                return

        if playlist == "all":
            self.redirect("/translations/subtitlestatus")

        query = SubtitleData.all()
        query.filter('language =', language)
        query.filter('playlist =', playlist)
        sdata_list = query.fetch(1000)

        if not sdata_list:
            query = SubtitleData.all()
            query.filter('language =', language)
            tmp = query.fetch(2)
            language_code = tmp[0].language_code
        else:
            language_code = sdata_list[0].language_code

        percent_complete = {}
        for sdata in sdata_list:
            percent_complete[sdata.youtube_id] = sdata.percent_complete

        oauth_util = OAuthUtil()
        ka_videos = json.loads(
            oauth_util.access_resource("/api/v1/playlists/%s/videos" %
                                       urllib.quote(playlist)))

        output_list = []
        for ka_video in ka_videos:
            query = SubtitleData.all()
            query.filter('playlist =', playlist)
            query.filter('language =', 'English')
            query.filter('youtube_id =', ka_video['youtube_id'])
            english_data = query.get()

            if english_data is None:
                continue

            pc = 0
            english_pc = True
            if english_data.percent_complete == 0:
                english_pc = False

            if ka_video['youtube_id'] in percent_complete:
                if percent_complete[ka_video['youtube_id']] == 100:
                    continue

                pc = percent_complete[ka_video['youtube_id']]

            output = {
                'video_id': english_data.video_id,
                'youtube_id': ka_video['youtube_id'],
                'percent_complete': pc,
                'video': ka_video['title'],
                'language_code': language_code,
                'base_language_id': english_data.language_id,
                'english_complete': english_pc
            }

            output_list.append(output)

        sorted_list = sorted(output_list, key=lambda k: k['percent_complete'])
        template_values = {
            'output_list': sorted_list,
            'language': language,
            'playlist': playlist
        }

        self.response.out.write(
            template.render('subtitleactions.html', template_values))
        return