summaryrefslogtreecommitdiffstats
path: root/livesettings/templatetags/config_tags.py
blob: bcdded122bb7865060d92bc1970ada744deee599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from django import template
from django.contrib.sites.models import Site
from django.core import urlresolvers
from livesettings import config_value
from livesettings.utils import url_join
import logging

log = logging.getLogger('configuration.config_tags')

register = template.Library()

def force_space(value, chars=40):
    """Forces spaces every `chars` in value"""

    chars = int(chars)
    if len(value) < chars:
        return value
    else:
        out = []
        start = 0
        end = 0
        looping = True

        while looping:
            start = end
            end += chars
            out.append(value[start:end])
            looping = end < len(value)

    return ' '.join(out)

def break_at(value,  chars=40):
    """Force spaces into long lines which don't have spaces"""
    #todo: EF - lazy patch
    return value

    chars = int(chars)
    value = unicode(value)
    if len(value) < chars:
        return value
    else:
        out = []
        line = value.split(' ')
        for word in line:
            if len(word) > chars:
                out.append(force_space(word, chars))
            else:
                out.append(word)

    return " ".join(out)

register.filter('break_at', break_at)

def config_boolean(option):
    """Looks up the configuration option, returning true or false."""
    args = option.split('.')
    try:
        val = config_value(*args)
    except:
        log.warn('config_boolean tag: Tried to look up config setting "%s", got SettingNotSet, returning False', option)
        val = False
    if val:
        return "true"
    else:
        return ""

register.filter('config_boolean', config_boolean)

def admin_site_views(view):
    """Returns a formatted list of sites, rendering for view, if any"""

    if view:
        path = urlresolvers.reverse(view)
    else:
        path = None

    links = []
    for site in Site.objects.all():
        paths = ["http://", site.domain]
        if path:
            paths.append(path)

        links.append((site.name, url_join(paths)))

    ret = {
        'links' : links,
    }
    return ret


register.inclusion_tag('livesettings/_admin_site_views.html')(admin_site_views)