cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
node.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.views.admin_cm.view_node
22 # @author Krzysztof Danielowski
23 # @author Piotr W√≥jcik
24 # @date 03.02.2012
25 #
26 
27 from django.template import RequestContext
28 from django.template.loader import render_to_string
29 from django.utils.translation import ugettext as _
30 from django.views.decorators.csrf import csrf_protect
31 
32 from wi.commontags.templatetags.templatetags import filesizeformatmb
33 from wi.forms.storage import MountStorageForm
34 from wi.utils import messages_ajax
35 from wi.utils.decorators import admin_cm_permission, django_view
36 from wi.utils.messages_ajax import ajax_request
37 from wi.utils.states import node_states_reversed as node_states
38 from wi.utils.views import prep_data
39 
40 
41 @django_view
42 @ajax_request
43 @admin_cm_permission
44 ##
45 #
46 # Ajax view fetching node list.
47 #
49  if request.method == 'GET':
50  nodes = prep_data('admin_cm/node/get_list/', request.session)
51  for item in nodes:
52  item['stateName'] = unicode(node_states[item['state']])
53  item['memory_free'] = filesizeformatmb(item['memory_free'])
54  item['memory_total'] = filesizeformatmb(item['memory_total'])
55  item['description'] = item['comment']
56 
57  return messages_ajax.success(nodes)
58 
59 
60 @django_view
61 @ajax_request
62 @admin_cm_permission
63 ##
64 #
65 # Ajax view fetching node details.
66 #
67 def cma_ajax_node_details(request, node_id, template_name='admin_cm/ajax/node_details.html'):
68  if request.method == 'POST':
69  node = prep_data(('admin_cm/node/get_by_id_details/', {'node_id': node_id}), request.session)
70  return messages_ajax.success(render_to_string(template_name, {'node_id': node_id,
71  'node': node,
72  'node_states': node_states},
73  context_instance=RequestContext(request)))
74 
75 
76 @django_view
77 @ajax_request
78 @admin_cm_permission
79 @csrf_protect
80 ##
81 #
82 # Ajax view handling node to storage mounting.
83 #
84 def cma_ajax_mount_storage(request, node_id, template_name='generic/form.html', form_class=MountStorageForm):
85  rest_data = prep_data({'storages': 'admin_cm/storage/get_list/'}, request.session)
86 
87  if request.method == 'POST':
88  form = form_class(data=request.POST, files=request.FILES, rest_data=rest_data)
89  if form.is_valid():
90  rest_data2 = prep_data({'storage': ('admin_cm/storage/mount/', {'node_id': int(node_id),
91  'storage_id': int(form.cleaned_data['storage_id'])}), },
92  request.session)
93 
94  all_ok = True
95  storage = rest_data2['storage']
96  for key in storage.keys():
97  for key2 in storage[key].keys():
98  if storage[key][key2] != 'mounted':
99  all_ok = False
100  if all_ok:
101  return messages_ajax.success(_('You have successfully mounted storage(s) to selected node.'))
102  else:
103  resp = []
104  for key in storage.keys():
105  for key2 in storage[key].keys():
106  if storage[key][key2] != 'mounted':
107  resp.append({'type': 'storage-node',
108  'storage_id': key,
109  'node_id': key2,
110  'status_text': storage[key][key2]
111  })
112  return messages_ajax.success(resp, 7999)
113  else:
114  form = form_class(rest_data=rest_data)
115  return messages_ajax.success(render_to_string(template_name,
116  {'form': form,
117  'confirmation': _('Mount'),
118  'text': ''},
119  context_instance=RequestContext(request)),
120  status=1)
121