Statistics
| Branch: | Tag: | Revision:

root / snf-webproject / synnefo / webproject / context_processors.py @ 49044e69

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.webproject.context_processors.cloudbar`` in your
12
    project's ``TEMPLATE_CONTEXT_PROCESSORS setting`` (snf-webproject already
13
    does).
14

15
    Then in your base html template::
16

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

30

31
    """
32

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

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

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

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

    
72
    CB_CODE = mark_safe(CB_CODE)
73

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