Esempio n. 1
0
def get_twitter_share_link_button(force_reload=False):
    value = cache.get_value('rendered_twitter_share_link_button')
    if value is None or force_reload:
        if get_config('social_twitter_share_link') != 'true':
            value = ''
        else:
            tpl = '''<a href="https://twitter.com/share" class="twitter-share-button"{twitter_via}{show_count}>Tweet</a>'''

            twitter_via = get_config('social_twitter_share_link_via')
            show_count = get_config('social_twitter_share_link_show_count')
            repl = {
                'twitter_via': 'pyrone',
                'show_count': ''
                }
            if twitter_via != '':
                # possible
                repl['twitter_via'] = ' data-via="{0}"'.format(html_escape(twitter_via))
            if show_count != 'true':
                repl['show_count'] = ' data-count="none"'

            value = tpl.format(**repl)

            value += '''<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>'''

        cache.set_value('rendered_twitter_share_link_button', value)

    return value
Esempio n. 2
0
def get_pages_widget_links(force_reload=False):

    value = cache.get_value('pages_links')

    if value is None or force_reload:
        pages_links = list()
        # fetch from settings, parse, fill cache
        raw = get_config('widget_pages_pages_spec')
        if raw is None:
            raw = ''
        for line in raw.split('\n'):
            line = line.strip()
            if len(line) == 0:
                continue
            # take first char - it's a delimiter
            delim = line[0]
            components = line[1:].split(delim)
            if len(components) != 2:
                continue
            url, title = components
            if not url.startswith('http://') and not url.startswith('https://'):
                continue
            link = {'url': url, 'title': title}
            pages_links.append(link)

        value = pages_links
        cache.set_value('pages_links', value)

    return value
Esempio n. 3
0
def get(key, force=False):
    """
    Get settings value, set "force" to True to update corresponding value in the cache
    """
    value = cache.get_value(key)
    if value is None or force is True:
        dbsession = DBSession()
        c = dbsession.query(Config).get(key)
        if c is not None:
            v = c.value
            cache.set_value(key, v)
            value = v

    if key == 'timezone' and not isinstance(value, pytz.tzinfo.DstTzInfo):
        # convert timezone string to the timezone object
        try:
            value = pytz.timezone(value)
        except pytz.exceptions.UnknownTimeZoneError:
            value = pytz.timezone('UTC')
        cache.set_value(key, value)

    return cache.get_value(key)
Esempio n. 4
0
def get_gplusone_button(force_reload=False):
    value = cache.get_value('rendered_gplusone_button')
    if value is None or force_reload:
        if get_config('social_gplusone') != 'true':
            value = ''
        else:
            tpl = '''<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone></g:plusone>'''
            value = tpl

        cache.set_value('rendered_gplusone_button', value)

    return value
Esempio n. 5
0
def get_public_tags_cloud(force_reload=False):
    """
    return tags cloud: list of tuples-pairs ("tag", "tag_weight"), tag_weight - is a number divisible by 5,
    0 <= tag_weight <= 100
    Only for published articles.
    """
    value = cache.get_value('tags_cloud')
    if value is None or force_reload:
        dbsession = DBSession()
        q = dbsession.query(func.count(Tag.id), Tag.tag).join(Article).filter(Article.is_draft==False).group_by(Tag.tag)
        items = list()
        counts = list()
        total = 0
        for rec in q.all():
            if rec[0] <= 0:
                continue
            total += rec[0]
            items.append((rec[1], int(rec[0])))
            counts.append(int(rec[0]))

        if len(counts) != 0:
            min_count = min(counts)
            max_counts = max(counts)

            if min_count == max_counts:
                # i.e. all tags counts are the same, so they have the same weight
                weights = [(x[0], 50) for x in items]
            else:
                lmm = lg(max_counts) - lg(min_count)

                weights = [(x[0], (lg(x[1])-lg(min_count)) / lmm) for x in items]
                weights = [(x[0], int(5*(int(100*x[1])/5))) for x in weights]

            value = weights
        else:
            value = []

        cache.set_value('tags_cloud', value)

    return value