def res_ajax_upload_iso_http(request, template_name='generic/form.html', form_class=UploadISOForm): """ Ajax view for uploading iso image from a http link. """ rest_data = prep_data( {'disk_controllers': 'user/iso_image/get_disk_controllers/'}, request.session) if request.method == 'POST': form = form_class(data=request.POST, rest_data=rest_data) if form.is_valid(): dictionary = form.cleaned_data prep_data({'images': ('user/iso_image/download/', dictionary)}, request.session) return messages_ajax.success(_("ISO image upload started.")) else: form = form_class(rest_data=rest_data) return messages_ajax.success(render_to_string( template_name, { 'form': form, 'confirmation': _('Upload ISO image'), 'text': _('Please specify ISO image parameters:') }, context_instance=RequestContext(request)), status=1)
def res_ajax_add_disk(request, template_name='resources/ajax/add_disk.html', form_class=AddDiskForm): """ Ajax view for adding new disk. """ rest_data = prep_data( { 'quota': 'user/user/check_quota/', 'supported_filesystems': 'user/storage_image/get_filesystems/', 'disk_controllers': 'user/storage_image/get_disk_controllers/' }, request.session) quota = rest_data['quota'] if request.method == 'POST': form = form_class(request.POST, rest_data=rest_data) if form.is_valid(): dictionary = form.cleaned_data prep_data({'key': ('user/storage_image/create/', dictionary)}, request.session) return messages_ajax.success(_("Disk is being created.")) else: form = form_class(rest_data=rest_data) return messages_ajax.success(render_to_string( template_name, { 'form': form, 'free': quota['storage'] - quota['used_storage'] }, context_instance=RequestContext(request)), status=1)
def mai_ajax_edit_news(request, id1, template_name='generic/form.html', form_class=NewsForm): """ Ajax view for editing a news entry. """ if request.method == 'POST': form = form_class(data=request.POST) if form.is_valid(): dictionary = form.cleaned_data dictionary.update({'news_id': id1}) prep_data(('admin_clm/news/edit/', dictionary), request.session) return messages_ajax.success(_('News entry edited.')) else: rest_data = prep_data(('admin_clm/news/get_by_id/', { 'news_id': id1 }), request.session) rest_data['sticky'] = rest_data['sticky'] != 0 form = form_class(rest_data) return messages_ajax.success(render_to_string( template_name, { 'form': form, 'text': _('Edit news data:'), 'confirmation': _('Save'), 'id': id1 }, context_instance=RequestContext(request)), status=1)
def generic_multiple_id(request, template_name=None, success_msg=lambda x: _('Success'), ask_msg=lambda x, y: _('Do you want to?'), confirmation=_('Yes'), request_url=None, id_key=None): """ Generic ajax view for handling dialogs working on multiple items. """ if request.method == 'POST': id_list = request.POST.getlist('ids[]') if id_key is None: raise Exception('\'id_key\' not set in urls') if request_url is None: raise Exception("No 'request_url' specified.") response = prep_data((request_url, {id_key: [int(a) for a in id_list]}), request.session) if response is None: return messages_ajax.success(success_msg(request.POST.get('desc'), int(request.POST.get('length')))) return _multiple_id_return(response, id_list, request.POST.get('desc'), success_msg) else: if request.GET.get('length') is None: return messages_ajax.error(_("Bad argument list")) return messages_ajax.success(render_to_string(template_name, {'text': ask_msg(force_escape(request.GET.get('desc')), int(request.GET.get('length'))), 'confirmation': confirmation}, context_instance=RequestContext(request)))
def cma_vms_ajax_vm_details(request, vm_id, template_name='admin_cm/ajax/vm_details.html'): """ Ajax view for fetching VM details. """ if request.method == 'POST': vm = prep_data(('admin_cm/vm/get_by_id/', { 'vm_id': vm_id }), request.session) rest_data2 = prep_data( {'user': ('admin_cm/user/get_by_id/', { 'user_id': vm['user_id'] })}, request.session) owner = parse_user(rest_data2['user']) if vm['state'] == vm_states['closed']: return messages_ajax.success('', status=1) vm['uptime'] = time_from_sec(vm['uptime']) return messages_ajax.success( render_to_string(template_name, { 'vm_id': vm_id, 'item': vm, 'states_reversed': vm_states_reversed, 'states': vm_states, 'owner': owner }, context_instance=RequestContext(request)))
def vms_ajax_assign_disk(request, vm_id, template_name='vms/ajax/assign_disk.html', form_class=AssignDiskForm): """ Ajax view for assigning Disk to a virtual machine. """ rest_data = prep_data({'disks': 'user/storage_image/get_list/', 'disk_controllers': 'user/storage_image/get_disk_controllers/' }, request.session) live_attach = [] for item in rest_data['disk_controllers']: if item['live_attach'] == True: live_attach.append(item['id']) disks_list = [] for item in rest_data['disks']: if item['disk_controller'] in live_attach: disks_list.append(item) rest_data['disks'] = disks_list if request.method == 'POST': form = form_class(data=request.POST, rest_data=rest_data) if form.is_valid(): dictionary = form.cleaned_data dictionary.update({'vm_id': vm_id}) prep_data(('user/storage_image/attach/', dictionary), request.session) return messages_ajax.success(_('Disk has been assigned.')) else: form = form_class(rest_data=rest_data) return messages_ajax.success(render_to_string(template_name, {'form': form, 'confirmation': _('Assign')}, context_instance=RequestContext(request)), status=1)
def cma_ajax_change_quota(request, template_name='generic/form.html', form_class=MultipleChangeQuotaForm): """ Ajax view for changing users quota. """ if request.method == 'POST': form = form_class(data=request.POST) user_list = request.POST.getlist('userids[]') if form.is_valid(): dictionary = {'user_ids': [int(a) for a in user_list]} for value, key in form.cleaned_data.items(): if key is not None: dictionary[value] = key prep_data(('admin_cm/user/multiple_change_quota/', dictionary), request.session) return messages_ajax.success( _('You have successfully changed quota.')) else: form = form_class() return messages_ajax.success( render_to_string(template_name, { 'form': form, 'confirmation': _('Change'), 'text': '' }, context_instance=RequestContext(request)))
def acc_ajax_account_data_edit(request, template_name='generic/form.html', form_class=AccountDataEdit): """ Ajax view for user account data editing. """ rest_data = prep_data({'cms': 'guest/cluster/list_names/'}, request.session) if request.method == 'POST': form = form_class(data=request.POST, rest_data=rest_data) if form.is_valid(): prep_data({'user': ('user/user/edit/', form.cleaned_data)}, request.session) request.session['user'].email = form.cleaned_data['email'] request.session['user'].default_cluster_id = form.cleaned_data['default_cluster_id'] request.session.modified = True return messages_ajax.success(_('Account data edited.')) else: form = form_class(data={'email': request.session['user'].email, 'default_cluster_id': request.session['user'].default_cluster_id}, rest_data=rest_data) return messages_ajax.success(render_to_string(template_name, {'form': form, 'text': '', 'confirmation': _('Save')}, context_instance=RequestContext(request)), status=1)
def simple_generic_twoid(request, id1, id2, template_name=None, success_msg=lambda desc: _('Success') % {'desc': desc}, ask_msg=lambda x: '', confirmation=_('Yes'), request_url=None, param=None, id_key=None, id_key2=None): """ Simple generic ajax view for creating dialogs (2 entities). """ if request.method == 'POST': if request_url is None: raise Exception("No 'request_url' specified.") args = {} if id1 is not None: if id_key is None: raise Exception('\'id_key\' not set in urls') args[id_key] = int(id1) if id2 is not None: if id_key2 is None: raise Exception('\'id_key2\' not set in urls') args[id_key2] = int(id2) if param is not None: args.update(param) prep_data((request_url, args), request.session) return messages_ajax.success(success_msg(force_escape(request.REQUEST.get('desc')))) if template_name is not None: return messages_ajax.success(render_to_string(template_name, {'text': ask_msg(force_escape(request.REQUEST.get('desc'))), 'confirmation': confirmation, 'id': id1}, context_instance=RequestContext(request)))
def cma_ajax_change_cm_password( request, template_name='admin_cm/ajax/change_password.html', form_class=PasswordForm): """ Ajax view for changing logged CM admin password. """ if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): prep_data(('admin_cm/user/change_password/', { 'new_password': form.cleaned_data['new_password'] }), request.session) request.session['user'].cm_password = form.cleaned_data[ 'new_password'] request.session.modified = True return messages_ajax.success( _('You have successfully changed your CM password.')) else: form = form_class() return messages_ajax.success(render_to_string( template_name, {'form': form}, context_instance=RequestContext(request)), status=1)
def cma_ajax_copy_iso(request, id1, template_name='generic/form.html', form_class=CopyToUserForm): """ Ajax view for changing the image owner. """ rest_data = prep_data({'users': 'admin_cm/user/get_list/'}, request.session) if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES, rest_data=rest_data) if form.is_valid(): dictionary = form.cleaned_data dictionary['src_image_id'] = int(id1) dictionary['dest_user_id'] = int(dictionary['dest_user_id']) prep_data(('admin_cm/iso_image/copy/', dictionary), request.session) return messages_ajax.success( _("<b>%(desc)s</b> copied.") % {'desc': force_escape(request.REQUEST.get('desc'))}) else: form = form_class(rest_data=rest_data) return messages_ajax.success(render_to_string( template_name, { 'form': form, 'confirmation': _('Copy'), 'text': _('Select user:') }, context_instance=RequestContext(request)), status=1)
def clm_ajax_set_password(request, id1, template_name='admin_clm/ajax/set_password.html', form_class=PasswordForm): """ Ajax view for setting user's password. """ id1 = int(id1) if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): new_password = form.cleaned_data['new_password'] prep_data(('admin_clm/user/set_password/', { 'user_id': id1, 'new_password': new_password }), request.session) if id1 == request.session['user'].user_id: request.session['user'].set_password(new_password) request.session.modified = True return messages_ajax.success( _('You have successfully set password for user <b>%(desc)s</b>.' ) % {'desc': (force_escape(request.POST.get('desc')))}) else: form = form_class() return messages_ajax.success(render_to_string( template_name, { 'form': form, 'text': _('Setting password for user <b>%(desc)s</b>:') % { 'desc': (force_escape(request.REQUEST.get('desc'))) } }, context_instance=RequestContext(request)), status=1)
def img_ajax_add_image_http(request, template_name="generic/form.html", form_class=AddImageHttp): """ Ajax view for handling adding image from http link. """ rest_data = prep_data( { "disk_controllers": "user/system_image/get_disk_controllers/", "video_devices": "user/system_image/get_video_devices/", "network_devices": "user/system_image/get_network_devices/", }, request.session, ) if request.method == "POST": form = form_class(data=request.POST, files=request.FILES, rest_data=rest_data) if form.is_valid(): dictionary = form.cleaned_data prep_data(("user/system_image/download/", dictionary), request.session) return messages_ajax.success(_("Image upload started.")) else: form = form_class(rest_data=rest_data) return messages_ajax.success( render_to_string( template_name, {"form": form, "confirmation": _("Add image"), "text": _("Please specify image parameters:")}, context_instance=RequestContext(request), ), status=1, )
def img_ajax_add_image_http(request, template_name='generic/form.html', form_class=AddImageHttp): """ Ajax view for handling adding image from http link. """ rest_data = prep_data( { 'disk_controllers': 'user/system_image/get_disk_controllers/', 'video_devices': 'user/system_image/get_video_devices/', 'network_devices': 'user/system_image/get_network_devices/' }, request.session) if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES, rest_data=rest_data) if form.is_valid(): dictionary = form.cleaned_data prep_data(('user/system_image/download/', dictionary), request.session) return messages_ajax.success(_('Image upload started.')) else: form = form_class(rest_data=rest_data) return messages_ajax.success(render_to_string( template_name, { 'form': form, 'confirmation': _('Add image'), 'text': _('Please specify image parameters:') }, context_instance=RequestContext(request)), status=1)
def grp_ajax_send_request(request, group_id, template_name='generic/simple.html'): """ Ajax view for sending group request membership. """ if request.method == 'POST': prep_data(('user/group/join_request/', {'group_id': group_id}), request.session) return messages_ajax.success(_('You have successfully sent a request.')) else: return messages_ajax.success(render_to_string(template_name, {'confirmation': _('Yes'), 'text': _('Do you want to send a request?')}, context_instance=RequestContext(request)))
def far_ajax_get_table(request): """ Ajax view for fetching farm list. """ if request.method == 'GET': rest_data = prep_data('user/farm/get_list/', request.session) for item in rest_data: item['uptime'] = time_from_sec(item['uptime']) item['stateName'] = unicode(farm_states[item['state']]) item['stateTooltip'] = unicode(farm_states_help[item['state']]) for vm in item['vms']: vm['stateName'] = unicode(vm_states_reversed[vm['state']]) vm['pub_ip'] = [] for i in vm['leases']: if i['public_ip'] != "": vm['pub_ip'].append(i['public_ip']['address']) vm['priv_ip'] = [] for i in vm['leases']: vm['priv_ip'].append(i['address']) vm['cpuLoadPercent'] = int(min(float(vm['cpu_load'].get('60') or 0) * 100, 100)) vm['cpuLoadColor'] = "#%02x%02x%02x" % tuple(i * 255 for i in hsv_to_rgb(float(vm['cpuLoadPercent']) / 300, 1.0, 0.8)) return messages_ajax.success(rest_data)
def img_ajax_get_group_table(request): """ Ajax view for fetching group images list. """ if request.method == 'GET': rest_data = prep_data( { 'own_groups': 'user/group/list_own_groups/', 'group_images': ('user/system_image/get_list/', { 'access': image_access['group'] }), }, request.session) leader = parsing.parse_own_groups(rest_data) group_list = [] for group in rest_data['group_images']: group_images_list = [] for item in group['images']: item['stateName'] = unicode( image_states_reversed[item['state']]) item['size'] = filesizeformatmb(item['size']) item['mine'] = True if item['user_id'] == request.session[ 'user'].user_id else False item['myGroup'] = True if item['group_id'] in leader else False group_images_list.append(item) group_list.append({ 'name': group['name'], 'items': group_images_list, 'group_id': group['group_id'] }) return messages_ajax.success(group_list)
def cma_ajax_get_table_images(request): """ Ajax view for fetching images list. """ if request.method == 'GET': rest_data = prep_data({'images_public': ('admin_cm/system_image/get_list/', {'access': image_access['public']}), 'images_private': ('admin_cm/system_image/get_list/', {'access': image_access['private']}), 'images_group': ('admin_cm/system_image/get_list/', {'access': image_access['group']})}, request.session) for item in rest_data['images_public']: item['size'] = unicode(filesizeformatmb(item['size'])) item['type'] = 'public' item['stateName'] = image_states_reversed[item['state']] list_images = [{'name': unicode(_('Public:')), 'items': rest_data['images_public']}] for item in rest_data['images_private']: item['size'] = unicode(filesizeformatmb(item['size'])) item['type'] = 'private' item['stateName'] = image_states_reversed[item['state']] list_images.append({'name': unicode(_('Private:')), 'items': rest_data['images_private']}) for group in rest_data['images_group']: for item in group['images']: item['size'] = unicode(filesizeformatmb(item['size'])) item['type'] = 'group' item['stateName'] = image_states_reversed[item['state']] list_images.append({'name': unicode(_('Group:')) + ' ' + group['name'], 'items': group['images']}) return messages_ajax.success(list_images)
def mai_ajax_get_news_table(request): """ Ajax view for fetching news list (whole). """ if request.method == 'GET': response = prep_data('guest/news/get_list/', request.session) return messages_ajax.success(response)
def res_ajax_generate_key(request, template_name='generic/form.html', form_class=GenerateKeyForm): """ Ajax view for ssh key generation. """ if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): rest_data = prep_data( { 'key': ('user/key/generate/', { 'name': form.cleaned_data['name'] }) }, request.session) return messages_ajax.success_with_key( _("You have successfully generated a key"), rest_data['key'], form.cleaned_data['name']) else: form = form_class() return messages_ajax.success(render_to_string(template_name, { 'form': form, 'confirmation': _('Generate'), 'text': _('2048-bit RSA key<br /><b>Note!</b> We don\'t keep a copy of your private key. We can\'t recreate it if your key is lost.' ) }, context_instance= RequestContext(request)), status=1)
def cma_ajax_get_cm_data(request): """ Ajax view for fetching CM data (quotas etc.). """ if request.method == 'GET': rest_data = prep_data({'cm_data': 'admin_cm/cluster/get_data/', 'nodes': 'admin_cm/node/get_list/' }, request.session) data = rest_data['cm_data'] total_cpu = 0 total_mem = 0 free_cpu = 0 free_mem = 0 for node in rest_data['nodes']: node['stateName'] = unicode(node_states[node['state']]) if node['state'] == 1: free_cpu += node['cpu_free'] free_mem += node['memory_free'] total_cpu += node['cpu_total'] total_mem += node['memory_total'] data['free_cpu'] = free_cpu data['free_mem'] = filesizeformatmb(free_mem) data['total_cpu'] = total_cpu data['total_mem'] = filesizeformatmb(total_mem) data['stateName'] = unicode(cm_states[data['state']]) return messages_ajax.success(data)
def far_ajax_get_table(request): """ Ajax view for fetching farm list. """ if request.method == 'GET': rest_data = prep_data('user/farm/get_list/', request.session) for item in rest_data: item['uptime'] = time_from_sec(item['uptime']) item['stateName'] = unicode(farm_states[item['state']]) item['stateTooltip'] = unicode(farm_states_help[item['state']]) for vm in item['vms']: vm['stateName'] = unicode(vm_states_reversed[vm['state']]) vm['pub_ip'] = [] for i in vm['leases']: if i['public_ip'] != "": vm['pub_ip'].append(i['public_ip']['address']) vm['priv_ip'] = [] for i in vm['leases']: vm['priv_ip'].append(i['address']) vm['cpuLoadPercent'] = int( min(float(vm['cpu_load'].get('60') or 0) * 100, 100)) vm['cpuLoadColor'] = "#%02x%02x%02x" % tuple( i * 255 for i in hsv_to_rgb( float(vm['cpuLoadPercent']) / 300, 1.0, 0.8)) return messages_ajax.success(rest_data)
def cma_ajax_get_cm_data(request): """ Ajax view for fetching CM data (quotas etc.). """ if request.method == "GET": rest_data = prep_data( {"cm_data": "admin_cm/cluster/get_data/", "nodes": "admin_cm/node/get_list/"}, request.session ) data = rest_data["cm_data"] total_cpu = 0 total_mem = 0 free_cpu = 0 free_mem = 0 for node in rest_data["nodes"]: node["stateName"] = unicode(node_states[node["state"]]) if node["state"] == 1: free_cpu += node["cpu_free"] free_mem += node["memory_free"] total_cpu += node["cpu_total"] total_mem += node["memory_total"] data["free_cpu"] = free_cpu data["free_mem"] = filesizeformatmb(free_mem) data["total_cpu"] = total_cpu data["total_mem"] = filesizeformatmb(total_mem) data["stateName"] = unicode(cm_states[data["state"]]) return messages_ajax.success(data)
def cma_networks_ajax_get_table(request, user_id): """ Ajax view returning network list. """ if request.method == 'GET': networks = prep_data(('admin_cm/network/list_user_networks/', {'user_id': int(user_id)}), request.session) return messages_ajax.success(networks)
def cma_farms_ajax_get_table(request, user_id): """ Ajax view for fetching farms list. """ if request.method == 'GET': farms = prep_data(('admin_cm/farm/get_list/', {'user_id': int(user_id)}), request.session) for farm in farms: farm['stateName'] = unicode(farm_states_reversed[farm['state']]) for vm in farm['vms']: vm['pub_ip'] = [] for i in vm['leases']: if i['public_ip'] != "": vm['pub_ip'].append(i['public_ip']['address']) vm['priv_ip'] = [] for i in vm['leases']: vm['priv_ip'].append(i['address']) farm['stringIP'] = ', '.join(farm['vms'][0]['priv_ip']) farm['stringPubIP'] = ', '.join(farm['vms'][0]['pub_ip']) farm['stringDisk'] = ', '.join([disk['name'] for disk in farm['vms'][0]['storage_images']]) farm['stringISO'] = ', '.join([iso['name'] for iso in farm['vms'][0]['iso_images']]) return messages_ajax.success(farms)
def img_ajax_get_group_table(request): """ Ajax view for fetching group images list. """ if request.method == "GET": rest_data = prep_data( { "own_groups": "user/group/list_own_groups/", "group_images": ("user/system_image/get_list/", {"access": image_access["group"]}), }, request.session, ) leader = parsing.parse_own_groups(rest_data) group_list = [] for group in rest_data["group_images"]: group_images_list = [] for item in group["images"]: item["stateName"] = unicode(image_states_reversed[item["state"]]) item["size"] = filesizeformatmb(item["size"]) item["mine"] = True if item["user_id"] == request.session["user"].user_id else False item["myGroup"] = True if item["group_id"] in leader else False group_images_list.append(item) group_list.append({"name": group["name"], "items": group_images_list, "group_id": group["group_id"]}) return messages_ajax.success(group_list)
def grp_ajax_grp_details_table(request, group_id): """ Ajax view for fetching group details. """ if request.method == 'GET': user = request.session['user'] rest_data = prep_data({'group_data': ('user/group/get_by_id/', {'group_id': group_id}), 'members': ('user/group/list_members/', {'group_id': group_id}), 'requests': ('user/group/list_requests/', {'group_id': group_id}) }, request.session) leader_id = rest_data['group_data'].get('leader_id') leader = True if leader_id == '': leader = False else: if int(user.user_id) != int(leader_id): leader = False for item in rest_data['members']: item['request'] = False item['leader'] = leader item['self'] = True if int(item['user_id']) == int(user.user_id) else False item['group_id'] = group_id for item in rest_data['requests']: item['request'] = True item['leader'] = leader item['self'] = True if int(item['user_id']) == int(user.user_id) else False item['group_id'] = group_id return messages_ajax.success([{'name':_('Members:'), 'items': rest_data['members']}, {'name':_('Requests:'), 'items': rest_data['requests']}])
def cma_ajax_get_cm_data(request): """ Ajax view for fetching CM data (quotas etc.). """ if request.method == 'GET': rest_data = prep_data( { 'cm_data': 'admin_cm/cluster/get_data/', 'nodes': 'admin_cm/node/get_list/' }, request.session) data = rest_data['cm_data'] total_cpu = 0 total_mem = 0 free_cpu = 0 free_mem = 0 for node in rest_data['nodes']: node['stateName'] = unicode(node_states[node['state']]) if node['state'] == 1: free_cpu += node['cpu_free'] free_mem += node['memory_free'] total_cpu += node['cpu_total'] total_mem += node['memory_total'] data['free_cpu'] = free_cpu data['free_mem'] = filesizeformatmb(free_mem) data['total_cpu'] = total_cpu data['total_mem'] = filesizeformatmb(total_mem) data['stateName'] = unicode(cm_states[data['state']]) return messages_ajax.success(data)
def form_generic_id(request, id1, template_name=None, form_class=None, request_url_post=None, request_url_get=None, success_msg=lambda desc: _('Success'), ask_msg=lambda x: '', confirmation=_('Yes'), request_url_both=None, ajax_success_status=0, id_key=None): """ Generic ajax view for dialog handling. """ rest_data1 = prep_data(request_url_both, request.session) if request.method == 'POST': kwargs = {} if rest_data1 is not None: kwargs['rest_data'] = rest_data1 form = form_class(request.POST, **kwargs) if form.is_valid(): args = {} if id1 is not None: if id_key is None: raise Exception('\'id_key\' not set in urls') args[id_key] = int(id1) args.update(form.cleaned_data) rest_data2 = prep_data((request_url_post, args), request.session) return messages_ajax.success(success_msg(force_escape(request.REQUEST.get('desc')), rest_data2), status=ajax_success_status) else: args = [] kwargs = {} if request_url_get is not None and id1 is not None: response = prep_data((request_url_get, {id_key: id1}), request.session) args.append(response) if rest_data1 is not None: kwargs['rest_data'] = rest_data1 form = form_class(*args, **kwargs) return messages_ajax.success(render_to_string(template_name, {'form': form, 'text': ask_msg(force_escape(request.REQUEST.get('desc'))), 'confirmation': confirmation, 'id': id1}, context_instance=RequestContext(request)), status=1)
def _multiple_id_return(response, id_list, desc, success_msg): """ Helper function creating ajax response with error codes. """ all_ok = True for machine_response in response: if machine_response['status'] != 'ok': all_ok = False if all_ok: count = len(id_list) return messages_ajax.success(success_msg(desc, count)) else: for i in range(len(response)): response[i]['type'] = 'vm' response[i]['vmid'] = id_list[i] response[i]['status_text'] = unicode(get_error(response[i]['status'])) return messages_ajax.success(response, 7999)
def simple_generic_twoid( request, id1, id2, template_name=None, success_msg=lambda desc: _('Success') % {'desc': desc}, ask_msg=lambda x: '', confirmation=_('Yes'), request_url=None, param=None, id_key=None, id_key2=None): """ Simple generic ajax view for creating dialogs (2 entities). """ if request.method == 'POST': if request_url is None: raise Exception("No 'request_url' specified.") args = {} if id1 is not None: if id_key is None: raise Exception('\'id_key\' not set in urls') args[id_key] = int(id1) if id2 is not None: if id_key2 is None: raise Exception('\'id_key2\' not set in urls') args[id_key2] = int(id2) if param is not None: args.update(param) prep_data((request_url, args), request.session) return messages_ajax.success( success_msg(force_escape(request.REQUEST.get('desc')))) if template_name is not None: return messages_ajax.success( render_to_string( template_name, { 'text': ask_msg(force_escape(request.REQUEST.get('desc'))), 'confirmation': confirmation, 'id': id1 }, context_instance=RequestContext(request)))
def cma_ajax_change_cm_password(request, template_name='admin_cm/ajax/change_password.html', form_class=PasswordForm): """ Ajax view for changing logged CM admin password. """ if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): prep_data(('admin_cm/user/change_password/', {'new_password': form.cleaned_data['new_password']}), request.session) request.session['user'].cm_password = form.cleaned_data['new_password'] request.session.modified = True return messages_ajax.success(_('You have successfully changed your CM password.')) else: form = form_class() return messages_ajax.success(render_to_string(template_name, {'form': form}, context_instance=RequestContext(request)), status=1)
def _multiple_id_return(response, id_list, desc, success_msg): """ Helper function creating ajax response with error codes. """ all_ok = True for machine_response in response: if machine_response['status'] != 'ok': all_ok = False if all_ok: count = len(id_list) return messages_ajax.success(success_msg(desc, count)) else: for i in range(len(response)): response[i]['type'] = 'vm' response[i]['vmid'] = id_list[i] response[i]['status_text'] = unicode( get_error(response[i]['status'])) return messages_ajax.success(response, 7999)
def mai_ajax_get_main_news_table(request): """ Ajax view for fetching news list (first 3). """ if request.method == 'GET': response = prep_data('guest/news/get_list/', request.session) sticky_news = [x for x in response if x['sticky'] == 1][:3] return messages_ajax.success(sticky_news)
def cma_networks_ajax_get_table(request, user_id): """ Ajax view returning network list. """ if request.method == 'GET': networks = prep_data(('admin_cm/network/list_user_networks/', { 'user_id': int(user_id) }), request.session) return messages_ajax.success(networks)
def cma_networks_ajax_network_details(request, network_id, template_name='admin_cm/ajax/network_details.html'): """ Ajax view fetching network details. """ if request.method == 'POST': net = prep_data(('admin_cm/network/list_leases/', {'network_id': network_id}), request.session) return messages_ajax.success(render_to_string(template_name, {'id': int(network_id), 'item': net}, context_instance=RequestContext(request)))
def vms_ajax_vm_details(request, vm_id, template_name='vms/ajax/vm_details.html'): """ Ajax view fetching virtual machine details. """ if request.method == 'POST': vm = prep_data(('user/vm/get_by_id/', {'vm_id': vm_id}), request.session) if vm['state'] == vm_states['closed']: return messages_ajax.success('', status=1) vm['uptime'] = time_from_sec(vm['uptime']) return messages_ajax.success( render_to_string(template_name, {'vm_id': vm_id, 'item': vm, 'states_reversed': vm_states_reversed, 'states': vm_states}, context_instance=RequestContext(request)))
def res_ajax_get_iso_table(request): """ Ajax view returning iso images list. """ if request.method == 'GET': rest_data = prep_data('user/iso_image/get_list/', request.session) for item in rest_data: item['size'] = filesizeformatmb(item['size']) return messages_ajax.success(rest_data)
def generic_multiple_id_form(request, template_name=None, success_msg=lambda x: _('Success'), ask_msg=lambda x, y: _('Do you want to?'), confirmation=_('Yes'), request_url=None, form_class=None, request_url_both=None, id_key=None): """ Generic ajax view for handling dialogs working on multiple items (with a form). """ response1 = prep_data(request_url_both, request.session) if request.method == 'POST': if id_key is None: raise Exception('\'id_key\' not set in urls') if request_url is None: raise Exception("No 'request_url' specified.") kwargs = {} if response1 is not None: kwargs['rest_data'] = response1 form = form_class(request.POST, **kwargs) if form.is_valid(): id_list = [int(a) for a in request.POST.getlist('ids[]')] dictionary = {id_key: id_list} dictionary.update(form.cleaned_data) response2 = prep_data((request_url, dictionary), request.session) return _multiple_id_return(response2, id_list, request.POST.get('desc'), success_msg) else: args = [] kwargs = {} if response1 is not None: kwargs['rest_data'] = response1 form = form_class(*args, **kwargs) return messages_ajax.success(render_to_string( template_name, { 'form': form, 'text': ask_msg(force_escape(request.REQUEST.get('desc')), int(request.REQUEST.get('length'))), 'confirmation': confirmation }, context_instance=RequestContext(request)), status=1)
def vms_ajax_revoke_ip(request, vm_id, template_name='generic/form.html', form_class=RevokeIPForm): """ Ajax view for detaching elastip IP from a virtual machine. """ rest_data = prep_data({'vm': ('user/vm/get_by_id/', {'vm_id': vm_id})}, request.session) if request.method == 'POST': form = form_class(data=request.POST, rest_data=rest_data) if form.is_valid(): prep_data(('user/public_ip/unassign/', {'public_ip_id': form.cleaned_data['public_ip_id']}), request.session) return messages_ajax.success(_('You have successfully revoked IP address.')) else: form = form_class(rest_data=rest_data) return messages_ajax.success(render_to_string(template_name, {'form': form, 'text': _('Select an IP address to revoke:'), 'confirmation': _('Revoke')}, context_instance=RequestContext(request)), status=1)
def clm_ajax_get_table_users(request): """ Ajax view for fetching users list. """ if request.method == 'GET': users = prep_data('admin_clm/user/get_list/', request.session) for item in users: item['is_activeName'] = unicode(user_states[item['is_active']]) return messages_ajax.success(users)
def grp_ajax_get_table_all(request): """ Ajax view for fetching all group list. """ if request.method == 'GET': all_groups = prep_data('user/group/get_list/', request.session) for item in all_groups: item['user_statusName'] = unicode(user_groups_states_reversed[item['user_status']]) return messages_ajax.success(all_groups)
def cma_ajax_node_details(request, node_id, template_name='admin_cm/ajax/node_details.html'): """ Ajax view fetching node details. """ if request.method == 'POST': node = prep_data(('admin_cm/node/get_by_id_details/', {'node_id': node_id}), request.session) return messages_ajax.success(render_to_string(template_name, {'node_id': node_id, 'node': node, 'node_states': node_states }, context_instance=RequestContext(request)))
def wrap(request, *args, **kwds): """ Returned decorated function. """ if 'user' in request.session and request.session['user'].is_admin_clm: return view_func(request, *args, **kwds) if request.is_ajax(): return success(auth_error_text, status=8003) path = urlquote(request.get_full_path()) tup = login_url, REDIRECT_FIELD_NAME, path return HttpResponseRedirect('%s?%s=%s' % tup)
def get_messages(request): """ Ajax view fetching user messages. """ if request.method == 'POST': response = prep_data('user/message/get_list/', request.session) for item in response: item['text'] = get_message(item['code'], item['params']) item['level'] = message_levels_reversed[item['level']] return messages_ajax.success(response)
def cma_ajax_get_table_disks(request): """ Ajax view for fetching disks list. """ if request.method == 'GET': disks = prep_data('admin_cm/storage_image/get_list/', request.session) for item in disks: item['size'] = filesizeformatmb(item['size']) return messages_ajax.success(disks)
def cma_ajax_get_table_iso(request): """ Ajax view for fetching ISO images list. """ if request.method == 'GET': iso = prep_data('admin_cm/iso_image/get_list/', request.session) for item in iso: item['size'] = filesizeformatmb(item['size']) return messages_ajax.success(iso)
def clm_ajax_get_table_cms(request): """ Ajax view for fetching Cluster Manager list. """ if request.method == 'GET': cms = prep_data('admin_clm/cluster/get_list/', request.session) for item in cms: item['is_activeName'] = unicode(cm_states[item['state']]) return messages_ajax.success(cms)
def cma_ajax_get_pool_table(request): """ Ajax view fetching pool list. """ if request.method == 'GET': pools = prep_data('admin_cm/network/list_available_networks/', request.session) for pool in pools: pool['stateName'] = unicode(pool_states_reversed[pool['state']]) return messages_ajax.success(pools)