Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / context_processors.py @ 7fb14dbb

History | View | Annotate | Download (2.3 kB)

1
from django.utils.safestring import mark_safe
2
from django.conf import settings
3

    
4

    
5
def cloudbar(request):
6
    """
7
    Django context processor that applies all cloudbar settings in response
8
    context plus a ready to use pre rendered script html tag containing valid
9
    javascript code for cloudbar to display.
10

11
    To use it add ``synnefo.lib.context_processors.cloudbar`` in your project's
12
    ``TEMPLATE_CONTEXT_PROCESSORS setting`` (snf-webproject already does).
13

14
    Then in your base html template::
15

16
        <html>
17
        ....
18
        <head>
19
        ...
20
        {% if CLOUDBAR_ACTIVE %}
21
            {{ CLOUDBAR_CODE }}
22
        {% endif %}
23
        </head>
24
        <body>
25
        ....
26
        </body>
27
        </html>
28

29

30
    """
31

    
32
    CB_ACTIVE = getattr(settings, 'CLOUDBAR_ACTIVE', True)
33
    CB_LOCATION = getattr(settings, 'CLOUDBAR_LOCATION')
34
    CB_COOKIE_NAME = getattr(settings, 'CLOUDBAR_COOKIE_NAME',
35
            'okeanos_account')
36
    CB_SERVICES_URL = getattr(settings, 'CLOUDBAR_SERVICES_URL')
37
    CB_MENU_URL = getattr(settings, 'CLOUDBAR_MENU_URL')
38
    CB_HEIGHT = getattr(settings, 'CLOUDBAR_HEIGHT',
39
            '35')
40
    CB_BGCOLOR = getattr(settings, 'CLOUDBAR_BACKGROUND_COLOR',
41
            '#000000')
42

    
43
    CB_CODE = """
44
    <script type="text/javascript">
45
        var CLOUDBAR_LOCATION = "%(location)s";
46
        var CLOUDBAR_COOKIE_NAME = "%(cookie_name)s";
47
        var GET_SERVICES_URL = "%(services_url)s";
48
        var GET_MENU_URL = "%(menu_url)s";
49
        var CLOUDBAR_HEIGHT = '%(height)s';
50

51
        $(document).ready(function(){
52
            $.getScript(CLOUDBAR_LOCATION + 'cloudbar.js');
53
        });
54

55
    </script>
56
    <style>
57
        body {
58
            border-top: %(height)spx solid %(bg_color)s;
59
        }
60
        body .cloudbar {
61
            height: %(height)spx;
62
        }
63
    </style>
64
""" % {'location': CB_LOCATION,
65
       'cookie_name': CB_COOKIE_NAME,
66
       'services_url': CB_SERVICES_URL,
67
       'menu_url': CB_MENU_URL,
68
       'height': str(CB_HEIGHT),
69
       'bg_color': CB_BGCOLOR}
70

    
71
    CB_CODE = mark_safe(CB_CODE)
72

    
73
    return {
74
        'CLOUDBAR_ACTIVE': CB_ACTIVE,
75
        'CLOUDBAR_LOCATION': CB_LOCATION,
76
        'CLOUDBAR_COOKIE_NAME': CB_COOKIE_NAME,
77
        'CLOUDBAR_SERVICES_URL': CB_SERVICES_URL,
78
        'CLOUDBAR_MENU_URL': CB_MENU_URL,
79
        'CLOUDBAR_CODE': CB_CODE
80
    }