def get(self): # Fetches votes for currently logged in user # Replace lesson_id and practice_id with full versions params = self.get_params() if 'practice_id' in params: params[u'practice_id'] = Practice.get_long_uid( params[u'practice_id']) if 'lesson_id' in params: params[u'lesson_id'] = Lesson.get_long_uid(params[u'lesson_id']) votes = self.api.get('Vote', ancestor=self.api.user, **params) self.write(votes)
def get(self, topic_id, lesson_id): full_lesson_id = Lesson.get_long_uid(lesson_id) lesson = self.api.get_by_id(full_lesson_id) full_topic_id = Topic.get_long_uid(topic_id) topic = self.api.get_by_id(full_topic_id) # check all content objects were found if not (lesson is None or topic is None): # Increment view counts on the lesson view_counter.increment(full_lesson_id) view_counter.increment('{}:{}'.format(full_topic_id, full_lesson_id)) theme = self.api.get_by_id(topic.themes[0]) # Determines if current theme is for Teachers or not # 'teacher_theme' variable affects the UI teacher_theme = (theme.short_uid in ['growth-mindset', 'growth-mindset-teachers']) # Get related practices related_practices = Practice.get_related_practices(topic, 4) # get other lessons in topic for navigating lessons = [] # first check for bad topic--lesson match if topic.lessons: lessons = self.api.get_by_id(topic.lessons) # get lesson index and previous and next lessons lesson_index = 0 if lesson.uid in topic.lessons: lesson_index = topic.lessons.index(lesson.uid) next_topic = '' related_topics = [] # get next lesson from current or next topic next_lesson = '' next_lesson_url = '' next_url = '' if lesson_index < len(lessons) - 1: next_lesson = lessons[lesson_index + 1] next_lesson_url = '/topics/{}/{}'.format( topic.short_uid, next_lesson.short_uid) next_url = next_lesson_url else: # fetch next topic for final lesson topic_index = 0 if topic.uid in theme.topics: topic_index = theme.topics.index(topic.uid) if topic_index < len(theme.topics) - 1: next_topic = self.api.get_by_id( theme.topics[topic_index + 1]) next_url = '/topics/{}'.format(next_topic.short_uid) # get list of 3 other topics related_topic_ids = [] for idx, val in enumerate(theme.topics): if next_topic: if val != topic.uid and val != next_topic.uid: related_topic_ids.append(val) elif val != topic.uid: related_topic_ids.append(val) if related_topic_ids: related_topics = self.api.get_by_id(related_topic_ids) if len(related_topics) >= 3: related_topics = random.sample(related_topics, 3) # All topic lessons use default locale locale = config.default_locale if os.path.isfile('templates/lessons/' + lesson.short_uid + '.html'): self.write( '/lessons/{}.html'.format(lesson.short_uid), theme=theme, teacher_theme=teacher_theme, topic=topic, lesson=lesson, lessons=lessons, lesson_index=lesson_index, next_lesson=next_lesson, next_lesson_url=next_lesson_url, next_url=next_url, next_topic=next_topic, color=topic.color, related_topics=related_topics, related_practices=related_practices, locale=locale, translation=locales.translations[locale]["lessons"], ) else: # 404 if lesson html cannot be found return self.http_not_found() else: # 404 if lesson cannot be found return self.http_not_found()
def get(self, theme_id, topic_id, lesson_id): full_lesson_id = Lesson.get_long_uid(lesson_id) lesson = self.api.get_by_id(full_lesson_id) full_theme_id = Theme.get_long_uid(theme_id) theme = self.api.get_by_id(full_theme_id) full_topic_id = Topic.get_long_uid(topic_id) topic = self.api.get_by_id(full_topic_id) # check all content objects were found if lesson is not None and topic is not None and theme is not None: # Increment view counts on the lesson view_counter.increment(full_lesson_id) view_counter.increment('{}:{}:{}'.format(full_theme_id, full_topic_id, full_lesson_id)) # Get other topics in theme for navigating topics = [] if theme.topics: topics = self.api.get_by_id(theme.topics) # get other lessons in topic for navigating lessons = [] # first check for bad topic--lesson match if topic.lessons: lessons = self.api.get_by_id(topic.lessons) # get lesson index and previous and next lessons lesson_index = 0 topic_index = 0 if topic.uid in theme.topics: topic_index = theme.topics.index(topic.uid) if lesson.uid in topic.lessons: lesson_index = topic.lessons.index(lesson.uid) # get next lesson from current or next topic next_lesson = '' next_lesson_url = '' next_topic = '' next_topic_url = '' next_url = '' if lesson_index < len(lessons) - 1: next_lesson = lessons[lesson_index + 1] next_lesson_url = '/{}/{}/{}'.format( theme.short_uid, topic.short_uid, next_lesson.short_uid) next_url = next_lesson_url elif topic_index < len(theme.topics) - 1: next_topic = topics[topic_index + 1] next_topic_url = '/{}/{}'.format(theme.short_uid, next_topic.short_uid) next_url = next_topic_url # Get translated text and locale if theme.locale in config.available_locales: locale = theme.locale else: locale = config.default_locale if os.path.isfile('templates/lessons/' + lesson.short_uid + '.html'): self.write( '/lessons/{}.html'.format(lesson.short_uid), theme=theme, topic=topic, lesson=lesson, lessons=lessons, lesson_index=lesson_index, next_lesson=next_lesson, next_lesson_url=next_lesson_url, next_topic=next_topic, next_topic_url=next_topic_url, next_url=next_url, color=topic.color, audience=theme.target_audience, locale=locale, translation=locales.translations[locale]["lessons"], ) else: # 404 if lesson html cannot be found return self.http_not_found() else: # 404 if lesson cannot be found return self.http_not_found()