def __init__()

in community/front-end/ofe/website/ghpcfe/forms.py [0:0]


    def __init__(self, user, *args, **kwargs):
        has_creds = "cloud_credential" in kwargs
        if has_creds:
            credential = kwargs.pop("cloud_credential")
            kwargs["initial"]["cloud_credential"] = credential
        super().__init__(*args, **kwargs)
        if not has_creds:
            credential = self.instance.cloud_credential
        zone_choices = None
        if "zone_choices" in kwargs:
            zone_choices = kwargs.pop("zone_choices")

        if self.instance.id:
            for field in self.fields:
                if field != "name":
                    self.fields[field].disabled = True

        self.fields["subnet"].queryset = VirtualSubnet.objects.filter(
            cloud_credential=credential
        ).filter(Q(cloud_state="i") | Q(cloud_state="m"))
        if zone_choices:
            # We set this on the widget, because we will be changing the
            # widget's field in the template via javascript
            self.fields["cloud_zone"].widget.choices = zone_choices

        if "n" not in self.instance.cloud_state:
            # Need to disable certain widgets
            self.fields["subnet"].disabled = True
            self.fields["cloud_zone"].disabled = True
            self.fields["attached_cluster"].disabled = True

        self.workbench_zones = cloud_info.get_gcp_workbench_region_zone_info(
            credential.detail
        )

        self.fields["trusted_user"].queryset = (
            User.objects.exclude(socialaccount__isnull=True)
        )

        # Pull instance types from cloud_info
        instance_types = cloud_info.get_machine_types(
            "GCP", credential.detail, "europe-west4", "europe-west4-a"
        )
        # set variables for retrieving instance types for dropdown menu
        choices_list = []
        instance_list = []
        category = ""
        # Populate dropdown menu with preset instance_types from
        # WorkbenchPresets
        for preset in WorkbenchPreset.objects.order_by("category").values():
            # if category variable has changed from last loop then append
            # instances to overall choices list as tuple and clear instance_list
            if category != preset["category"]:
                if category:
                    choices_list.append((category, tuple(instance_list)))
                instance_list = []
            # set category to current value and append preset to dropdown menu
            # list
            category = preset["category"]
            instance_list.append((preset["machine_type"], preset["name"]))
        # append final preset instance type from loop
        choices_list.append((category, tuple(instance_list)))
        category = ""
        if Role.CLUSTERADMIN in [x.id for x in user.roles.all()]:
            for instance_type in sorted(instance_types):
                # if family variable has changed from last loop then append
                # instances to overall choices list as tuple and clear
                # instance_list
                if category != instance_types[instance_type]["family"]:
                    if category:
                        choices_list.append((category, tuple(instance_list)))
                    instance_list = []
                # save family of current instance
                category = instance_types[instance_type]["family"]
                # create instance string for displaying to user
                instance_string = (
                    instance_types[instance_type]["name"]
                    + " - "
                    + str(instance_types[instance_type]["vCPU"])
                    + "x "
                    + instance_types[instance_type]["arch"]
                    + " vCPUs with "
                    + str(instance_types[instance_type]["memory"])
                    + " Memory"
                )
                # append tuple to instance list
                instance_list.append(
                    (instance_types[instance_type]["name"], instance_string)
                )
            # append final preset instance type from loop
            choices_list.append((category, tuple(instance_list)))
        self.fields["machine_type"].widget.choices = choices_list
        self.fields["attached_cluster"].queryset= Cluster.objects.filter(
                cloud_state="m"
                )