Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / context_processors.py @ 4ec8c043

History | View | Annotate | Download (2.3 kB)

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

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

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

13
    Then in your base html template::
14

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

28

29
    """
30

    
31
    CB_ACTIVE = getattr(settings, 'CLOUDBAR_ACTIVE', True)
32
    CB_LOCATION = getattr(settings, 'CLOUDBAR_LOCATION',
33
            'https://accounts.okeanos.grnet.gr/static/im/cloudbar/')
34
    CB_COOKIE_NAME = getattr(settings, 'CLOUDBAR_COOKIE_NAME',
35
            'okeanos_account')
36
    CB_ACTIVE_SERVICE = getattr(settings, 'CLOUDBAR_ACTIVE_SERVICE',
37
            'cloud')
38
    CB_SERVICES_URL = getattr(settings, 'CLOUDBAR_SERVICES_URL',
39
            'https://accounts.okeanos.grnet.gr/im/get_services')
40
    CB_MENU_URL = getattr(settings, 'CLOUDBAR_MENU_URL',
41
            'https://accounts.okeanos.grnet.gr/im/get_menu')
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 CLOUDBAR_ACTIVE_SERVICE = "%(active_service)s";
48
        var GET_SERVICES_URL = "%(services_url)s";
49
        var GET_MENU_URL = "%(menu_url)s";
50

51
        $(document).ready(function(){
52
            $.getScript(CLOUDBAR_LOCATION + 'cloudbar.js');
53
        })
54
    </script>
55
""" % {'location': CB_LOCATION,
56
       'active_service': CB_ACTIVE_SERVICE,
57
       'cookie_name': CB_COOKIE_NAME,
58
       'services_url': CB_SERVICES_URL,
59
       'menu_url': CB_MENU_URL}
60

    
61
    CB_CODE = mark_safe(CB_CODE)
62

    
63
    return {
64
        'CLOUDBAR_ACTIVE': CB_ACTIVE,
65
        'CLOUDBAR_LOCATION': CB_LOCATION,
66
        'CLOUDBAR_COOKIE_NAME': CB_COOKIE_NAME,
67
        'CLOUDBAR_ACTIVE_SERVICE': CB_ACTIVE_SERVICE,
68
        'CLOUDBAR_SERVICES_URL': CB_SERVICES_URL,
69
        'CLOUDBAR_MENU_URL': CB_MENU_URL,
70
        'CLOUDBAR_CODE': CB_CODE
71
    }
72