def course(self, slug, api_id, locale): """Fetches a course by slug.""" courses = self.courses(api_id, locale, {'fields.slug': slug}) if courses: return courses[0] raise EntryNotFoundError('errorMessage404Course')
def search_entry_by_field(self, space_id, content_type_id, field_name, field_value): request_url = '{}/spaces/{}/environments/master/entries?&content_type={}&{}[match]={}'.format( self.CONTENTFUL_CM_URL, space_id, content_type_id, field_name, urllib.quote_plus(field_value.encode('utf-8'))) headers = { 'Authorization': 'Bearer {}'.format(self.CONTENTFUL_MANAGEMENT_TOKEN), 'Content-Type': 'application/vnd.contentful.management.v1+json', 'X-Contentful-Content-Type': content_type_id } response = urlfetch.fetch(method=urlfetch.GET, url=request_url, headers=headers) if response.status_code == 404: raise EntryNotFoundError( "Entry not found for query: '{0}={1}'".format( field_name, field_value)) if response.status_code / 400: raise RequestError('{} {}'.format(response.status_code, response.content)) return json.loads(response.content)
def get_entry_by_id(self, space_id, content_type_id, entry_id): request_url = '{}/spaces/{}/environments/master/entries/{}'.format( self.CONTENTFUL_CM_URL, space_id, entry_id) headers = { 'Authorization': 'Bearer {}'.format(self.CONTENTFUL_MANAGEMENT_TOKEN), 'Content-Type': 'application/vnd.contentful.management.v1+json', 'X-Contentful-Content-Type': content_type_id } response = urlfetch.fetch(method=urlfetch.GET, url=request_url, headers=headers) if response.status_code == 404: raise EntryNotFoundError( "Entry not found for ID: '{0}'".format(entry_id)) if response.status_code / 400: raise RequestError('{} {}'.format(response.status_code, response.content)) return response
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 landing_page(self, slug, api_id, locale): """Fetches a landing page by slug.""" pages = self.client(api_id).entries({ 'content_type': 'layout', 'locale': locale, 'include': 6, 'fields.slug': slug }) if pages: return pages[0] raise EntryNotFoundError( 'Landing Page not found for slug: {0}'.format(slug))
def entry(self, entry_id, query=None): if query is None: query = {} self._normalize_select(query) query.update({'sys.id': entry_id}) response = self._get('/entries', query) if self.raw_mode: return response try: response[0] except IndexError: raise EntryNotFoundError( "Entry not found for ID: '{0}'".format(entry_id))
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 entry(self, entry_id, _api_id): if self.published_at is None: raise EntryNotFoundError('error') return MockEntry(entry_id, self.updated_at, self.published_at)