Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / context_processors.py @ 0be81d73

History | View | Annotate | Download (2.7 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
            'https://accounts.okeanos.grnet.gr/static/im/cloudbar/')
35
    CB_COOKIE_NAME = getattr(settings, 'CLOUDBAR_COOKIE_NAME',
36
            'okeanos_account')
37
    CB_ACTIVE_SERVICE = getattr(settings, 'CLOUDBAR_ACTIVE_SERVICE',
38
            'cloud')
39
    CB_SERVICES_URL = getattr(settings, 'CLOUDBAR_SERVICES_URL',
40
            'https://accounts.okeanos.grnet.gr/astakos/api/get_services')
41
    CB_MENU_URL = getattr(settings, 'CLOUDBAR_MENU_URL',
42
            'https://accounts.okeanos.grnet.gr/im/get_menu')
43
    CB_HEIGHT = getattr(settings, 'CLOUDBAR_HEIGHT',
44
            '35')
45
    CB_BGCOLOR = getattr(settings, 'CLOUDBAR_BACKGROUND_COLOR',
46
            '#000000')
47

    
48
    CB_CODE = """
49
    <script type="text/javascript">
50
        var CLOUDBAR_LOCATION = "%(location)s";
51
        var CLOUDBAR_COOKIE_NAME = "%(cookie_name)s";
52
        var CLOUDBAR_ACTIVE_SERVICE = "%(active_service)s";
53
        var GET_SERVICES_URL = "%(services_url)s";
54
        var GET_MENU_URL = "%(menu_url)s";
55
        var CLOUDBAR_HEIGHT = '%(height)s';
56

57
        $(document).ready(function(){
58
            $.getScript(CLOUDBAR_LOCATION + 'cloudbar.js');
59
        });
60

61
    </script>
62
    <style>
63
        body {
64
            border-top: %(height)spx solid %(bg_color)s;
65
        }
66
        body .cloudbar {
67
            height: %(height)spx;
68
        }
69
    </style>
70
""" % {'location': CB_LOCATION,
71
       'active_service': CB_ACTIVE_SERVICE,
72
       'cookie_name': CB_COOKIE_NAME,
73
       'services_url': CB_SERVICES_URL,
74
       'menu_url': CB_MENU_URL,
75
       'height': str(CB_HEIGHT),
76
       'bg_color': CB_BGCOLOR}
77

    
78
    CB_CODE = mark_safe(CB_CODE)
79

    
80
    return {
81
        'CLOUDBAR_ACTIVE': CB_ACTIVE,
82
        'CLOUDBAR_LOCATION': CB_LOCATION,
83
        'CLOUDBAR_COOKIE_NAME': CB_COOKIE_NAME,
84
        'CLOUDBAR_ACTIVE_SERVICE': CB_ACTIVE_SERVICE,
85
        'CLOUDBAR_SERVICES_URL': CB_SERVICES_URL,
86
        'CLOUDBAR_MENU_URL': CB_MENU_URL,
87
        'CLOUDBAR_CODE': CB_CODE
88
    }