Statistics
| Branch: | Tag: | Revision:

root / ui / views.py @ 391752b9

History | View | Annotate | Download (6.4 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33
#
34
import os
35
from django.conf import settings
36
from django.utils.translation import gettext_lazy as _
37
from django.template import Context, loader
38
from django.http import HttpResponse
39
from django.utils.translation import get_language
40
from django.utils import simplejson as json
41
from django.shortcuts import render_to_response
42
from django.core.urlresolvers import reverse
43

    
44
TIMEOUT = settings.TIMEOUT
45
UPDATE_INTERVAL = settings.UPDATE_INTERVAL
46
IMAGE_ICONS = settings.IMAGE_ICONS
47

    
48
def template(name, context):
49
    template_path = os.path.join(os.path.dirname(__file__), "templates/")
50
    current_template = template_path + name + '.html'
51
    t = loader.get_template(current_template)
52
    return HttpResponse(t.render(Context(context)))
53

    
54
def home(request):
55
    context = {'timeout': TIMEOUT,
56
               'project': '+nefo',
57
               'request': request,
58
               'current_lang': get_language() or 'en',
59
               'update_interval': UPDATE_INTERVAL,
60
               'image_icons': IMAGE_ICONS,}
61
    return template('home', context)
62

    
63
def machines(request):
64
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
65
    return template('machines', context)
66

    
67
def machines_icon(request):
68
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
69
    return template('machines_icon', context)
70

    
71
def machines_list(request):
72
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
73
    return template('machines_list', context)
74

    
75
def machines_single(request):
76
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
77
    return template('machines_single', context)
78

    
79
def machines_console(request):
80
    host, port, password = ('','','')
81
    host = request.GET.get('host','')
82
    port = request.GET.get('port','')
83
    password = request.GET.get('password','')
84
    machine = request.GET.get('machine','')
85
    host_ip = request.GET.get('host_ip','')
86
    context = {'host': host, 'port': port, 'password': password, 'machine': machine, 'host_ip': host_ip}
87
    return template('machines_console', context)
88

    
89

    
90
CONNECT_LINUX_LINUX_MESSAGE = _("Trying to connect from linux to linux")
91
CONNECT_LINUX_WINDOWS_MESSAGE = _("Trying to connect from linux to windows")
92
CONNECT_WINDOWS_LINUX_MESSAGE = _("Trying to connect from windows to linux")
93
CONNECT_WINDOWS_WINDOWS_MESSAGE = _("Trying to connect from windows to windows")
94

    
95
CONNECT_PROMT_MESSAGES = {
96
    'linux': {
97
            'linux': CONNECT_LINUX_LINUX_MESSAGE,
98
            'windows': CONNECT_LINUX_WINDOWS_MESSAGE
99
        },
100
    'windows': {
101
            'linux': CONNECT_WINDOWS_LINUX_MESSAGE,
102
            'windows': CONNECT_WINDOWS_WINDOWS_MESSAGE
103
        }
104
    }
105

    
106
def machines_connect(request):
107
    ip_address = request.GET.get('ip_address','')
108
    operating_system = request.GET.get('os','')
109
    host_os = request.GET.get('host_os','Linux').lower()
110

    
111
    if operating_system != "windows":
112
        operating_system = "linux"
113

    
114
    if operating_system == 'windows' and request.GET.get("rdp", False): #check if we are on windows
115
        rdp_file = os.path.join(os.path.dirname(__file__), "static/") + 'synnefo-windows.rdp'
116
        connect_data = open(rdp_file, 'r').read()
117
        connect_data = connect_data + 'full address:s:' + ip_address + '\n'
118
        response = HttpResponse(connect_data, mimetype='application/x-rdp')
119
        response['Content-Disposition'] = 'attachment; filename=synnefo-windows.rdp'
120
    else:
121
        ssh = False
122
        if (operating_system != "windows"):
123
            ssh = True
124

    
125
        info = _("Connect on windows using the following RDP shortcut file")
126
        link_title = _("Windows RDP shortcut file")
127
        link_url = "%s?ip_address=%s&os=%s&rdp=1" % (reverse("machines-connect"), ip_address, operating_system)
128

    
129
        if (operating_system != "windows"):
130
            info = _("Connect on linux machine using the following url")
131
            link_url = "ssh://%s/" % ip_address
132
            link_title = link_url
133

    
134
        # try to find a specific message
135
        try:
136
            connect_message = CONNECT_PROMT_MESSAGES[host_os][operating_system]
137
        except KeyError:
138
            connect_message = _("You are trying to connect from a %s machine to a %s machine") % (host_os, operating_system)
139

    
140
        response_object = {
141
                'ip': ip_address,
142
                'os': operating_system,
143
                'ssh': ssh,
144
                'info': unicode(connect_message),
145
                'link': {'title': unicode(link_title), 'url': link_url}
146
            }
147
        response = HttpResponse(json.dumps(response_object), mimetype='application/json')  #no windows, no rdp
148

    
149
    return response
150

    
151

    
152
def images(request):
153
    context = {}
154
    return template('images', context)
155

    
156
def disks(request):
157
    context = {}
158
    return template('disks', context)
159

    
160
def networks(request):
161
    context = {}
162
    return template('networks', context)
163

    
164
def files(request):
165
    context = {}
166
    return template('files', context)
167

    
168
def desktops(request):
169
    context = {}
170
    return template('desktops', context)
171

    
172
def apps(request):
173
    context = {}
174
    return template('apps', context)