cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
storage_image.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.user.storage_image
22 #
23 # @author Krzysztof Danielowski
24 # @author Piotr W√≥jcik
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_image import AddDiskForm, UploadDiskForm
34 from wi.utils import messages_ajax, get_dict_from_list
35 from wi.utils.decorators import django_view, user_permission
36 from wi.utils.messages_ajax import ajax_request
37 from wi.utils.views import prep_data
38 
39 
40 @django_view
41 @ajax_request
42 @user_permission
43 ##
44 #
45 # Ajax view for fetching disk list.
46 #
48  if request.method == 'GET':
49  rest_data = prep_data({'disks': 'user/storage_image/get_list/',
50  'disk_controllers': 'user/storage_image/get_disk_controllers/'}, request.session)
51 
52  for item in rest_data['disks']:
53  item['size'] = filesizeformatmb(item['size'])
54  item['bus'] = get_dict_from_list(rest_data['disk_controllers'], item['disk_controller'])['name']
55  return messages_ajax.success(rest_data['disks'])
56 
57 
58 @django_view
59 @ajax_request
60 @user_permission
61 @csrf_protect
62 ##
63 #
64 # Ajax view for adding new disk.
65 #
66 def res_ajax_add_disk(request, template_name='resources/ajax/add_disk.html', form_class=AddDiskForm):
67  rest_data = prep_data({'quota': 'user/user/get_quota/',
68  'supported_filesystems': 'user/storage_image/get_filesystems/',
69  'disk_controllers': 'user/storage_image/get_disk_controllers/'}, request.session)
70 
71  quota = rest_data['quota']
72 
73  if request.method == 'POST':
74  form = form_class(request.POST, rest_data=rest_data)
75  if form.is_valid():
76  dictionary = form.cleaned_data
77 
78  prep_data({'key': ('user/storage_image/create/', dictionary)}, request.session)
79  return messages_ajax.success(_("Disk is being created."))
80  else:
81  form = form_class(rest_data=rest_data)
82 
83  return messages_ajax.success(render_to_string(template_name, {'form': form,
84  'free': quota['storage'] - quota['used_storage']},
85  context_instance=RequestContext(request)),
86  status=1)
87 
88 
89 @django_view
90 @ajax_request
91 @user_permission
92 @csrf_protect
93 ##
94 #
95 # Ajax view for uploading image from a http link.
96 #
97 def res_ajax_upload_disk_http(request, template_name='generic/form.html', form_class=UploadDiskForm):
98  rest_data = prep_data({'disk_controllers': 'user/storage_image/get_disk_controllers/'}, request.session)
99 
100  if request.method == 'POST':
101  form = form_class(data=request.POST, rest_data=rest_data)
102  if form.is_valid():
103  dictionary = form.cleaned_data
104 
105  prep_data({'images': ('user/storage_image/download/', dictionary)}, request.session)
106  return messages_ajax.success(_("Disk upload started."))
107  else:
108  form = form_class(rest_data=rest_data)
109  return messages_ajax.success(render_to_string(template_name, {'form': form,
110  'confirmation': _('Upload disk'),
111  'text': _('Only RAW image format is supported at the moment. Please specify disk parameters:')},
112  context_instance=RequestContext(request)),
113  status=1)
114