def _redirect_first_link_in_contents(request, content_id, version=None, lang=None, is_raw=False): """ Given a version and a content service, redirect to the first link in it's navigation. """ if not lang: lang = portal_helper.get_preferred_language(request) # Get the directory paths on the filesystem, AND of the URL. content_path, url_prefix = url_helper.get_full_content_path( content_id, lang, version) # If the content doesn't exist yet, try generating it. navigation = None try: navigation, menu_path = menu_helper.get_menu(content_id, lang, version) assert os.path.exists(content_path) except Exception, e: if type(e) in [AssertionError, IOError]: if type(e) == IOError: menu_path = e[1] _generate_content(os.path.dirname(menu_path), content_path, content_id, lang, version) if not navigation: navigation, menu_path = menu_helper.get_menu( content_id, lang, version) else: raise e
def content_sub_path(request, version, path=None): content_id = '' additional_context = {} if path.startswith(url_helper.DOCUMENTATION_ROOT): content_id = Content.DOCUMENTATION lang = portal_helper.get_preferred_language(request) search_url = '%s/%s/search.html' % (content_id, lang) additional_context = {'allow_search': True, 'search_url': search_url} elif path.startswith(url_helper.VISUALDL_ROOT): content_id = Content.VISUALDL elif path.startswith(url_helper.BOOK_ROOT): content_id = Content.BOOK elif path.startswith(url_helper.MODEL_ROOT): content_id = Content.MODELS elif path.startswith(url_helper.MOBILE_ROOT): content_id = Content.MOBILE elif path.startswith(url_helper.API_ROOT): content_id = Content.API search_url = '%s/%s/search.html' % (content_id, 'en') additional_context = {'allow_search': True, 'search_url': search_url} if settings.CURRENT_PPO_MODE == settings.PPO_MODES.DOC_EDIT_MODE: additional_context['form'] = PaddleOperatorsForm() return _render_static_content(request, version, content_id, additional_context)
def _redirect_first_link_in_contents(request, version, content_id, category=None): """ Given a version and a content service, redirect to the first link in it's navigation. """ lang = portal_helper.get_preferred_language(request) root_navigation = sitemap_helper.get_sitemap(version, lang) try: # Get the first section link from the content. content = root_navigation[content_id] path = _get_first_link_in_contents(content, lang, category) if not path: msg = 'Cannot perform reverse lookup on link: %s' % path raise Exception(msg) return redirect(path) except Exception as e: print e.message return redirect('/')
def change_version(request): """ Change current documentation version. """ # Look for a new version in the URL get params. preferred_version = request.GET.get('preferred_version', settings.DEFAULT_DOCS_VERSION) # Refers to the name of the contents service, for eg. 'models', 'documentation', or 'book'. content_id = request.GET.get('content_id', None) # Infer language based on session/cookie. lang = portal_helper.get_preferred_language(request) root_navigation = sitemap_helper.get_sitemap(preferred_version, lang) response = home_root(request) if content_id: if content_id in root_navigation and root_navigation[content_id]: response = _redirect_first_link_in_contents(request, preferred_version, content_id) else: # This version doesn't support this book. Redirect it back to home response = redirect('/') # If no content service specified, just redirect to first page of root site navigation. elif root_navigation and len(root_navigation) > 0: for content_id, content in root_navigation.items(): if content: response = _redirect_first_link_in_contents(request, preferred_version, content_id) portal_helper.set_preferred_version(request, response, preferred_version) return response
def base_context(request): path = urlparse(request.path).path content_id, lang, version = url_helper.get_parts_from_url_path(path) if not version: version = portal_helper.get_preferred_version(request) if lang in ['en', 'zh' ] and lang != portal_helper.get_preferred_language(request): portal_helper.set_preferred_language(request, None, lang) else: lang = portal_helper.get_preferred_language(request) return { 'CURRENT_DOCS_VERSION': version, 'settings': settings, 'url_helper': url_helper, 'lang': lang, 'content_id': content_id }
def content_sub_path(request, version, path=None): content_id = '' additional_context = {} if path.startswith(url_helper.DOCUMENTATION_ROOT): content_id = Content.DOCUMENTATION lang = portal_helper.get_preferred_language(request) search_url = '%s/%s/search.html' % (content_id, lang) if path.startswith(url_helper.DOCUMENTATION_ROOT + 'fluid'): search_url = '%s/fluid/%s/search.html' % (content_id, lang) additional_context = { 'allow_search': True, 'allow_version': True, 'search_url': search_url } elif path.startswith(url_helper.VISUALDL_ROOT): content_id = Content.VISUALDL elif path.startswith(url_helper.BOOK_ROOT): content_id = Content.BOOK elif path.startswith(url_helper.MODEL_ROOT): content_id = Content.MODELS elif path.startswith(url_helper.MOBILE_ROOT): content_id = Content.MOBILE elif path.startswith(url_helper.API_ROOT): content_id = Content.API search_url = '%s/%s/search.html' % (content_id, 'en') if path.startswith(url_helper.API_ROOT + 'fluid'): search_url = '%s/fluid/%s/search.html' % (content_id, 'en') additional_context = { 'allow_search': True, 'allow_version': True, 'search_url': search_url } return _render_static_content(request, version, content_id, additional_context)
def content_home(request, content_id, lang=None): is_raw = request.GET.get('raw', None) == '1' if lang == None: lang = portal_helper.get_preferred_language(request) content_id = urlparse(request.path).path[15:] if hasattr(request, 'urlconf') and request.urlconf == 'visualDL.urls': content_id = 'visualdl' elif content_id in ['en', 'zh']: # If content_id is en or zh, it means we are dealing with regular documentations content_id = 'docs' # else content_id stays the same, it could be models or mobile return _redirect_first_link_in_contents( request, content_id, 'develop' if content_id == 'visualdl' else portal_helper.get_preferred_version(request), lang, is_raw)