def __init__(self, cluster, node, *args, **kwargs): super(EvacuateForm, self).__init__(*args, **kwargs) node_list = [str(h) for h in cluster.nodes.exclude(pk=node.pk)\ .values_list('hostname', flat=True)] nodes = zip(node_list, node_list) nodes.insert(0, self.EMPTY_FIELD) self.fields['node'].choices = nodes defaults = cluster_default_info(cluster) if defaults['iallocator'] != '' : self.fields['iallocator'].initial = True self.fields['iallocator_hostname'].initial = defaults['iallocator']
def cluster_defaults(request): """ Ajax view for retrieving the default cluster options to be set on the NewVirtualMachineForm. """ cluster_id = request.GET.get('cluster_id', None) cluster = get_object_or_404(Cluster, id__exact=cluster_id) user = request.user if not (user.is_superuser or user.has_perm('create_vm', cluster) or user.has_perm('admin', cluster)): return render_403(request, 'You do not have permission to view the default cluster options') content = json.dumps(cluster_default_info(cluster)) return HttpResponse(content, mimetype='application/json')
def __init__(self, user, cluster=None, initial=None, *args, **kwargs): self.user = user super(NewVirtualMachineForm, self).__init__(initial, *args, **kwargs) if initial: if 'cluster' in initial and initial['cluster']: try: cluster = Cluster.objects.get(pk=initial['cluster']) except Cluster.DoesNotExist: # defer to clean function to return errors pass if cluster is not None: # set choices based on selected cluster if given oslist = cluster_os_list(cluster) nodelist = [str(h) for h in cluster.nodes.values_list('hostname', flat=True)] nodes = zip(nodelist, nodelist) nodes.insert(0, self.empty_field) oslist.insert(0, self.empty_field) self.fields['pnode'].choices = nodes self.fields['snode'].choices = nodes self.fields['os'].choices = oslist defaults = cluster_default_info(cluster) if defaults['iallocator'] != '' : self.fields['iallocator'].initial = True self.fields['iallocator_hostname'] = forms.CharField( \ initial=defaults['iallocator'], \ required=False, \ widget = forms.HiddenInput()) self.fields['vcpus'].initial = defaults['vcpus'] self.fields['memory'].initial = defaults['memory'] self.fields['disk_type'].initial = defaults['disk_type'] self.fields['root_path'].initial = defaults['root_path'] self.fields['kernel_path'].initial = defaults['kernel_path'] self.fields['serial_console'].initial = defaults['serial_console'] self.fields['nic_link'].initial = defaults['nic_link'] # set cluster choices based on the given owner if initial and 'owner' in initial and initial['owner']: try: self.owner = ClusterUser.objects.get(pk=initial['owner']).cast() except ClusterUser.DoesNotExist: self.owner = None else: self.owner = None # Set up owner and cluster choices. if user.is_superuser: # Superusers may do whatever they like. self.fields['owner'].queryset = ClusterUser.objects.all() self.fields['cluster'].queryset = Cluster.objects.all() else: # Fill out owner choices. Remember, the list of owners is a list # of tuple(ClusterUser.id, label). If you put ids from other # Models into this, no magical correction will be applied and you # will assign permissions to the wrong owner; see #2007. owners = [(u'', u'---------')] for group in user.groups.all(): owners.append((group.organization.id, group.name)) if user.has_any_perms(Cluster, ['admin','create_vm'], False): profile = user.get_profile() owners.append((profile.id, profile.name)) self.fields['owner'].choices = owners # Set cluster choices. If an owner has been selected then filter # by the owner. Otherwise show everything the user has access to # through themselves or any groups they are a member of if self.owner: q = self.owner.get_objects_any_perms(Cluster, ['admin','create_vm']) else: q = user.get_objects_any_perms(Cluster, ['admin','create_vm']) self.fields['cluster'].queryset = q