class AccessPointCreateForm(ChainedChoicesModelForm): community = ModelChoiceField(queryset=Community.objects.all()) building = ChainedModelChoiceField('community', reverse_lazy('core:chained_building'), Building) room = ChainedModelChoiceField('building', reverse_lazy('core:chained_room'), Room) upstream_device = ChainedModelChoiceField( 'room', reverse_lazy('network:ports_chained_port'), Port, label="Port") def __init__(self, *args, **kwargs): super(AccessPointCreateForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_method = 'post' self.helper.html5_required = True self.helper.form_class = 'form-horizontal table-add-form' self.helper.label_class = 'col-sm-2' self.helper.field_class = 'col-sm-10 col-md-8' self.helper.layout = Layout( Fieldset( 'Add a new access point', Field('community', autocomplete='off'), Field('building', autocomplete='off'), Field('room', autocomplete='off'), Field('upstream_device', autocomplete='off'), Field('dns_name', placeholder=self.fields['dns_name'].label), Field('property_id', placeholder=self.fields['property_id'].label), Field('serial_number', placeholder=self.fields['serial_number'].label), Field('mac_address', placeholder=self.fields['mac_address'].label), Field('ip_address', placeholder=self.fields['ip_address'].label), Field('ap_type', placeholder=self.fields['ap_type'].label), ), FormActions(Submit('submit', 'Add Access Point'), )) # Make error messages a bit more readable for field_name in self.fields: self.fields[field_name].error_messages = { 'required': 'A ' + field_name + ' is required.' } def save(self, commit=True): access_point = super().save(commit=False) access_point.display_name = access_point.dns_name if commit: access_point.save() return access_point class Meta: model = AccessPoint fields = [ 'community', 'building', 'room', 'upstream_device', 'dns_name', 'property_id', 'serial_number', 'mac_address', 'ip_address', 'ap_type' ]
class PortCreateForm(ChainedChoicesModelForm): community = ModelChoiceField(queryset=Community.objects.all()) building = ChainedModelChoiceField('community', reverse_lazy('core:chained_building'), Building) room = ChainedModelChoiceField('building', reverse_lazy('core:chained_room'), Room) upstream_device = ModelChoiceField( queryset=NetworkInfrastructureDevice.objects.all()) def __init__(self, *args, **kwargs): super(PortCreateForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_method = 'post' self.helper.html5_required = True self.helper.form_class = 'form-horizontal table-add-form' self.helper.label_class = 'col-sm-2' self.helper.field_class = 'col-sm-10 col-md-8' self.fields['display_name'].label = 'Jack' self.helper.layout = Layout( Fieldset( 'Add a new port', Field('community', autocomplete='off'), Field('building', autocomplete='off'), Field('room', autocomplete='off'), Field('upstream_device', placeholder=self.fields['upstream_device'].label), Field('display_name', placeholder=self.fields['display_name'].label), Field('blade_number', placeholder=self.fields['blade_number'].label), Field('port_number', placeholder=self.fields['port_number'].label), ), FormActions(Submit('submit', 'Add Port'), )) # Make error messages a bit more readable for field_name in self.fields: self.fields[field_name].error_messages = { 'required': 'A ' + field_name + ' is required.' } class Meta: model = Port fields = [ 'community', 'building', 'room', 'upstream_device', 'display_name', 'blade_number', 'port_number' ]
class PrinterForm(ChainedChoicesModelForm): sub_department = ChainedModelChoiceField('department', reverse_lazy('core:chained_sub_department'), SubDepartment, label="Sub Department") community = ModelChoiceField(queryset=Community.objects.all(), required=False) building = ChainedModelChoiceField('community', reverse_lazy('core:chained_building'), Building, required=False) room = ChainedModelChoiceField('building', reverse_lazy('core:chained_room'), Room, required=False) def __init__(self, *args, **kwargs): super(PrinterForm, self).__init__(*args, **kwargs) self.fields["date_purchased"].widget.attrs['class'] = "dateinput" self.fields['display_name'].label = 'Printer Name' self.helper = FormHelper() self.helper.form_method = 'post' self.helper.html5_required = True self.helper.form_class = 'form-horizontal table-add-form' self.helper.label_class = 'col-sm-2' self.helper.field_class = 'col-sm-10 col-md-8' self.helper.layout = Layout( Fieldset( 'Add a new printer', Field('community', autocomplete='off'), Field('building', autocomplete='off'), Field('room', autocomplete='off'), Field('department', autocomplete='off'), Field('sub_department', autocomplete='off'), Field('display_name', placeholder=self.fields['display_name'].label), Field('mac_address', placeholder=self.fields['mac_address'].label), Field('ip_address', css_class="ip_address_field", placeholder=self.fields['ip_address'].label, title="Leave blank for DHCP."), Field('model', placeholder=self.fields['model'].label), Field('serial_number', placeholder=self.fields['serial_number'].label), Field('property_id', placeholder=self.fields['property_id'].label), Field('location', placeholder=self.fields['location'].label), Field('date_purchased', css_class="dateinput", placeholder=self.fields['date_purchased'].label), Field('description', placeholder=self.fields['description'].label), ), FormActions( Submit('submit', 'Add Printer'), ) ) # Make error messages a bit more readable for field_name in self.fields: self.fields[field_name].error_messages['required'] = 'A ' + field_name + ' is required.' class Meta: model = Printer fields = ['community', 'building', 'room', 'department', 'sub_department', 'display_name', 'mac_address', 'ip_address', 'model', 'serial_number', 'property_id', 'location', 'date_purchased', 'description']
class PartsRequestForm(Form): # Ticket info priority = ChoiceField(label='Request Priority', error_messages={'required': 'Please select a priority'}) # Request info printer = ModelChoiceField(queryset=None, empty_label="-------------", label='Printer', error_messages={'required': 'Please select a printer'}) part = ChainedModelChoiceField('printer', reverse_lazy('printerrequests:chained_part'), Part, label='Part', error_messages={'required': 'Please select a part'}) address = CharField(label='Location') def __init__(self, *args, **kwargs): super(PartsRequestForm, self).__init__(*args, **kwargs) self.fields['printer'].queryset = PrinterType.objects.filter(id__in=set(Part.objects.values_list('printer', flat=True))) self.fields["priority"].choices = PRIORITY_CHOICES self.helper = FormHelper() self.helper.form_method = 'post' self.helper.html5_required = True self.helper.form_class = 'form-horizontal' self.helper.label_class = 'col-sm-2' self.helper.field_class = 'col-sm-8 col-md-6' self.helper.layout = Layout( Fieldset( 'Create a parts request', *self.fields ), FormActions( Submit('submit', 'Submit'), ) )
class NetworkInfrastructureDeviceCreateForm(ChainedChoicesModelForm): community = ModelChoiceField(queryset=Community.objects.all()) building = ChainedModelChoiceField(parent_field='community', ajax_url=reverse_lazy('core:chained_building'), model=Building) room = ChainedModelChoiceField('building', reverse_lazy('core:chained_room'), model=Room) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_method = 'post' self.helper.html5_required = True self.helper.form_class = 'form-horizontal table-add-form' self.helper.label_class = 'col-sm-2' self.helper.field_class = 'col-sm-10 col-md-8' self.helper.layout = Layout( Fieldset( 'Add a new network infrastructure device', Field('community', autocomplete='off'), Field('building', autocomplete='off'), Field('room', autocomplete='off'), Field('dns_name', placeholder=self.fields['dns_name'].label), Field('display_name', placeholder=self.fields['display_name'].label), Field('ip_address', placeholder=self.fields['ip_address'].label), ), FormActions( Submit('submit', 'Add Device'), ) ) # Make error messages a bit more readable for field_name in self.fields: self.fields[field_name].error_messages = {'required': 'A ' + field_name + ' is required.'} class Meta: model = NetworkInfrastructureDevice fields = ['community', 'building', 'room', 'dns_name', 'display_name', 'ip_address']
class ModelChainForm(ChainedChoicesModelForm): brand = ModelChoiceField(queryset=CarBrand.objects.all(), required=True, empty_label=_(u'Select a car brand')) model = ChainedModelChoiceField(parent_field='brand', ajax_url=reverse_lazy('ajax_chained_models'), empty_label=_(u'Select a car model'), model=BrandModel, required=True) engine = ChoiceField(choices=([('', _('All engine types'))] + Car.ENGINES), required=False) color = ChainedChoiceField(parent_field='model', ajax_url=reverse_lazy('ajax_chained_colors'), empty_label=_(u'Select a car model'), required=False) class Meta: model = Car fields = ['brand', 'model', 'engine', 'color', 'numberplate']
class OrderDetailForm(ChainedChoicesModelForm): class Meta: model = OrderDetail exclude = [] customer = autocomplete_light.forms.ModelChoiceField( Customer.objects.all(), widget=autocomplete_light.ChoiceWidget('CustomerAutocomplete'), label='Клиент') product = ChainedModelChoiceField(parent_field='category', ajax_url='/ajax/chained-products/', label='Товар', model=Product)
class OrderAppointmentForm(ChainedChoicesForm): def __init__(self, *arg, **kwarg): super(OrderAppointmentForm, self).__init__(*arg, **kwarg) self.empty_permitted = False category = forms.ModelChoiceField( ProductCategory.objects.filter(service=False), label='Категория') product = ChainedModelChoiceField(parent_field='category', ajax_url='/ajax/chained-products/', label='Товар', model=Product) quantity = forms.IntegerField(min_value=1, initial=1, label='Количество') cost = forms.DecimalField(min_value=0, label='Цена')
class ProjectForm(ChainedChoicesModelForm): release = forms.ModelChoiceField( Release.objects.all().order_by('-sequence'), empty_label=_(u'Select a Release'), # help_text=_(u'Select a VM Type'), label='Release', ) database_type = forms.ModelChoiceField( DBMode.objects.all(), empty_label=_(u'Select a Database Type'), # help_text=_(u'Select a VM Type'), label='Database Type', initial=DBMode.objects.all().filter(name='NDB')[0].pk, ) hardwareType = ModelChoiceField(queryset=HardwareType.objects.all(), required=True, empty_label=_(u'Select a hardware type'), label='Hardware Type') hardwareModel = ChainedModelChoiceField( parent_field='hardwareType', ajax_url=reverse_lazy('ajax_hardware_models'), empty_label=_(u'Select a CPU model'), model=HardwareModel, required=True, label='CPU Model') class Meta: model = Project fields = [ 'name', 'release', 'hardwareType', 'hardwareModel', 'customer', 'version', 'database_type', 'comment', ]
class ComputerForm(ChainedChoicesModelForm): sub_department = ChainedModelChoiceField('department', reverse_lazy('core:chained_sub_department'), SubDepartment, label="Sub Department") community = ModelChoiceField(queryset=Community.objects.all(), required=False) building = ChainedModelChoiceField('community', reverse_lazy('core:chained_building'), Building, required=False) room = ChainedModelChoiceField('building', reverse_lazy('core:chained_room'), Room, required=False) def __init__(self, *args, **kwargs): super(ComputerForm, self).__init__(*args, **kwargs) self.fields['display_name'].label = 'Computer Name' self.helper = FormHelper() self.helper.form_method = 'post' self.helper.html5_required = True self.helper.form_class = 'form-horizontal table-add-form' self.helper.label_class = 'col-sm-2' self.helper.field_class = 'col-sm-10 col-md-8' self.helper.layout = Layout( Fieldset( 'Add a new computer', Field('community', autocomplete='off'), Field('building', autocomplete='off'), Field('room', autocomplete='off'), Field('department', autocomplete='off'), Field('sub_department', autocomplete='off'), Field('display_name', placeholder=self.fields['display_name'].label), Field('mac_address', placeholder=self.fields['mac_address'].label), Field('ip_address', css_class="ip_address_field", placeholder=self.fields['ip_address'].label, title="Leave blank for DHCP."), Field('model', placeholder=self.fields['model'].label), Field('serial_number', placeholder=self.fields['serial_number'].label), Field('property_id', placeholder=self.fields['property_id'].label), Field('location', placeholder=self.fields['location'].label), Field('date_purchased', css_class="dateinput", placeholder=self.fields['date_purchased'].label), Field('dn', placeholder=self.fields['dn'].label), Field('description', placeholder=self.fields['description'].label), ), FormActions( Submit('submit', 'Add Computer'), ) ) self.fields["date_purchased"].widget.attrs['class'] = "dateinput" # Make error messages a bit more readable for field_name in self.fields: self.fields[field_name].error_messages = {'required': 'A ' + field_name + ' is required.'} def clean_dn(self): data = self.cleaned_data['dn'] dn_pieces = data.split(",") stripped_dn_pieces = [] for dn_piece in dn_pieces: try: group_type, group_string = dn_piece.split("=") except ValueError: self.add_error("dn", ValidationError("Please enter a valid DN.")) return data stripped_dn_pieces.append('%(type)s=%(string)s' % {'type': group_type.strip(), 'string': group_string.strip()}) return ', '.join(stripped_dn_pieces) def save(self, commit=True): computer = super().save(commit=False) computer.dns_name = computer.display_name.strip() + '.ad.calpoly.edu' if commit: computer.save() return computer class Meta: model = Computer fields = ['community', 'building', 'room', 'department', 'sub_department', 'display_name', 'mac_address', 'ip_address', 'model', 'serial_number', 'property_id', 'location', 'date_purchased', 'dn', 'description']