def clean(self): for name, value in self.cleaned_data.iteritems(): if name.endswith('-custom'): continue if value == 'custom': info = self.field_info.get(name, DefaultInfo()) try: value = info.clean(self.cleaned_data[name + '-custom']) except (TypeError, ValueError, forms.ValidationError) as e: self._errors[name + '-custom'] = self.error_class([e]) else: if (name == 'type' and self.result['type'].get((value,)) == 'unknown'): msg = "Please specify custom value for this component." self._errors[name] = self.error_class([msg]) if 'ralph_assets' in settings.INSTALLED_APPS: from ralph_assets.api_ralph import is_asset_assigned from ralph_assets.models import Asset try: asset = self.get_value('asset') except (KeyError, ValueError): asset = None else: if asset == 'None': asset = None if asset: _, asset_sn, asset_barcode = asset.split(' - ') try: if asset_sn == '': asset_sn = None if asset_barcode == '': asset_barcode = None asset_obj = Asset.objects.get(sn=asset_sn, barcode=asset_barcode) except Asset.DoesNotExist: pass else: if (self.data['asset'] != 'database' and is_asset_assigned(asset_id=asset_obj.id)): msg = ("This asset is already linked to some other " "device. To resolve this conflict, please " "click the link above.") self._errors['asset'] = self.error_class([msg]) try: selected_type = self.get_value('type') except (KeyError, ValueError): if not asset: msg = "Can't save this device without specifying an asset." self._errors['asset'] = self.error_class([msg]) else: selected_type = get_choice_by_name(DeviceType, selected_type) if selected_type not in ASSET_NOT_REQUIRED and not asset: msg = "Asset is required for this kind of device." self._errors['asset'] = self.error_class([msg]) return self.cleaned_data
def clean(self): for name, value in self.cleaned_data.iteritems(): if name.endswith('-custom'): continue if value == 'custom': info = self.field_info.get(name, DefaultInfo()) try: value = info.clean(self.cleaned_data[name + '-custom']) except (TypeError, ValueError, forms.ValidationError) as e: self._errors[name + '-custom'] = self.error_class([e]) else: if (name == 'type' and self.result['type'].get( (value, )) == 'unknown'): msg = "Please specify custom value for this component." self._errors[name] = self.error_class([msg]) if 'ralph_assets' in settings.INSTALLED_APPS: from ralph_assets.api_ralph import is_asset_assigned from ralph_assets.models import Asset try: asset = self.get_value('asset') except (KeyError, ValueError): asset = None else: if asset == 'None': asset = None if asset: _, asset_sn, asset_barcode = asset.split(' - ') try: if asset_sn == '': asset_sn = None if asset_barcode == '': asset_barcode = None asset_obj = Asset.objects.get(sn=asset_sn, barcode=asset_barcode) except Asset.DoesNotExist: pass else: if (self.data['asset'] != 'database' and is_asset_assigned(asset_id=asset_obj.id)): msg = ("This asset is already linked to some other " "device. To resolve this conflict, please " "click the link above.") self._errors['asset'] = self.error_class([msg]) try: selected_type = self.get_value('type') except (KeyError, ValueError): if not asset: msg = "Can't save this device without specifying an asset." self._errors['asset'] = self.error_class([msg]) else: selected_type = get_choice_by_name(DeviceType, selected_type) if selected_type not in ASSET_NOT_REQUIRED and not asset: msg = "Asset is required for this kind of device." self._errors['asset'] = self.error_class([msg]) return self.cleaned_data
def clean_asset(self): asset = self.cleaned_data['asset'] if 'ralph_assets' in settings.INSTALLED_APPS: from ralph_assets.api_ralph import is_asset_assigned if is_asset_assigned( asset_id=asset.id, exclude_devices=[self.device_id], ): raise forms.ValidationError( 'This asset is assigned to other device.', ) return asset
def clean_asset(self): asset = self.cleaned_data["asset"] if not asset: raise forms.ValidationError( "You cannot save this field without selecting an asset. " "If you want to unlink an asset from this device, " "please use 'Edit this asset' button below." ) if "ralph_assets" in settings.INSTALLED_APPS: from ralph_assets.api_ralph import is_asset_assigned if is_asset_assigned(asset_id=asset.id, exclude_devices=[self.device_id]): raise forms.ValidationError( "This asset is already linked to some other device. " "To resolve this conflict, please click the link above." ) return asset
def clean_asset(self): asset = self.cleaned_data['asset'] if not asset: raise forms.ValidationError( "You cannot save this field without selecting an asset. " "If you want to unlink an asset from this device, " "please use 'Edit this asset' button below.") if 'ralph_assets' in settings.INSTALLED_APPS: from ralph_assets.api_ralph import is_asset_assigned if is_asset_assigned( asset_id=asset.id, exclude_devices=[self.device_id], ): raise forms.ValidationError( "This asset is already linked to some other device. " "To resolve this conflict, please click the link above.") return asset
def clean_asset(self): model = self.cleaned_data.get('model') asset = self.cleaned_data.get('asset') # model should be required anyway, so there's no sense to validate an # asset when the model is missing if not model: return asset if model and model.type not in ASSET_NOT_REQUIRED: if not asset: msg = "Asset is required for this kind of device." raise forms.ValidationError(msg) if 'ralph_assets' in settings.INSTALLED_APPS: from ralph_assets.api_ralph import is_asset_assigned if is_asset_assigned(asset_id=asset.id): raise forms.ValidationError( "This asset is already linked to some other device. " "To resolve this conflict, please click the link above.") return asset
def clean_asset(self): model = self.cleaned_data.get('model') asset = self.cleaned_data.get('asset') # model should be required anyway, so there's no sense to validate an # asset when the model is missing if not model: return asset if model and model.type not in ASSET_NOT_REQUIRED: if not asset: msg = "Asset is required for this kind of device." raise forms.ValidationError(msg) if 'ralph_assets' in settings.INSTALLED_APPS: from ralph_assets.api_ralph import is_asset_assigned if is_asset_assigned(asset_id=asset.id): raise forms.ValidationError( "This asset is already linked to some other device. " "To resolve this conflict, please click the link above." ) return asset