def dc_user_modal_form(request): """ Ajax page for updating, removing and adding user. """ qs = request.GET.copy() if request.POST['action'] == 'update': user = get_edited_user(request, request.POST['adm-username']) else: user = None form = AdminUserModalForm(request, user, request.POST, prefix='adm') if form.is_valid(): args = (form.cleaned_data['username'],) status = form.save(args=args) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): if form.action == 'create': messages.success(request, _('User was successfully created')) return redirect('dc_user_profile', username=form.cleaned_data['username']) else: messages.success(request, _('User profile was successfully updated')) user = user.__class__.objects.get(pk=user.pk) # Reload user object from DB (modified in API) # You can modify yourself and lose access to /dc - Issue #108 if request.user == user and not user.is_admin(dc=request.dc): redirect_to = '/' else: redirect_to = 'dc_user_list' return redirect(redirect_to, query_string=qs) return render(request, 'gui/dc/user_dc_form.html', {'form': form})
def dc_user_modal_form(request): """ Ajax page for updating, removing and adding user. """ qs = request.GET.copy() if request.POST['action'] == 'update': user = get_edited_user(request, request.POST['adm-username']) else: user = None form = AdminUserModalForm(request, user, request.POST, prefix='adm') if form.is_valid(): args = (form.cleaned_data['username'],) status = form.save(args=args) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): if form.action == 'create': messages.success(request, _('User was successfully created')) return redirect('dc_user_profile', username=form.cleaned_data['username']) else: messages.success(request, _('User profile was successfully updated')) return redirect('dc_user_list', query_string=qs) return render(request, 'gui/dc/user_dc_form.html', {'form': form})
def dc_user_profile_sshkey_modal_form(request, username, action): """ Ajax page for adding or deleting SSH keys. """ user = get_edited_user(request, username) if action == 'add': sform = SSHKeyForm(request, None, request.POST) if sform.is_valid(): status = sform.save(action='create', args=(user.username, sform.cleaned_data['name'])) if status == 201: messages.success(request, _('SSH key was successfully saved')) return redirect('dc_user_profile', user.username) return render(request, 'gui/profile/profile_sshkey_form.html', { 'user': user, 'sform': sform }, status=200) elif action == 'delete': res = SSHKeyForm.api_call('delete', None, request, args=(user.username, request.POST.get('name'))) status = res.status_code if status == 200: messages.success(request, _('SSH key was successfully removed')) return redirect('dc_user_profile', user.username) return render(request, 'gui/profile/profile_sshkey_list.html', { 'user': user, 'ssh_keys': user.usersshkey_set.all().order_by('id') }, status=status)
def domain_record_form(request, name): """ Ajax page for updating, removing and adding DNS records. """ domain = get_domain(request, name) form_class = DnsRecordForm if request.POST['action'] == 'update': try: record = Record.objects.get(domain=domain, id=request.POST.get('id')) except Record.DoesNotExist: raise Http404 else: record = None if request.POST.get('records', False): form_class = MultiDnsRecordForm form = form_class(request, domain, record, request.POST) if form.is_valid(): status = form.save(args=form.api_call_args(domain.name)) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): assert request.GET['flt-domain'] == domain.name return redirect('dc_domain_record_list', query_string=request.GET) return render(request, form.template, {'form': form, 'domain': domain})
def dc_dc_settings(request, dc=settings.VMS_DC_MAIN): """ Switch current datacenter and redirect to dc_settings page. """ dc_switch(request, dc) return redirect('dc_settings', query_string=request.GET)
def dc_vm_backup(request, dc, hostname): """ Switch current datacenter and redirect to VM backup page. """ dc_switch(request, dc) return redirect('vm_backup', hostname=hostname)
def admin_iso_form(request): """ Ajax page for updating, removing and adding iso images. """ qs = request.GET.copy() if request.POST['action'] == 'update': try: iso = Iso.objects.select_related( 'owner', 'dc_bound').get(name=request.POST['adm-name']) except Iso.DoesNotExist: raise Http404 else: iso = None form = AdminIsoForm(request, iso, request.POST, prefix='adm') if form.is_valid(): args = (form.cleaned_data['name'], ) status = form.save(args=args) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): if form.action == 'create' and not form.cleaned_data.get( 'dc_bound'): qs['all'] = 1 # Show all items if adding new item and not attaching return redirect('dc_iso_list', query_string=qs) return render(request, 'gui/dc/iso_admin_form.html', {'form': form})
def imagestore_update(request, repo): """ Ajax page for refreshing imagestore repositories. """ if repo not in ImageStore.get_repositories( include_image_vm=request.user.is_staff): raise Http404 res = call_api_view(request, 'PUT', imagestore_manage, repo, log_response=True) if res.status_code == 200: imagestore = res.data['result'] msg = _( 'Downloaded metadata for %(image_count)d images from image repository %(name)s' ) messages.success(request, msg % imagestore) return redirect('imagestore_list_repo', repo=repo, query_string=request.GET) else: if res.data.get('result', {}).get('error', None): status = 200 # The error will be displayed by ImageStoreList JS else: status = res.status_code return JSONResponse(res.data, status=status)
def dc_node_form(request): """ Ajax page for creating or updating Compute node <-> Datacenter associations. """ if request.POST['action'] == 'create': dc_node = None else: try: node = Node.objects.get(hostname=request.POST['hostname']) dc_node = DcNode.objects.select_related('dc', 'node').get(dc=request.dc, node=node) except (Node.DoesNotExist, DcNode.DoesNotExist): raise Http404 form = DcNodeForm(request, dc_node, request.POST) if form.is_valid(): status = form.save(args=(form.cleaned_data.get('hostname'), )) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): return redirect('dc_node_list', query_string=request.GET) return render(request, 'gui/dc/node_form.html', { 'form': form, 'mb_addon': SIZE_FIELD_MB_ADDON })
def admin_domain_form(request): """ Ajax page for updating, removing and adding DNS domains. """ qs = request.GET.copy() nodc = request.GET.get('nodc', '') if request.POST['action'] == 'create': domain = None else: domain = get_domain(request, request.POST['adm-name']) form = AdminDomainForm(request, domain, request.POST, prefix='adm') if form.is_valid(): args = (form.cleaned_data['name'], ) status = form.save(args=args) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): if form.action == 'create' and not form.cleaned_data.get( 'dc_bound'): qs['all'] = 1 # Show all items if adding new item and not attaching return redirect('dc_domain_list', query_string=qs) return render(request, 'gui/dc/domain_admin_form.html', { 'form': form, 'nodc': nodc })
def network_ip_form(request, name): """ Ajax page for updating, removing and adding network IP address(es). """ try: net = Subnet.objects.get(name=name) except Subnet.DoesNotExist: raise Http404 form_class = NetworkIPForm if request.POST['action'] == 'update': try: ip = IPAddress.objects.get(subnet=net, ip=request.POST.get('ip')) except IPAddress.DoesNotExist: raise Http404 else: ip = None if request.POST.get('ips', False): form_class = MultiNetworkIPForm form = form_class(request, net, ip, request.POST) if form.is_valid(): status = form.save(args=form.api_call_args(name)) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): messages.success(request, form.get_action_message()) return redirect('dc_network_ip_list', net.name, query_string=request.GET) return render(request, form.template, {'form': form, 'net': {'name': name}})
def dc_vm_details(request, dc, hostname): """ Switch current datacenter and redirect to VM details page. """ dc_switch(request, dc) return redirect('vm_details', hostname=hostname)
def dc_switch_form(request): """ Ajax page for changing current working datacenter. """ form = DcSwitch(request, request.POST, prefix='dc') if form.is_valid(): if form.save(): return redirect(form.get_referrer() or settings.LOGIN_REDIRECT_URL) else: return HttpResponse(None, status=204) return render(request, 'gui/dc/dc_switch_form.html', {'dcs_form': form})
def dc_user_profile_form(request, username): """ Ajax page for updating user profile. """ user = get_edited_user(request, username, sr=('dc_bound', 'userprofile')) profile = user.userprofile if request.POST['action'] == 'update': uform = AdminUserForm(request, user, request.POST) upform = AdminUserProfileForm(request, profile, request.POST) if uform.is_valid() and upform.is_valid( ): # The real validation is not happening here but below args = (uform.cleaned_data['username'], ) # The validation happens in these two forms and they inform about the result which we process. # However, if upform save fails, uform is saved already and we cannot do anything about that. # FIXME bad design here and in gui.profile.views.update ustatus = uform.save(action='update', args=args) upstatus = upform.save(action='update', args=args) if (ustatus == 200 and upstatus in (200, 204)) or (upstatus == 200 and ustatus in (200, 204)): messages.success(request, _('User profile was successfully updated')) user = user.__class__.objects.get( pk=user.pk) # Reload user object from DB (modified in API) # You can modify yourself and lose access to /dc - Issue #108 if request.user == user and not user.is_admin(dc=request.dc): redirect_to = '/' else: redirect_to = 'dc_user_list' return redirect(redirect_to) else: uform = AdminUserForm(request, user, init=True) upform = AdminUserProfileForm(request, profile, init=True) context = { 'uform': uform, 'upform': upform, 'user': user, 'profile': profile, 'ssh_keys': user.usersshkey_set.all().order_by('id'), } if not request.user.is_staff: context['disabled_api_key'] = True return render(request, 'gui/dc/profile/profile_page.html', context)
def dc_template_form(request): """ Ajax page for attaching and detaching templates. """ form = TemplateForm(request, VmTemplate.objects.all().order_by('name'), request.POST) if form.is_valid(): status = form.save(args=(form.cleaned_data['name'],)) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): return redirect('dc_template_list', query_string=request.GET) return render(request, 'gui/dc/template_form.html', {'form': form})
def dc_storage_form(request): """ Ajax page for creating or updating storages. """ nss = NodeStorage.objects.select_related(*SR).filter(dc=request.dc).order_by(*OB) form = StorageForm(request, nss, request.POST) if form.is_valid(): status = form.save(args=(form.zpool_node,)) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): return redirect('dc_storage_list', query_string=request.GET) return render(request, 'gui/dc/storage_form.html', {'form': form})
def dc_user_profile_password_modal_form(request, username): """ Ajax page for changing user password. """ user = get_edited_user(request, username) status = 200 pform = AdminChangePasswordForm(user, request.POST) if pform.is_valid(): status = pform.save(request) if status == 200: messages.success(request, _('User password was successfully changed')) return redirect('dc_user_profile', user.username) return render(request, 'gui/dc/profile/profile_password_form.html', { 'user': user, 'pform': pform, }, status=status)
def dc_settings_form(request): """ Ajax page for changing advanced datacenter settings. """ dc = get_dc_or_404(request, request.POST['dc'], api=False) form = DcSettingsForm(request, dc, request.POST) context = {'form': form} if form.is_valid(): status = form.save(args=(dc.name,)) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): _dc_settings_msg_updated(request) return redirect('dc_list', query_string=request.GET) _dc_settings_msg_error(context) return render(request, 'gui/dc/dc_settings_form.html', context)
def dc_group_form(request): """ Ajax page for attaching and detaching isos. """ if 'adm-name' in request.POST: prefix = 'adm' else: prefix = None form = DcGroupForm(request, Role.objects.all().order_by('name'), request.POST, prefix=prefix) if form.is_valid(): status = form.save(args=(form.cleaned_data['name'], )) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): return redirect('dc_group_list', query_string=request.GET) # An error occurred when attaching or detaching object if prefix: # The displayed form was an admin form, so we need to return the admin form back # But with errors from the attach/detach form try: group = Role.objects.select_related('dc_bound').get( name=request.POST['adm-name']) except Role.DoesNotExist: group = None form_admin = AdminGroupForm(request, group, request.POST, prefix=prefix) # noinspection PyProtectedMember form_admin._errors = form._errors form = form_admin template = 'gui/dc/group_admin_form.html' else: template = 'gui/dc/group_dc_form.html' return render(request, template, {'form': form})
def dc_settings_table(request): """ Ajax page for changing all datacenter settings. """ context = _dc_settings_table(request, data=request.POST) form = context['form'] if form.is_valid(): status = form.save(args=(request.dc.name,), action='update') if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): _dc_settings_msg_updated(request) return redirect('dc_settings', query_string=request.GET) _dc_settings_msg_error(context) form.set_mon_zabbix_server_login_error() return render(request, 'gui/dc/dc_settings_table.html', context)
def dc_form(request): """ Ajax page for creating or updating datacenter. """ if request.POST['action'] == 'create': dc = None else: dc = get_dc_or_404(request, request.POST['name'], api=False) form = DcForm(request, dc, request.POST) if form.is_valid(): status = form.save(args=(form.cleaned_data.get('name'),)) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): messages.success(request, _('Datacenter settings were successfully updated')) return redirect('dc_list', query_string=request.GET) return render(request, 'gui/dc/dc_form.html', {'form': form})
def admin_network_form(request): """ Ajax page for updating, removing and adding networks. """ qs = request.GET.copy() if request.POST['action'] == 'update': try: net = Subnet.objects.select_related('owner', 'dc_bound').get(name=request.POST['adm-name']) except Subnet.DoesNotExist: raise Http404 else: net = None form = AdminNetworkForm(request, net, request.POST, prefix='adm') if form.is_valid(): args = (form.cleaned_data['name'],) status = form.save(args=args) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): if form.action == 'create' and not form.cleaned_data.get('dc_bound'): qs['all'] = 1 # Show all items if adding new item and not attaching # Redirect to ip list or network list depending on ips parameter if request.GET.get('ips', False): redir_view = 'dc_network_ip_list' redir_args = (net.name,) else: redir_view = 'dc_network_list' redir_args = () return redirect(redir_view, *redir_args, query_string=qs) return render(request, 'gui/dc/network_admin_form.html', {'form': form, 'nodc': request.GET.get('ips', '')})
def dc_user_profile_form(request, username): """ Ajax page for updating user profile. """ user = get_edited_user(request, username, sr=('dc_bound', 'userprofile')) profile = user.userprofile if request.POST['action'] == 'update': uform = AdminUserForm(request, user, request.POST) upform = AdminUserProfileForm(request, profile, request.POST) if uform.is_valid() and upform.is_valid(): args = (uform.cleaned_data['username'],) ustatus = uform.save(action='update', args=args) upstatus = upform.save(action='update', args=args) if (ustatus == 200 and upstatus in (200, 204)) or (upstatus == 200 and ustatus in (200, 204)): messages.success(request, _('User profile was successfully updated')) return redirect('dc_user_list') else: uform = AdminUserForm(request, user, init=True) upform = AdminUserProfileForm(request, profile, init=True) context = { 'uform': uform, 'upform': upform, 'user': user, 'profile': profile, 'ssh_keys': user.usersshkey_set.all().order_by('id'), } if not request.user.is_staff: context['disabled_api_key'] = True return render(request, 'gui/dc/profile/profile_page.html', context)
def admin_image_form(request): """ Ajax page for updating, removing and importing server images. """ qs = request.GET.copy() if request.POST['action'] == 'update': try: img = Image.objects.select_related( 'owner', 'dc_bound').get(name=request.POST['adm-name']) except Image.DoesNotExist: raise Http404 else: img = None form = AdminImageForm(request, img, request.POST, prefix='adm') if form.is_valid(): img_name = form.cleaned_data['name'] status = form.save(args=(img_name, )) if status == 204: return HttpResponse(None, status=status) elif status in (200, 201): qs['task_id'] = form.task_id # Add task ID into last task IDs array if form.action == 'create' and not form.cleaned_data.get( 'dc_bound'): qs['all'] = 1 # Show all items if adding new item and not attaching if status == 201: qs['last_img'] = img_name return redirect('dc_image_list', query_string=qs) return render(request, 'gui/dc/image_admin_form.html', {'form': form})