def get_module_with_contents(slug): """ Search for a module with respective sections and chapters :param slug: module's slug :return: Module with respective section on attribute sections """ return _Module.objects.filter(slug=slug).prefetch_related( _Prefetch( 'section_set', queryset=_Section.objects.order_by('order').prefetch_related( _Prefetch( 'chapter_set', queryset=_Chapter.objects.order_by('order').prefetch_related( _Prefetch( 'topic_set', queryset=_Topic.objects.order_by( 'order'), to_attr='topics') ), to_attr='chapters' ) ), to_attr='sections') ).get()
def find_cohort(slug): return _Cohort.objects.filter(slug=slug).prefetch_related( _Prefetch('liveclass_set', queryset=_LiveClass.objects.order_by('start'), to_attr='classes')).prefetch_related( _Prefetch('webinar_set', queryset=_Webinar.objects.order_by('start'), to_attr='webinars')).get()
def get_section_with_contents(slug): """ Search for a section with respective module and chapters :param slug: section's slug :return: Section """ return _Section.objects.filter( slug=slug).select_related('module').prefetch_related( _Prefetch( 'chapter_set', queryset=_Chapter.objects.order_by('order').prefetch_related( _Prefetch('topic_set', queryset=_Topic.objects.order_by('order'), to_attr='topics')), to_attr='chapters')).get()
def get_tree(module): """ Return a list of modules with the entire content on it :return: """ sections = list( _Section.objects.filter(module=module).order_by('order'). prefetch_related( _Prefetch( 'chapter_set', queryset=_Chapter.objects.order_by('order').prefetch_related( _Prefetch('topic_set', queryset=_Topic.objects.order_by('order'), to_attr='topics')), to_attr='chapters'))) module.sections = sections return sections
def get_entire_content_forest(): """ Return a list of modules with the entire content on it :return: """ return list(_Module.objects.all().prefetch_related( _Prefetch( 'section_set', queryset=_Section.objects.order_by('order').prefetch_related( _Prefetch( 'chapter_set', queryset=_Chapter.objects.order_by( 'order').prefetch_related( _Prefetch( 'topic_set', queryset=_Topic.objects.order_by('order'), to_attr='topics')), to_attr='chapters')), to_attr='sections')))
def get_chapter_with_contents(slug): """ Search for a chapter respective to slug with it's module, section and topics :param slug: chapter's slug :return: Chapter """ return _Chapter.objects.filter(slug=slug).select_related( 'section').select_related('section__module').prefetch_related( _Prefetch('topic_set', queryset=_Topic.objects.order_by('order'), to_attr='topics')).get()