def get_current() -> str: """Get current language """ if not _languages: raise RuntimeError('No languages are defined') tid = _threading.get_id() if tid not in _current: _current[_threading.get_id()] = _languages[0] return _current[_threading.get_id()]
def get_previous_user() -> _model.AbstractUser: """Get previous user """ tid = threading.get_id() p_tid = threading.get_parent_id() user = _previous_user.get(tid) or _current_user.get( p_tid) or _previous_user.get(p_tid) if not user: user = get_anonymous_user() _previous_user[threading.get_id()] = user return user
def remove(lng: str): """Remove a link """ try: del _links[_threading.get_id()][lng] except KeyError: pass
def reset(): """Reset links """ _links[_threading.get_id()] = {} for lng in _lang.langs(False): put(lng, _router.current_url(lang=lng))
def set_current(language: str): """Set current language """ if language not in _languages: raise _error.LanguageNotSupported("Language '{}' is not supported".format(language)) _current[_threading.get_id()] = language
def inline_js(s: str = None) -> Optional[str]: tid = threading.get_id() if s: _inline_js[tid].append(s) else: return ''.join(_inline_js[tid]) if tid in _inline_js else ''
def dump(tag: str) -> str: """Dump a tag """ if not _tags: raise RuntimeError('reset() should be called before') tid = _threading.get_id() if tag not in _tags[tid]: return '' # Page charset if tag == 'charset': r = '<meta charset="{}">\n'.format(_tags[tid][tag]) # Page title elif tag == 'title': r = '<title>{} | {}</title>\n'.format(_tags[tid][tag], _lang.t('app_name')) # OpenGraph tags elif tag.startswith('og:') or tag.startswith('author:') or tag.startswith('fb:'): r = '<meta property="{}" content="{}">'.format(tag, _tags[tid][tag]) # Page links elif tag == 'link': r = '' for value in _tags[tid][tag]: args_str = ' '.join(['{}="{}"'.format(k, v) for k, v in value.items()]) r += '<{} {}>\n'.format(tag, args_str) # Other else: r = '<meta name="{}" content="{}">'.format(tag, _tags[tid][tag]) return r
def get(tag: str) -> str: """Get value of the tag """ if not _tags: raise RuntimeError('reset() should be called before') return _tags[_threading.get_id()].get(tag, '')
def get_current_user() -> _model.AbstractUser: """Get current user """ user = _current_user.get(threading.get_id()) or _current_user.get( threading.get_parent_id()) if not user: user = switch_user_to_anonymous() return user
def langs(include_current: bool = True) -> _List[str]: """Get all defined languages """ r = _languages.copy() if not include_current: r.remove(_current[_threading.get_id()]) return r
def reset(title: str = None): """Reset tags """ tid = _threading.get_id() _tags[tid] = {} t_set('charset', 'UTF-8') t_set('title', title or _lang.t('pytsite.metatag@untitled_document')) t_set('viewport', 'width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0') t_set('pytsite-version', str(_package_info.version('pytsite')))
def switch_user(user: _model.AbstractUser): """Switch current user """ tid = threading.get_id() p_tid = threading.get_parent_id() _previous_user[tid] = _current_user.get(tid) or _current_user.get( p_tid) or get_anonymous_user() _current_user[tid] = user return user
def dump_all() -> str: """Dump all tags """ if not _tags: raise RuntimeError('reset() should be called before') _events.fire('pytsite.metatag@dump_all') r = str() for tag in _tags[_threading.get_id()]: r += dump(tag) + '\n' return r
def t_set(tag: str, value: str = None, **kwargs): """Set tag's value """ if not _tags: raise RuntimeError('reset() should be called before') tid = _threading.get_id() if tag not in _tags[tid]: _tags[tid][tag] = [] if tag == 'link' else '' if tag == 'link': _tags[tid][tag].append(kwargs) else: _tags[tid][tag] = _util.escape_html(value)
def rm(tag: str, **kwargs): """Remove a tag """ tid = _threading.get_id() if tid not in _tags or tag not in _tags[tid]: return if tag == 'link': if not _tags[tid][tag]: return values_to_rm = [] for v in _tags[tid][tag]: if set(kwargs.items()).issubset(set(v.items())): values_to_rm.append(v) for v in values_to_rm: _tags[tid][tag].remove(v) else: del _tags[tid][tag]
def reset(): """Reset """ _inline_js[threading.get_id()] = []
def get_all() -> dict: """Get all links """ return _deepcopy(_links[_threading.get_id()])
def get(lang: str) -> str: """Get a link """ return _links[_threading.get_id()].get(lang)
def put(lang: str, href: str): """Add a link """ _links[_threading.get_id()][lang] = href