context_processors.py 2.28 KB
Newer Older
1
2
3
from django.utils.safestring import mark_safe
from django.conf import settings

Christos Stavrakakis's avatar
Christos Stavrakakis committed
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
def cloudbar(request):
    """
    Django context processor that applies all cloudbar settings in response
    context plus a ready to use pre rendered script html tag containing valid
    javascript code for cloudbar to display.

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

    Then in your base html template::

        <html>
        ....
        <head>
        ...
        {% if CLOUDBAR_ACTIVE %}
            {{ CLOUDBAR_CODE }}
        {% endif %}
        </head>
        <body>
        ....
        </body>
        </html>


    """

    CB_ACTIVE = getattr(settings, 'CLOUDBAR_ACTIVE', True)
33
    CB_LOCATION = getattr(settings, 'CLOUDBAR_LOCATION')
34
35
    CB_COOKIE_NAME = getattr(settings, 'CLOUDBAR_COOKIE_NAME',
            'okeanos_account')
36
37
    CB_SERVICES_URL = getattr(settings, 'CLOUDBAR_SERVICES_URL')
    CB_MENU_URL = getattr(settings, 'CLOUDBAR_MENU_URL')
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
38
39
40
41
    CB_HEIGHT = getattr(settings, 'CLOUDBAR_HEIGHT',
            '35')
    CB_BGCOLOR = getattr(settings, 'CLOUDBAR_BACKGROUND_COLOR',
            '#000000')
42
43
44
45
46
47
48

    CB_CODE = """
    <script type="text/javascript">
        var CLOUDBAR_LOCATION = "%(location)s";
        var CLOUDBAR_COOKIE_NAME = "%(cookie_name)s";
        var GET_SERVICES_URL = "%(services_url)s";
        var GET_MENU_URL = "%(menu_url)s";
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
49
        var CLOUDBAR_HEIGHT = '%(height)s';
50
51
52

        $(document).ready(function(){
            $.getScript(CLOUDBAR_LOCATION + 'cloudbar.js');
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
53
54
        });

55
    </script>
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
56
57
58
59
60
61
62
63
    <style>
        body {
            border-top: %(height)spx solid %(bg_color)s;
        }
        body .cloudbar {
            height: %(height)spx;
        }
    </style>
64
65
66
""" % {'location': CB_LOCATION,
       'cookie_name': CB_COOKIE_NAME,
       'services_url': CB_SERVICES_URL,
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
67
68
69
       'menu_url': CB_MENU_URL,
       'height': str(CB_HEIGHT),
       'bg_color': CB_BGCOLOR}
70
71
72
73
74
75
76
77
78
79
80

    CB_CODE = mark_safe(CB_CODE)

    return {
        'CLOUDBAR_ACTIVE': CB_ACTIVE,
        'CLOUDBAR_LOCATION': CB_LOCATION,
        'CLOUDBAR_COOKIE_NAME': CB_COOKIE_NAME,
        'CLOUDBAR_SERVICES_URL': CB_SERVICES_URL,
        'CLOUDBAR_MENU_URL': CB_MENU_URL,
        'CLOUDBAR_CODE': CB_CODE
    }