Statistics
| Branch: | Tag: | Revision:

root / ui / views.py @ 0e8e87ac

History | View | Annotate | Download (6.5 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
               'DEBUG': settings.DEBUG}
62
    return template('home', context)
63

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

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

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

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

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

    
90

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

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

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

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

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

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

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

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

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

    
150
    return response
151

    
152

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

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

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

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

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

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