Пример #1
0
    def test_get_deleted(self):
        """Deleted entities should never be returned."""
        # Create and delete a theme.
        theme = self.admin_api.create('Theme',
                                      name="Test Theme",
                                      listed=True,
                                      deleted=True)

        # Get the theme by key to enforce consistency.
        theme.key.get()

        # Prove that the deletion worked and is consistent.
        fetched_theme = Theme.query(Theme.name == "Test Theme").get()
        self.assertIsInstance(fetched_theme, Theme)
        self.assertTrue(fetched_theme.deleted)

        # It should no longer appear for anyone, either by id or by query.

        public_entity = self.public_api.get_by_id(theme.uid)
        normal_entity = self.normal_api.get_by_id(theme.uid)
        admin_entity = self.admin_api.get_by_id(theme.uid)

        self.assertIsNone(public_entity)
        self.assertIsNone(normal_entity)
        self.assertIsNone(admin_entity)

        public_results = self.public_api.get('Theme')
        normal_results = self.normal_api.get('Theme')
        admin_results = self.admin_api.get('Theme')

        self.assertNotIn(theme, public_results)
        self.assertNotIn(theme, normal_results)
        self.assertNotIn(theme, admin_results)
Пример #2
0
 def head(self, theme_id):
     # Include an override here to not confuse bad subdirectories as 200 OK
     id = Theme.get_long_uid(theme_id)
     theme = self.api.get_by_id(id)
     if theme is not None:
         self.response.clear()
     else:
         self.error(404)
Пример #3
0
 def options(self, theme_id):
     # Include an override here to not confuse bad subdirectories as 200 OK
     id = Theme.get_long_uid(theme_id)
     theme = self.api.get_by_id(id)
     if theme is not None:
         self.response.set_status(200)
         self.response.headers['Allow'] = 'GET,HEAD,OPTIONS'
     else:
         self.error(404)
Пример #4
0
    def get(self, theme_id):
        id = Theme.get_long_uid(theme_id)
        theme = self.api.get_by_id(id)
        first_lesson_link = ''
        if theme is not None:
            # fetch topics for theme
            topics = []
            if theme.topics:
                topics = self.api.get_by_id(theme.topics)
                # fetch lessons for each topic
                topic_lesson_ids = [
                    id for topic in topics for id in topic.lessons
                ]
                theme_lessons = self.api.get_by_id(topic_lesson_ids)
                # associate lessons with appropriate topics
                for topic in topics:
                    topic.lessons_list = [
                        l for l in theme_lessons if l.uid in topic.lessons
                    ]

                # get first lesson for CTA
                first_lesson_link = '/{}'.format(theme.short_uid)
                has_first_lesson = topics[0] and topics[
                    0].lessons_list and topics[0].lessons_list[0]
                if has_first_lesson:
                    first_lesson_link = '/{}/{}/{}'.format(
                        first_lesson_link, topics[0].short_uid,
                        topics[0].lessons_list[0].short_uid)

            # Get related practices
            related_practices = Practice.get_related_practices(theme, 6)

            # Get translated text and locale
            if theme.locale in config.available_locales:
                locale = theme.locale
            else:
                locale = default_locale

            self.write(
                'theme.html',
                theme=theme,
                topics=topics,
                first_lesson_link=first_lesson_link,
                audience=theme.target_audience,
                related_practices=related_practices,
                locale=locale,
                translation=locales.translations[locale]["courses"],
            )
        else:
            # Special (temporary) redirect for math kit
            # Consider adding 'aliases' for themes
            if theme_id == 'math':
                self.redirect('/growth-mindset-math', permanent=True)
            # 404 if theme cannot be found
            return self.http_not_found()
def load_themes():
    """load themes into database."""
    print("Themes")

    Theme.query.delete()

    for tag in themes_tags:
        theme_index = themes_tags.index(tag) + 1
        add_theme = Theme(theme_id=theme_index, theme=tag)

        db.session.add(add_theme)

    db.session.commit()
Пример #6
0
 def get_all_themes(self):
     themes = self.collection.find()
     target_themes = []
     for theme in themes:
         target_themes.append(Theme(theme))
     return target_themes
Пример #7
0
 def get_themes_for_user(self, user_id):
     user = self.collection.find({'user_id': user_id})
     target_themes = []
     for theme in user:
         target_themes.append(Theme(theme))
     return target_themes
Пример #8
0
 def get_theme_by_id(self, theme_id):
     theme = self.collection.find_one({'_id': theme_id})
     if theme is None:
         return None
     target_theme = Theme(theme)
     return target_theme
Пример #9
0
    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()