def show_courses_by_category(category_slug): categories = contentful().categories(api_id(), locale().code) active_category = None for category in categories: if category.slug == category_slug: active_category = category break if active_category is None: raise EntryNotFoundError('errorMessage404Category') courses = contentful().courses_by_category( active_category.id, api_id(), locale().code ) if should_attach_entry_state(api_id(), session): for course in courses: attach_entry_state(course) return render_with_globals( 'courses', title='{0} ({1})'.format(active_category.title, len(courses)), courses=courses, categories=categories, current_category=active_category, breadcrumbs=refine(raw_breadcrumbs(), active_category) )
def show_index(): landing_page = contentful().landing_page('home', api_id(), locale().code) if should_attach_entry_state(api_id(), session): attach_entry_state(landing_page) return render_with_globals( 'landingPage', title='Home', landing_page=landing_page, breadcrumbs=refine( raw_breadcrumbs(), landing_page ) )
def show_courses(): courses = contentful().courses(api_id(), locale().code) categories = contentful().categories(api_id(), locale().code) if should_attach_entry_state(api_id(), session): for course in courses: attach_entry_state(course) return render_with_globals( 'courses', title='{0} ({1})'.format( translate('allCoursesLabel', locale().code), len(courses) ), courses=courses, categories=categories )
def find_courses_by_slug(slug): course = contentful().course(slug, api_id(), locale().code) lessons = course.lessons if 'lessons' in course.fields(locale().code) else [] visited_lessons = session.get('visited_lessons', []) if course.id not in visited_lessons: visited_lessons.append(course.id) session['visited_lessons'] = visited_lessons if should_attach_entry_state(api_id(), session): attach_entry_state(course) return render_with_globals( 'course', title=course.title, course=course, lessons=lessons, lesson=None, next_lesson=lessons[0] if len(lessons) > 0 else None, visited_lessons=visited_lessons, breadcrumbs=refine(raw_breadcrumbs(), course) )
def find_lesson_by_slug(course_slug, lesson_slug): course = contentful().course(course_slug, api_id(), locale().code) lessons = course.lessons if 'lessons' in course.fields(locale().code) else [] lesson = None for l in lessons: if l.slug == lesson_slug: lesson = l break if lesson is None: raise EntryNotFoundError('errorMessage404Lesson') visited_lessons = session.get('visited_lessons', []) if lesson.id not in visited_lessons: visited_lessons.append(lesson.id) session['visited_lessons'] = visited_lessons next_lesson = find_next_lesson(lessons, lesson.slug) if should_attach_entry_state(api_id(), session): attach_entry_state(course) attach_entry_state(lesson) return render_with_globals( 'course', title=course.title, course=course, lessons=lessons, lesson=lesson, next_lesson=next_lesson, visited_lessons=visited_lessons, breadcrumbs=refine( refine( raw_breadcrumbs(), course ), lesson ) )
def test_true_when_current_api_is_cpa_and_editorial_features_is_true(self): self.assertTrue(should_attach_entry_state('cpa', Session(True)))
def test_false_when_current_api_is_cpa_but_editorial_features_is_false( self): self.assertFalse(should_attach_entry_state('cpa', Session(False)))
def test_false_when_current_api_is_not_cpa(self): self.assertFalse(should_attach_entry_state('cda', Session(True)))