def clean(self): cleaned_data = super(VolumeOptionsAction, self).clean() volume_opt = cleaned_data.get('volume_type', None) if volume_opt and not cleaned_data[volume_opt]: raise forms.ValidationError( _('Please choose a volume, or select ' '%s.') % self.VOLUME_CHOICES[0][1]) return cleaned_data
def clean(self): cleaned_data = super(CreateApplicationCredentialForm, self).clean() try: cleaned_data['access_rules'] = yaml.safe_load( cleaned_data['access_rules']) except yaml.YAMLError: msg = (_('Access rules must be a valid JSON or YAML list.')) raise forms.ValidationError(msg) return cleaned_data
def clean(self): cleaned_data = super(PolicyBaseInfoAction, self).clean() exists_policys = alarmpolicy.objects.all() for p in exists_policys: if p.name == cleaned_data.get('name'): raise forms.ValidationError( _('The name "%s" is already used by another policy.') % cleaned_data.get('name')) return cleaned_data
def clean(self): cleaned_data = super(SetFlavorChoiceAction, self).clean() flavor = cleaned_data.get('flavor', None) if flavor is None or flavor == cleaned_data['old_flavor_id']: raise forms.ValidationError( _('Please choose a new flavor that ' 'can not be same as the old one.')) return cleaned_data
def clean(self): cleaned_data = super(UpdateSubnet, self).clean() ip_version = int(cleaned_data.get('ip_version')) gateway_ip = cleaned_data.get('gateway_ip') if gateway_ip: if netaddr.IPAddress(gateway_ip).version is not ip_version: msg = _('Gateway IP and IP version are inconsistent.') raise forms.ValidationError(msg) return cleaned_data
def _convert_ip_address(self, ip, field_name): try: return netaddr.IPAddress(ip) except (netaddr.AddrFormatError, ValueError): msg = (_('%(field_name)s: Invalid IP address (value=%(ip)s)') % { 'field_name': field_name, 'ip': ip }) raise forms.ValidationError(msg)
def clean(self): cleaned_data = super(RebuildInstanceForm, self).clean() if 'password' in cleaned_data: passwd = cleaned_data.get('password') confirm = cleaned_data.get('confirm_password') if passwd is not None and confirm is not None: if passwd != confirm: raise forms.ValidationError(_("Passwords do not match.")) return cleaned_data
def clean(self): cleaned_data = super(IncreaseForm, self).clean() orig_size = cleaned_data.get('orig_size') if not orig_size: msg = _( "Fail to get the size of current volume storage, please check the cinder services or exclude other reasons." ) raise forms.ValidationError(msg) return cleaned_data
def clean_period(self): '''Check to make sure period is zero unless type is WEBHOOK. ''' data = self.cleaned_data if data['type'] != constants.NotificationType.WEBHOOK and data[ 'period'] != 0: raise forms.ValidationError( _("Period must be zero except for type webhook.")) return data['period']
def _convert_ip_network(self, network, field_name): try: return netaddr.IPNetwork(network) except (netaddr.AddrFormatError, ValueError): msg = ( _('%(field_name)s: Invalid IP address (value=%(network)s)') % { 'field_name': field_name, 'network': network }) raise forms.ValidationError(msg)
def clean(self): cleaned_data = super(AddVolumeTypesToGroupAction, self).clean() volume_types = cleaned_data.get('add_vtypes_to_group_role_member') if not volume_types: raise forms.ValidationError( _('At least one volume type must be assigned ' 'to a group.') ) return cleaned_data
def clean_password2(self): if ("password1" in self.cleaned_data and "password2" in self.cleaned_data): if (self.cleaned_data["password1"] != self.cleaned_data["password2"]): raise forms.ValidationError( _("You must type the same password" " each time.")) return self.cleaned_data["password2"]
def clean(self): cleaned_data = super(CreateSubnetInfoAction, self).clean() with_subnet = cleaned_data.get('with_subnet') cidr = cleaned_data.get('cidr') ip_version = int(cleaned_data.get('ip_version')) gateway_ip = cleaned_data.get('gateway_ip') if with_subnet and not cidr: msg = _('Specify "Network Address" or ' 'clear "Create Subnet" checkbox.') raise forms.ValidationError(msg) if cidr: if netaddr.IPNetwork(cidr).version is not ip_version: msg = _('Network Address and IP version are inconsistent.') raise forms.ValidationError(msg) if gateway_ip: if netaddr.IPAddress(gateway_ip).version is not ip_version: msg = _('Gateway IP and IP version are inconsistent.') raise forms.ValidationError(msg) return cleaned_data
def clean(self): cleaned_data = super(CreateForm, self).clean() import_type = cleaned_data.get('source') if import_type == 'app_file' and not cleaned_data.get('app_file'): msg = _('Please supply an app file') raise forms.ValidationError(msg) elif import_type == 'input': if not (cleaned_data.get('name')): msg = _('Please supply a name') raise forms.ValidationError(msg) elif not cleaned_data.get('languagepack'): msg = _('Please supply a languagepack') raise forms.ValidationError(msg) elif not cleaned_data.get('git_url'): msg = _('Please supply a github url') raise forms.ValidationError(msg) elif not cleaned_data.get('run_cmd'): msg = _('Please supply a run command') raise forms.ValidationError(msg) return cleaned_data
def clean_consumer_choice(self): # ensure that new consumer isn't the same as current consumer qos_spec = self.initial['qos_spec'] cleaned_new_consumer = self.cleaned_data.get('consumer_choice') old_consumer = qos_spec.consumer if cleaned_new_consumer == old_consumer: raise forms.ValidationError( _('QoS Spec consumer value must be different than ' 'the current consumer value.')) return cleaned_new_consumer
def clean(self): cleaned_data = super(UpdateForm, self).clean() values = cleaned_data.get('values') try: values = json.loads(values) cleaned_data['values'] = values except json.JSONDecodeError: raise forms.ValidationError(_('Values must be written in JSON')) return cleaned_data
def clean_email(self): email = self.cleaned_data["email"] self.users = get_user_model().objects \ .filter(Q(email__iexact=email)).distinct() if not self.users.exists(): raise forms.ValidationError( _("The e-mail address is not assigned" " to any user account")) return self.cleaned_data["email"]
def clean(self): cleaned_data = super(UpdateForm, self).clean() values = cleaned_data.get('values') try: values = eval(values) cleaned_data['values'] = values except (SyntaxError, NameError): raise forms.ValidationError(_('Values must written in JSON')) return cleaned_data
def _check_count(self, cleaned_data): min_count = cleaned_data.get('min_count') if min_count: max_count = cleaned_data.get('count', 1) if max_count < min_count: error_message = ungettext_lazy( 'The requested instance cannot be launched as ' 'the requested %(req)i instances is less than ', 'the requested minimum %(min)i instances. ' 'The request instance cannot be launched.', max_count) params = {'req': max_count, 'min': min_count} raise forms.ValidationError(error_message % params)
def clean(self): cleaned_data = super(EditPortsAction, self).clean() public_protocol = cleaned_data.get('public_protocol', None) private_protocol = cleaned_data.get('private_protocol', None) if public_protocol and private_protocol: if public_protocol != private_protocol: raise forms.ValidationError( "The source and destination Port Aliases " "must use the same protocol.") return cleaned_data
def clean(self): cleaned_data = super(BaseFirewallRuleForm, self).clean() s_protocol = None d_protocol = None msg = u"This field is required." if cleaned_data.get('source_port_alias', None): if cleaned_data['source_port_alias'] == 'Custom': if cleaned_data['source_protocol'] is None: self._errors['source_protocol'] = self.error_class([msg]) del cleaned_data["source_protocol"] else: s_protocol = cleaned_data['source_protocol'] if cleaned_data['source_public_port'] is None: self._errors['source_public_port'] = self.error_class( [msg]) del cleaned_data["source_public_port"] else: port_alias = quantum_extensions_client.portalias_get( self.request, cleaned_data['source_port_alias']) cleaned_data['source_protocol'] = port_alias['protocol'] cleaned_data['source_public_port'] = port_alias['port'] s_protocol = port_alias['protocol'] if cleaned_data.get('destination_port_alias', None): if cleaned_data['destination_port_alias'] == 'Custom': if cleaned_data['destination_protocol'] is None: self._errors['destination_protocol'] = self.error_class( [msg]) del cleaned_data["destination_protocol"] else: d_protocol = cleaned_data["destination_protocol"] if cleaned_data['destination_public_port'] is None: self._errors['destination_public_port'] = self.error_class( [msg]) del cleaned_data["destination_public_port"] else: port_alias = quantum_extensions_client.portalias_get( self.request, cleaned_data['destination_port_alias']) cleaned_data['destination_protocol'] = port_alias['protocol'] cleaned_data['destination_public_port'] = port_alias['port'] d_protocol = port_alias['protocol'] if s_protocol and d_protocol: if s_protocol != d_protocol: raise forms.ValidationError( "The source and destination Port Aliases " "must use the same protocol.") return cleaned_data
def clean(self): cleaned_data = super(EditFirewallRuleForm, self).clean() source_protocol = cleaned_data.get('source_protocol', None) destination_protocol = cleaned_data.get('destination_protocol', None) if source_protocol and destination_protocol: if source_protocol != destination_protocol: raise forms.ValidationError( "The source and destination Port Aliases " "must use the same protocol.") return cleaned_data
def clean(self): cleaned_data = super().clean() # access_rules field exists only when keystone API >= 3.13 and # the field is deleted above when a lower version of API is used. if 'access_rules' in cleaned_data: try: cleaned_data['access_rules'] = yaml.safe_load( cleaned_data['access_rules']) except yaml.YAMLError: msg = (_('Access rules must be a valid JSON or YAML list.')) raise forms.ValidationError(msg) return cleaned_data
def clean(self): cleaned_data = super(RebuildInstanceForm, self).clean() is_allow_inject_passwd = False select_image_id = cleaned_data.get('image', None) if select_image_id != None: image = api.glance.image_get(self.request, select_image_id) is_allow_inject_passwd = getattr(image, 'is_allow_inject_passwd', False) cleaned_data['is_allow_inject_passwd'] = is_allow_inject_passwd cleaned_data['image_name'] = image.name if 'password' in cleaned_data: passwd = cleaned_data.get('password') confirm = cleaned_data.get('confirm_password') if passwd is not None and confirm is not None: if passwd != confirm: raise forms.ValidationError(_("Passwords do not match.")) if is_allow_inject_passwd: if not validators.validate_password(passwd): raise forms.ValidationError(_("The password must begin with a letter, " "and the length is between the 8-64 bits, " "and the number, the letter and the symbol are the same.")) return cleaned_data
def clean(self): """Checks that no two articles have the same title.""" if any(self.errors): # Don't bother validating the formset unless each form is valid on its own return titles = [] for form in self.forms: title = form.cleaned_data['produkt'] if title in titles: raise forms.ValidationError( "Articles in a set must have distinct titles.") titles.append(title)
def handle(self, request, data): try: vim_name = data['vim_name'] description = data['vim_description'] password = data['password'] username = data['username'] project_name = data['project_name'] is_default = data['is_default'] auth_url = data['auth_url'] domain_name = data['domain_name'] cert_verify = data.get('cert_verify', api.tacker.CERT_TRUE_TYPE) if cert_verify not in [ api.tacker.CERT_TRUE_TYPE, api.tacker.CERT_FALSE_TYPE ]: raise forms.ValidationError("cert_verify type not supported.") auth_cred = { 'username': username, 'password': password, 'user_domain_name': domain_name, 'cert_verify': cert_verify } bearer_token = data['bearer_token'].replace('None', '') ssl_ca_cert = data['ssl_ca_cert'].replace('\r\n', ' ') vim_type = data['vim_type'] if vim_type == 'kubernetes': auth_cred = {'username': username, 'password': password} # if bearer_token is provided, use it instead if bearer_token: auth_cred = {'bearer_token': bearer_token} # only k8s vim needs ssl_ca_cert and it's optional if ssl_ca_cert: auth_cred['ssl_ca_cert'] = ssl_ca_cert vim_arg = { 'vim': { 'name': vim_name, 'description': description, 'type': vim_type, 'auth_url': auth_url, 'auth_cred': auth_cred, 'vim_project': { 'name': project_name, 'project_domain_name': domain_name }, 'is_default': is_default } } api.tacker.create_vim(request, vim_arg) messages.success( request, _('VIM %s create operation initiated.') % vim_name) return True except Exception: exceptions.handle(request, _('Failed to register VIM.'))
def _check_source_volume(self, cleaned_data): if not cleaned_data.get('volume_id'): msg = _("You must select a volume.") self._errors['volume_id'] = self.error_class([msg]) # Prevent launching multiple instances with the same volume. # TODO(gabriel): is it safe to launch multiple instances with # a snapshot since it should be cloned to new volumes? count = cleaned_data.get('count', 1) if count > 1: msg = _('Launching multiple instances is only supported for ' 'images and instance snapshots.') raise forms.ValidationError(msg)
def clean_backup(self): backup = self.cleaned_data['backup'] if backup: try: # Make sure the user is not "hacking" the form # and that they have access to this backup_id LOG.debug("Obtaining backups") bkup = api.trove.backup_get(self.request, backup) self.cleaned_data['backup'] = bkup.id except Exception: raise forms.ValidationError(_("Unable to find backup!")) return backup
def clean(self): cleaned_data = super(SetInstanceDetailsAction, self).clean() # Validate our instance source. source = cleaned_data['source_type'] # There should always be at least one image_id choice, telling the user # that there are "No Images Available" so we check for 2 here... volume_type = self.data.get('volume_type', None) if volume_type: # Boot from volume if cleaned_data[source]: raise forms.ValidationError( _("You can't select an instance " "source when booting from a " "Volume. The Volume is your " "source and should contain " "the operating system.")) else: # Boot from image / image_snapshot if source == 'image_id' and not \ filter(lambda x: x[0] != '', self.fields['image_id'].choices): raise forms.ValidationError( _("There are no image sources " "available; you must first " "create an image before " "attemtping to launch an " "instance.")) elif not cleaned_data[source]: raise forms.ValidationError( _("Please select an option for the" " instance source.")) # Prevent launching multiple instances with the same volume. # TODO(gabriel): is it safe to launch multiple instances with # a snapshot since it should be cloned to new volumes? count = cleaned_data.get('count', 1) if volume_type and count > 1: msg = _('Launching multiple instances is only supported for ' 'images and instance snapshots.') raise forms.ValidationError(msg) return cleaned_data
def clean(self): cleaned_data = super(AddInterface, self).clean() ifclass = cleaned_data.get('ifclass', 'none') if ifclass != 'platform': cleaned_data['networks'] = [] if ifclass != 'data': cleaned_data.pop('ipv4_mode', None) cleaned_data.pop('ipv6_mode', None) if cleaned_data.get('ipv4_mode') != 'pool': cleaned_data.pop('ipv4_pool', None) if cleaned_data.get('ipv6_mode') != 'pool': cleaned_data.pop('ipv6_pool', None) if ifclass == 'data': datanetworks = [ _f for _f in cleaned_data.get('datanetworks_data', []) if _f ] elif ifclass == 'pci-passthrough': datanetworks = [ _f for _f in cleaned_data.get('datanetworks_pci', []) if _f ] elif ifclass == 'pci-sriov': datanetworks = [ _f for _f in cleaned_data.get('datanetworks_sriov', []) if _f ] else: datanetworks = [] # datanetwork selection is required for 'data', 'pci-passthrough' # and 'pci-sriov'. It is NOT required for any other interface class if not datanetworks: # Note that 1 of 3 different controls may be used to select # data network, make sure to set the error on the appropriate # control if ifclass in ['data', 'pci-passthrough', 'pci-sriov']: raise forms.ValidationError( _("You must specify a Data Network")) cleaned_data['datanetworks'] = ",".join(datanetworks) if 'datanetworks_data' in cleaned_data: del cleaned_data['datanetworks_data'] if 'datanetworks_pci' in cleaned_data: del cleaned_data['datanetworks_pci'] if 'datanetworks_sriov' in cleaned_data: del cleaned_data['datanetworks_sriov'] return cleaned_data