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.user.system_image
22 # @author Krzysztof Danielowski
23 # @author Piotr Wójcik
24 # @date 3.1.2011
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 common.states import image_access, image_states
33 from wi.commontags.templatetags.templatetags import filesizeformatmb
34 from wi.forms.system_image import AddImageHttp
35 from wi.utils import messages_ajax, parsing
36 from wi.utils.decorators import django_view
37 from wi.utils.decorators import user_permission
38 from wi.utils.messages_ajax import ajax_request
39 from wi.utils.states import image_states_reversed
40 from wi.utils.views import prep_data
41 
42 
43 @django_view
44 @ajax_request
45 @user_permission
46 ##
47 #
48 # Ajax view for fetching user's private images list.
49 #
51  if request.method == 'GET':
52  rest_data = prep_data({'images_private': ('user/system_image/get_list/', {'access': image_access['private']}),
53  'images_group': ('user/system_image/get_list/', {'access': image_access['group']})}, request.session)
54 
55  for item in rest_data['images_private']:
56  item['stateName'] = image_states_reversed[item['state']]
57  item['type'] = 'private'
58  item['size'] = filesizeformatmb(item['size'])
59 
60  group_list = [{'name': unicode(_('Private')), 'items': rest_data['images_private']}]
61 
62  for group in rest_data['images_group']:
63  group_images_list = []
64  for item in group['images']:
65  if item['user_id'] == request.session['user'].user_id:
66  item['stateName'] = image_states_reversed[item['state']]
67  item['size'] = unicode(filesizeformatmb(item['size']))
68  item['type'] = 'group'
69  group_images_list.append(item)
70 
71  group_list.append({'name': unicode(_('Shared to group')) + ' ' + group['name'],
72  'items': group_images_list, 'group_id': group['group_id']})
73 
74  return messages_ajax.success(group_list)
75 
76 
77 @django_view
78 @ajax_request
79 @user_permission
80 ##
81 #
82 # Ajax view for fetching public images list.
83 #
85  if request.method == 'GET':
86  rest_data = prep_data(('user/system_image/get_list/', {'access': image_access['public']}), request.session)
87 
88  for item in rest_data:
89  item['stateName'] = image_states_reversed[item['state']]
90  item['size'] = filesizeformatmb(item['size'])
91 
92  return messages_ajax.success(rest_data)
93 
94 
95 @django_view
96 @ajax_request
97 @user_permission
98 ##
99 #
100 # Ajax view for fetching group images list.
101 #
103  if request.method == 'GET':
104  rest_data = prep_data({'own_groups': 'user/group/list_own_groups/',
105  'group_images': ('user/system_image/get_list/', {'access': image_access['group']})},
106  request.session)
107 
108  leader = parsing.parse_own_groups(rest_data)
109 
110  group_list = []
111  for group in rest_data['group_images']:
112 
113  group_images_list = []
114  for item in group['images']:
115  item['stateName'] = unicode(image_states_reversed[item['state']])
116  item['size'] = filesizeformatmb(item['size'])
117  item['mine'] = True if item['user_id'] == request.session['user'].user_id else False
118  item['myGroup'] = True if item['group_id'] in leader else False
119  group_images_list.append(item)
120 
121  group_list.append({'name': group['name'], 'items': group_images_list, 'group_id': group['group_id']})
122  return messages_ajax.success(group_list)
123 
124 
125 @django_view
126 @ajax_request
127 @user_permission
128 @csrf_protect
129 ##
130 #
131 # Ajax view for handling adding image from http link.
132 #
133 def img_ajax_add_image_http(request, template_name='generic/form.html', form_class=AddImageHttp):
134  rest_data = prep_data({'disk_controllers': 'user/system_image/get_disk_controllers/',
135  'video_devices': 'user/system_image/get_video_devices/',
136  'network_devices': 'user/system_image/get_network_devices/'},
137  request.session)
138 
139  if request.method == 'POST':
140  form = form_class(data=request.POST, files=request.FILES, rest_data=rest_data)
141  if form.is_valid():
142  dictionary = form.cleaned_data
143 
144  prep_data(('user/system_image/download/', dictionary), request.session)
145  return messages_ajax.success(_('Image upload started.'))
146  else:
147  form = form_class(rest_data=rest_data)
148  return messages_ajax.success(render_to_string(template_name,
149  {'form': form,
150  'confirmation': _('Add image'),
151  'text': _('Only RAW image format is supported at the moment. Please specify image parameters:')},
152  context_instance=RequestContext(request)),
153  status=1)
154 
155 
156 @django_view
157 @ajax_request
158 @user_permission
159 ##
160 #
161 # Ajax view fetching image list.
162 # all = ALL
163 # private = My images
164 # public = Public
165 # 1 ... x = Group
166 #
167 def img_ajax_get_all_table(request, img_type):
168  if request.method == 'GET':
169  rest_data = prep_data({'images_private': ('user/system_image/get_list/', {'access': image_access['private']}),
170  'images_public': ('user/system_image/get_list/', {'access': image_access['public']}),
171  'images_group': ('user/system_image/get_list/', {'access': image_access['group']})},
172  request.session)
173  list_images = []
174 
175  if img_type == 'private' or img_type == 'all':
176  for item in rest_data['images_private']:
177  if item['state'] != image_states['ok']:
178  continue
179  item['stateName'] = image_states_reversed[item['state']]
180  item['type'] = _('private')
181  item['size'] = filesizeformatmb(item['size'])
182  list_images.append(item)
183 
184  if img_type == 'public' or img_type == 'all':
185  for item in rest_data['images_public']:
186  if item['state'] != image_states['ok']:
187  continue
188  item['stateName'] = image_states_reversed[item['state']]
189  item['type'] = _('public')
190  item['size'] = filesizeformatmb(item['size'])
191  list_images.append(item)
192 
193  if img_type == 'all':
194  for group in rest_data['images_group']:
195  for item in group['images']:
196  if item['state'] != image_states['ok']:
197  continue
198  item['stateName'] = image_states_reversed[item['state']]
199  item['size'] = unicode(filesizeformatmb(item['size']))
200  item['type'] = _('group ') + group['name']
201  list_images.append(item)
202 
203  if img_type != 'public' and img_type != 'private' and img_type != 'all':
204  for group in rest_data['images_group']:
205  if (img_type == str(group['group_id'])):
206  for item in group['images']:
207  if item['state'] != image_states['ok']:
208  continue
209  item['stateName'] = image_states_reversed[item['state']]
210  item['size'] = unicode(filesizeformatmb(item['size']))
211  item['type'] = _('group ') + group['name']
212  list_images.append(item)
213 
214  if img_type == 'private':
215  for group in rest_data['images_group']:
216  for item in group['images']:
217  if img_type == 'private' and item['user_id'] == request.session['user'].user_id:
218  if item['state'] != image_states['ok']:
219  continue
220  item['stateName'] = image_states_reversed[item['state']]
221  item['size'] = unicode(filesizeformatmb(item['size']))
222  item['type'] = _('group ') + group['name']
223  list_images.append(item)
224 
225  return messages_ajax.success(list_images)
226