def __init__(self, *args, **kwargs): super(DeviceForm, self).__init__(*args, **kwargs) if self.instance.id is not None: asset = self.instance.get_asset() if asset: self.fields['dc'].widget = ReadOnlyWidget() self.fields['rack'].widget = ReadOnlyWidget() self.fields['chassis_position'].widget = ReadOnlyWidget() self.fields['position'].widget = ReadOnlyWidget()
def __init__(self, *args, **kwargs): super(SplitDevice, self).__init__(*args, **kwargs) fillable_fields = [ 'model_user', 'device_info', 'invoice_no', 'order_no', 'request_date', 'delivery_date', 'invoice_date', 'production_use_date', 'provider_order_date', 'provider_order_date', 'support_period', 'support_type', 'provider', 'source', 'status', 'warehouse', ] for field_name in self.fields: if field_name in fillable_fields: classes = "span12 fillable" elif field_name == 'support_void_reporting': classes = "" else: classes = "span12" self.fields[field_name].widget.attrs = {'class': classes} self.fields['model_proposed'].widget = ReadOnlyWidget()
def __init__(self, *args, **kwargs): super(DeviceCreateForm, self).__init__(*args, **kwargs) self.fields['venture'].choices = all_ventures() self.fields['venture_role'].choices = all_roles() self.fields['venture'].required = True self.fields['model'].required = True self.fields['asset'] = AutoCompleteSelectField( ('ralph_assets.api_ralph', 'AssetLookup'), required=False, ) self.fields['asset'].widget.help_text = ( 'Enter asset sn, barcode or model') del self.fields['save_comment'] if 'data' in kwargs and kwargs['data'].get('asset'): if Asset.objects.filter(pk=kwargs['data']['asset']).exists(): self.fields['chassis_position'].widget = ReadOnlyWidget() self.fields['position'].widget = ReadOnlyWidget()
def __init__(self, *args, **kwargs): super(DeviceForm, self).__init__(*args, **kwargs) if self.instance: asset = self.instance.get_asset() if 'parent' in self.fields: self.fields['parent'].choices = [ ('', '----'), ] + [(p.id, p.name) for p in self.get_possible_parents(self.instance)] if asset: self.fields['parent'].widget = ReadOnlySelectWidget( choices=self.fields['parent'].choices, ) if asset: if 'chassis_position' in self.fields: self.fields['chassis_position'].widget = ReadOnlyWidget() if 'position' in self.fields: self.fields['position'].widget = ReadOnlyWidget()
class CIViewForm(CIEditForm): class Meta: model = models.CI widgets = { 'id': ReadOnlyWidget, 'uid' : ReadOnlyWidget, 'name' : ReadOnlyWidget, 'type' : ReadOnlySelectWidget, 'state' : ReadOnlySelectWidget, 'status' : ReadOnlySelectWidget, 'barcode' : ReadOnlyWidget, 'pci_scope' : ReadOnlyWidget, 'technical_owner' : ReadOnlyWidget, } fields = ( 'id', 'uid', 'name', 'type', 'state', 'status', 'layers', 'barcode', 'pci_scope', 'technical_owner', ) layers = forms.ModelMultipleChoiceField( models.CILayer.objects.all(), widget = ReadOnlyMultipleChoiceWidget("layers", False, attrs={'rows' : '10' }) ) technical_owner = forms.CharField(widget = ReadOnlyWidget()) def __init__(self, *args, **kwargs): super(CIViewForm, self).__init__(*args, **kwargs) if self.data: self.data = self.data.copy() self.data['technical_owner'] = self.initial['technical_owner']
class ServerMoveStep3Form(forms.Form): address = forms.CharField(widget=ReadOnlyWidget()) new_ip = forms.CharField() new_hostname = forms.CharField() def clean_address(self): address = self.cleaned_data['address'] _check_move_address(address) return address def clean_new_ip(self): old_ip = self.cleaned_data.get('address') new_ip = self.cleaned_data['new_ip'] try: new_ip = str(ipaddr.IPAddress(new_ip)) except ValueError: raise forms.ValidationError("Malformed IP address.") rdomain = '.'.join(list(reversed( new_ip.split('.')))[1:]) + '.in-addr.arpa' if not Domain.objects.filter(name=rdomain).exists(): raise forms.ValidationError("No RevDNS domain for address.") try: ipaddress = IPAddress.objects.get(address=new_ip) except IPAddress.DoesNotExist: if Record.objects.filter(content=new_ip).exists(): raise forms.ValidationError("Address already in DNS.") if get_revdns_records(new_ip).exists(): raise forms.ValidationError("Address already in DNS.") if DHCPEntry.objects.filter(ip=new_ip).exists(): raise forms.ValidationError("Address already in DHCP.") else: if ipaddress.device and not ipaddress.device.deleted: if not old_ip: raise forms.ValidationError("Address in use.") device = Device.objects.get(ipaddress__address=old_ip) if ipaddress.device.id != device.id: raise forms.ValidationError("Address used by %s" % device, ) return new_ip def clean_new_hostname(self): old_ip = self.cleaned_data.get('address') new_hostname = self.cleaned_data['new_hostname'] if not is_valid_hostname(new_hostname): raise forms.ValidationError("Invalid hostname") try: get_domain(new_hostname) except Domain.DoesNotExist: raise forms.ValidationError("Invalid domain") try: ipaddress = IPAddress.objects.get(hostname=new_hostname) except IPAddress.DoesNotExist: if find_addresses_for_hostname(new_hostname): raise forms.ValidationError("Hostname already in DNS.") else: if ipaddress.device and not ipaddress.device.deleted: if not old_ip: raise forms.ValidationError("Hostname in use.") device = Device.objects.get(ipaddress__address=old_ip) if ipaddress.device.id != device.id: raise forms.ValidationError("Hostname used by %s" % device, ) elif Record.objects.filter(name=new_hostname).exists(): raise forms.ValidationError("Hostname already in DNS.") return new_hostname