Esempio n. 1
0
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()]
Esempio n. 2
0
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
Esempio n. 3
0
def remove(lng: str):
    """Remove a link
    """
    try:
        del _links[_threading.get_id()][lng]
    except KeyError:
        pass
Esempio n. 4
0
def reset():
    """Reset links
    """
    _links[_threading.get_id()] = {}

    for lng in _lang.langs(False):
        put(lng, _router.current_url(lang=lng))
Esempio n. 5
0
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
Esempio n. 6
0
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 ''
Esempio n. 7
0
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
Esempio n. 8
0
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, '')
Esempio n. 9
0
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
Esempio n. 10
0
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
Esempio n. 11
0
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')))
Esempio n. 12
0
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
Esempio n. 13
0
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
Esempio n. 14
0
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)
Esempio n. 15
0
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]
Esempio n. 16
0
def reset():
    """Reset
    """
    _inline_js[threading.get_id()] = []
Esempio n. 17
0
def get_all() -> dict:
    """Get all links
    """
    return _deepcopy(_links[_threading.get_id()])
Esempio n. 18
0
def get(lang: str) -> str:
    """Get a link
    """
    return _links[_threading.get_id()].get(lang)
Esempio n. 19
0
def put(lang: str, href: str):
    """Add a link
    """
    _links[_threading.get_id()][lang] = href