cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
system_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.admin_cm.system_image
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.defaultfilters import force_escape
29 from django.template.loader import render_to_string
30 from django.utils.translation import ugettext as _
31 from django.views.decorators.csrf import csrf_protect
32 
33 from common.states import image_access
34 from wi.commontags.templatetags.templatetags import filesizeformatmb
35 from wi.forms.system_image import AddImageHttp
36 from wi.forms.user import CopyToUserForm
37 from wi.utils import messages_ajax
38 from wi.utils.decorators import admin_cm_permission
39 from wi.utils.decorators import django_view
40 from wi.utils.messages_ajax import ajax_request
41 from wi.utils.states import image_states_reversed
42 from wi.utils.views import prep_data
43 
44 
45 @django_view
46 @ajax_request
47 @admin_cm_permission
48 ##
49 #
50 # Ajax view for fetching images list.
51 #
53  if request.method == 'GET':
54  rest_data = prep_data({'images_public': ('admin_cm/system_image/get_list/', {'access': image_access['public']}),
55  'images_private': ('admin_cm/system_image/get_list/', {'access': image_access['private']}),
56  'images_group': ('admin_cm/system_image/get_list/', {'access': image_access['group']})}, request.session)
57 
58  for item in rest_data['images_public']:
59  item['size'] = unicode(filesizeformatmb(item['size']))
60  item['type'] = 'public'
61  item['stateName'] = image_states_reversed[item['state']]
62 
63  list_images = [{'name': unicode(_('Public:')), 'items': rest_data['images_public']}]
64 
65  for item in rest_data['images_private']:
66  item['size'] = unicode(filesizeformatmb(item['size']))
67  item['type'] = 'private'
68  item['stateName'] = image_states_reversed[item['state']]
69 
70  list_images.append({'name': unicode(_('Private:')), 'items': rest_data['images_private']})
71 
72  for group in rest_data['images_group']:
73  for item in group['images']:
74  item['size'] = unicode(filesizeformatmb(item['size']))
75  item['type'] = 'group'
76  item['stateName'] = image_states_reversed[item['state']]
77 
78  list_images.append({'name': unicode(_('Group:')) + ' ' + group['name'], 'items': group['images']})
79 
80  return messages_ajax.success(list_images)
81 
82 
83 @django_view
84 @ajax_request
85 @admin_cm_permission
86 @csrf_protect
87 ##
88 #
89 # Ajax view for adding an image (http link).
90 #
91 def cma_ajax_add_image(request, template_name='generic/form.html', form_class=AddImageHttp):
92  rest_data = prep_data({'disk_controllers': 'user/system_image/get_disk_controllers/',
93  'video_devices': 'user/system_image/get_video_devices/',
94  'network_devices': 'user/system_image/get_network_devices/',
95  }, request.session)
96 
97  if request.method == 'POST':
98  form = form_class(data=request.POST, rest_data=rest_data)
99  if form.is_valid():
100  dictionary = form.cleaned_data
101  prep_data({'images': ('admin_cm/system_image/download/', dictionary)}, request.session)
102 
103  return messages_ajax.success(_('You have successfully added an image.'))
104  else:
105  form = form_class(rest_data=rest_data)
106  return messages_ajax.success(render_to_string(template_name, {'form': form,
107  'confirmation': _('Create'),
108  'text': ''},
109  context_instance=RequestContext(request)),
110  status=1)
111 
112 
113 @django_view
114 @ajax_request
115 @admin_cm_permission
116 @csrf_protect
117 ##
118 #
119 # Ajax view for changing the image owner.
120 #
121 def cma_ajax_copy_image(request, id1, template_name='generic/form.html', form_class=CopyToUserForm):
122  rest_data = prep_data({'users': ('admin_cm/user/get_list/', {'short': True})}, request.session)
123 
124  if request.method == 'POST':
125  form = form_class(data=request.POST, files=request.FILES, rest_data=rest_data)
126  if form.is_valid():
127  dictionary = form.cleaned_data
128  dictionary['src_image_id'] = int(id1)
129  dictionary['dest_user_id'] = int(dictionary['dest_user_id'])
130 
131  prep_data(('admin_cm/system_image/copy/', dictionary), request.session)
132 
133  return messages_ajax.success(_("<b>%(desc)s</b> copied.") % {'desc': force_escape(request.REQUEST.get('desc'))})
134  else:
135  form = form_class(rest_data=rest_data)
136  return messages_ajax.success(render_to_string(template_name, {'form': form,
137  'confirmation': _('Copy'),
138  'text': _('Select user:')},
139  context_instance=RequestContext(request)),
140  status=1)
141