class LicencesFilterForm(BootstrapMixin, forms.ModelForm): q = forms.CharField(required=False, label="Search") software = DynamicModelMultipleChoiceField( queryset=Software.objects.all(), required=False, widget=APISelectMultiple(api_url="/api/plugins/licences/software/", )) site = DynamicModelMultipleChoiceField( queryset=Site.objects.all(), required=False, ) date_valid_after = forms.DateField(required=False, input_formats='%Y,%m,%d', widget=DatePicker()) date_valid_before = forms.DateField(required=False, input_formats='%Y,%m,%d', widget=DatePicker()) class Meta: model = Licence fields = [ "q", "site", "software", ]
class Meta: model = Circuit fields = [ 'cid', 'type', 'provider', 'status', 'install_date', 'commit_rate', 'description', 'tenant_group', 'tenant', 'comments', 'tags', ] fieldsets = ( ('Circuit', ('provider', 'cid', 'type', 'status', 'install_date', 'commit_rate', 'description', 'tags')), ('Tenancy', ('tenant_group', 'tenant')), ) help_texts = { 'cid': "Unique circuit ID", 'commit_rate': "Committed rate", } widgets = { 'status': StaticSelect(), 'install_date': DatePicker(), 'commit_rate': SelectSpeedWidget(), }
class Meta: model = Circuit fields = [ 'cid', 'type', 'provider', 'status', 'install_date', 'commit_rate', 'description', 'tenant_group', 'tenant', 'comments', 'tags', ] help_texts = { 'cid': "Unique circuit ID", 'commit_rate': "Committed rate", } widgets = { 'provider': APISelect(api_url="/api/circuits/providers/"), 'type': APISelect(api_url="/api/circuits/circuit-types/"), 'status': StaticSelect2(), 'install_date': DatePicker(), }
class Meta: nullable_fields = [ 'date_added', 'description', ] widgets = { 'date_added': DatePicker(), }
class Meta: model = Aggregate fields = [ 'prefix', 'rir', 'date_added', 'description', 'tags', ] help_texts = { 'prefix': "IPv4 or IPv6 network", 'rir': "Regional Internet Registry responsible for this prefix", } widgets = { 'date_added': DatePicker(), }
class Meta: model = Aggregate fields = [ 'prefix', 'rir', 'date_added', 'description', 'tenant_group', 'tenant', 'tags', ] fieldsets = ( ('Aggregate', ('prefix', 'rir', 'date_added', 'description', 'tags')), ('Tenancy', ('tenant_group', 'tenant')), ) help_texts = { 'prefix': "IPv4 or IPv6 network", 'rir': "Regional Internet Registry responsible for this prefix", } widgets = { 'date_added': DatePicker(), }
class Meta: model = ASN fields = [ 'asn', 'rir', 'sites', 'tenant_group', 'tenant', 'description', 'tags' ] fieldsets = ( ('ASN', ('asn', 'rir', 'sites', 'description', 'tags')), ('Tenancy', ('tenant_group', 'tenant')), ) help_texts = { 'asn': "AS number", 'rir': "Regional Internet Registry responsible for this prefix", } widgets = { 'date_added': DatePicker(), }
def to_form_field(self, set_initial=True, enforce_required=True, for_csv_import=False): """ Return a form field suitable for setting a CustomField's value for an object. set_initial: Set initial date for the field. This should be False when generating a field for bulk editing. enforce_required: Honor the value of CustomField.required. Set to False for filtering/bulk editing. for_csv_import: Return a form field suitable for bulk import of objects in CSV format. """ initial = self.default if set_initial else None required = self.required if enforce_required else False # Integer if self.type == CustomFieldTypeChoices.TYPE_INTEGER: field = forms.IntegerField( required=required, initial=initial, min_value=self.validation_minimum, max_value=self.validation_maximum ) # Boolean elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN: choices = ( (None, '---------'), (True, 'True'), (False, 'False'), ) field = forms.NullBooleanField( required=required, initial=initial, widget=StaticSelect(choices=choices) ) # Date elif self.type == CustomFieldTypeChoices.TYPE_DATE: field = forms.DateField(required=required, initial=initial, widget=DatePicker()) # Select elif self.type in (CustomFieldTypeChoices.TYPE_SELECT, CustomFieldTypeChoices.TYPE_MULTISELECT): choices = [(c, c) for c in self.choices] default_choice = self.default if self.default in self.choices else None if not required or default_choice is None: choices = add_blank_choice(choices) # Set the initial value to the first available choice (if any) if set_initial and default_choice: initial = default_choice if self.type == CustomFieldTypeChoices.TYPE_SELECT: field_class = CSVChoiceField if for_csv_import else forms.ChoiceField field = field_class( choices=choices, required=required, initial=initial, widget=StaticSelect() ) else: field_class = CSVChoiceField if for_csv_import else forms.MultipleChoiceField field = field_class( choices=choices, required=required, initial=initial, widget=StaticSelectMultiple() ) # URL elif self.type == CustomFieldTypeChoices.TYPE_URL: field = LaxURLField(required=required, initial=initial) # Text else: field = forms.CharField(max_length=255, required=required, initial=initial) if self.validation_regex: field.validators = [ RegexValidator( regex=self.validation_regex, message=mark_safe(f"Values must match this regex: <code>{self.validation_regex}</code>") ) ] field.model = self field.label = str(self) if self.description: field.help_text = escape(self.description) return field
def to_form_field(self, set_initial=True, enforce_required=True, for_csv_import=False): """ Return a form field suitable for setting a CustomField's value for an object. set_initial: Set initial date for the field. This should be False when generating a field for bulk editing. enforce_required: Honor the value of CustomField.required. Set to False for filtering/bulk editing. for_csv_import: Return a form field suitable for bulk import of objects in CSV format. """ initial = self.default if set_initial else None required = self.required if enforce_required else False # Integer if self.type == CustomFieldTypeChoices.TYPE_INTEGER: field = forms.IntegerField(required=required, initial=initial) # Boolean elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN: choices = ( (None, '---------'), (1, 'True'), (0, 'False'), ) if initial is not None and initial.lower() in ['true', 'yes', '1']: initial = 1 elif initial is not None and initial.lower() in ['false', 'no', '0']: initial = 0 else: initial = None field = forms.NullBooleanField( required=required, initial=initial, widget=StaticSelect2(choices=choices) ) # Date elif self.type == CustomFieldTypeChoices.TYPE_DATE: field = forms.DateField(required=required, initial=initial, widget=DatePicker()) # Select elif self.type == CustomFieldTypeChoices.TYPE_SELECT: choices = [(cfc.pk, cfc.value) for cfc in self.choices.all()] if not required: choices = add_blank_choice(choices) # Set the initial value to the PK of the default choice, if any if set_initial: default_choice = self.choices.filter(value=self.default).first() if default_choice: initial = default_choice.pk field_class = CSVChoiceField if for_csv_import else forms.ChoiceField field = field_class( choices=choices, required=required, initial=initial, widget=StaticSelect2() ) # URL elif self.type == CustomFieldTypeChoices.TYPE_URL: field = LaxURLField(required=required, initial=initial) # Text else: field = forms.CharField(max_length=255, required=required, initial=initial) field.model = self field.label = self.label if self.label else self.name.replace('_', ' ').capitalize() if self.description: field.help_text = self.description return field
def get_custom_fields_for_model(content_type, filterable_only=False, bulk_edit=False): """ Retrieve all CustomFields applicable to the given ContentType """ field_dict = OrderedDict() custom_fields = CustomField.objects.filter(obj_type=content_type) if filterable_only: custom_fields = custom_fields.exclude(filter_logic=CF_FILTER_DISABLED) for cf in custom_fields: field_name = 'cf_{}'.format(str(cf.name)) initial = cf.default if not bulk_edit else None # Integer if cf.type == CF_TYPE_INTEGER: field = forms.IntegerField(required=cf.required, initial=initial) # Boolean elif cf.type == CF_TYPE_BOOLEAN: choices = ( (None, '---------'), (1, 'True'), (0, 'False'), ) if initial is not None and initial.lower() in ['true', 'yes', '1']: initial = 1 elif initial is not None and initial.lower() in [ 'false', 'no', '0' ]: initial = 0 else: initial = None field = forms.NullBooleanField( required=cf.required, initial=initial, widget=StaticSelect2(choices=choices)) # Date elif cf.type == CF_TYPE_DATE: field = forms.DateField(required=cf.required, initial=initial, widget=DatePicker()) # Select elif cf.type == CF_TYPE_SELECT: choices = [(cfc.pk, cfc) for cfc in cf.choices.all()] if not cf.required or bulk_edit or filterable_only: choices = [(None, '---------')] + choices # Check for a default choice default_choice = None if initial: try: default_choice = cf.choices.get(value=initial).pk except ObjectDoesNotExist: pass field = forms.TypedChoiceField(choices=choices, coerce=int, required=cf.required, initial=default_choice, widget=StaticSelect2()) # URL elif cf.type == CF_TYPE_URL: field = LaxURLField(required=cf.required, initial=initial) # Text else: field = forms.CharField(max_length=255, required=cf.required, initial=initial) field.model = cf field.label = cf.label if cf.label else cf.name.replace( '_', ' ').capitalize() if cf.description: field.help_text = cf.description field_dict[field_name] = field return field_dict