cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
storage.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.storage
22 # @author Krzysztof Danielowski
23 # @author Piotr W√≥jcik
24 # @date 03.02.2012
25 #
26 from django.template import RequestContext
27 from django.template.loader import render_to_string
28 from django.utils.translation import ugettext as _
29 from django.views.decorators.csrf import csrf_protect
30 
31 from wi.commontags.templatetags.templatetags import filesizeformatmb
32 from wi.forms.node import MountNodeForm
33 from wi.utils import messages_ajax
34 from wi.utils.decorators import admin_cm_permission
35 from wi.utils.decorators import django_view
36 from wi.utils.messages_ajax import ajax_request
37 from wi.utils.states import storage_states_reversed as storage_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 returning storages list.
47 #
49  if request.method == 'GET':
50  storages = prep_data('admin_cm/storage/get_list/', request.session)
51 
52  for item in storages:
53  item['stateName'] = unicode(storage_states[item['state']])
54  item['capacity'] = filesizeformatmb(item['capacity'])
55 
56  return messages_ajax.success(storages)
57 
58 
59 @django_view
60 @ajax_request
61 @admin_cm_permission
62 @csrf_protect
63 ##
64 #
65 # Ajax view for storage to node mounting.
66 #
67 def cma_ajax_mount_node(request, storage_id, template_name='generic/form.html', form_class=MountNodeForm):
68  rest_data = prep_data({'nodes': 'admin_cm/node/get_list/'}, request.session)
69 
70  if request.method == 'POST':
71  form = form_class(data=request.POST, rest_data=rest_data)
72  if form.is_valid():
73  rest_data2 = prep_data({'storages': ('admin_cm/storage/mount/', [{'storage_id': int(storage_id),
74  'node_id': int(form.cleaned_data['node_id'])}]),
75  }, request.session)
76 
77  storages = rest_data2['storages']
78 
79  all_ok = True
80  for key in storages.keys():
81  for key2 in storages[key].keys():
82  if storages[key][key2] != 'mounted':
83  all_ok = False
84  if all_ok:
85  return messages_ajax.success(_('You have successfully mounted storage(s) to selected node.'))
86  else:
87  resp = []
88  for key in storages.keys():
89  for key2 in storages[key].keys():
90  if storages[key][key2] != 'mounted':
91  resp.append({'type': 'storage-node',
92  'storage_id': key,
93  'node_id': key2,
94  'status_text': storages[key][key2]
95  })
96  return messages_ajax.success(resp, 7999)
97  else:
98  form = form_class(rest_data=rest_data)
99  return messages_ajax.success(render_to_string(template_name, {'form': form,
100  'confirmation': _('Mount'),
101  'text': ''},
102  context_instance=RequestContext(request)),
103  status=1)
104