class SafetyIssueCreationForm(ModelForm): report_anonymously = BooleanField(required=False, initial=False) class Meta: model = SafetyIssue fields = ['reporter', 'concern', 'location'] def __init__(self, user, *args, **kwargs): super(SafetyIssueCreationForm, self).__init__(*args, **kwargs) self.user = user def clean_update(self): return self.cleaned_data['concern'].strip() def clean_location(self): return self.cleaned_data['location'].strip() def save(self, commit=True): instance = super(SafetyIssueCreationForm, self).save(commit=False) if not self.cleaned_data['report_anonymously']: self.instance.reporter = self.user if commit: instance.save() return super(SafetyIssueCreationForm, self).save(commit=commit)
class ArticleForm(forms.Form): title = CharField(max_length=120) #Required has to be False, because i did not find a way that i could edit an article without uplouding an image again. header_image = ImageField(required=False) is_read = BooleanField(required=False, initial=False) # is_read = TypedChoiceField( #choices=((True, 'Yes'), (False, 'No')), # widget=CheckBox, #coerce=lambda x: x == 'True', #initial="False", # required=False #) text = CharField() category = ChoiceField(choices=CATEGORIES, required=False) class Meta: #The two below has something to do with assigning who the author of the article is model = Article exclude = ('user', ) #Check if the things that is written in the form are valid def clean(self): return self.cleaned_data
class ContactUpdateModelForm(FormCleanContactNumber, ModelForm): has_confirmed = BooleanField(initial=False, required=False) need_to_confirm = False class Meta: model = default_contact_model fields = default_contact_fields def __init__(self, *args, **kwargs): if 'request' in kwargs.keys(): self.request = kwargs.pop('request') super().__init__(*args, **kwargs) def clean(self): # if name exists make user confirm cleaned_data = super().clean() name = cleaned_data.get('name', '') name_initial = self.initial['name'] has_confirmed = cleaned_data['has_confirmed'] self.need_to_confirm = False contacts = self.Meta.model.objects.filter(name__iexact=name) has_changed = name_initial.lower() != name.lower() if contacts.exists() and has_changed: if not has_confirmed: msg = 'A contact with name "{}"\ already exists. Please confirm to continue' msg = msg.format(name) self.add_error('has_confirmed', msg) self.need_to_confirm = True if hasattr(self, 'request'): messages.warning(self.request, msg) return cleaned_data
class CoffeeMeetingForm(ModelForm): class Meta: model = CoffeeMeeting fields = ['title', 'content', 'cafe', 'meeting_date', 'max_participants'] widgets = {'meeting_date': DateTimeInput(attrs={'id': 'inline_datetimepicker'})} images = forms.FileField(widget=ClearableFileInput(attrs={'multiple': True}), label='카페 사진', help_text='카페 사진이 있으면 첨부해주세요!', required=False) save_cafephoto = BooleanField(required=False,initial=False, label='카페 정보에 사진을 등록할까요?', help_text='체크하면 카페 정보가 업데이트됩니다! 본인이 찍은 사진이면 등록해주세요!') def __init__(self, *args, **kwargs): # kwargs에서 form을 만드는데 필요없는 view에서 넘겨준 부가정보를 먼저 빼낸다 self.request = kwargs.pop('request', None) self.cafe = kwargs.pop('cafes') read_only = kwargs.pop('read_only') # form을 생성하고 필요한 처리를 한다 super(CoffeeMeetingForm, self).__init__(*args, **kwargs) self.fields['cafe'].initial = self.cafe # UpdateView에서 넘어온 경우 cafe를 활성화한다 if read_only: self.fields['cafe'].widget.attrs['readonly'] = True self.fields['meeting_date'].input_formats = ["%Y-%m-%d %I:%M %p"] def clean_cafe(self): # 카페의 경우 항상 url 인자로 넘어온 카페를 리턴해야 한다 return self.cafe def save(self, commit=True): instance = super(CoffeeMeetingForm, self).save(commit=False) instance.author = self.request.user instance.cafe = self.cafe instance.save() return instance
class CodeGlobalPermissionsForm(Form): onlyAuthUsers = BooleanField( label="Solo permitir acceso por usuarios autenticados?", required=False) onlyCollaborators = BooleanField( label="Solo permitir acceso por usuarios explicitamente permitidos?", required=False) globalCanWrite = BooleanField( label="Acceso global al grupo definido anteriormente de escritura?", required=False) globalCanRead = BooleanField( label="Acceso global al grupo definido anteriormente de lectura?", required=False) globalCanExecute = BooleanField( label="Acceso global al grupo definido anteriormente de ejecucion?", required=False) globalCanDownload = BooleanField( label="Acceso global al grupo definido anteriormente de descargar?", required=False)
class WorkerForm(DocumentForm): """ Worker form representation """ h2direct = BooleanField(required=False, widget=CheckboxInput(attrs={"class": "js-switch"})) h2moderntlsonly = BooleanField(required=False, widget=CheckboxInput(attrs={"class": "js-switch"})) h2push = BooleanField(required=False, widget=CheckboxInput(attrs={"class": "js-switch"})) h2direct = BooleanField(required=False, widget=CheckboxInput(attrs={"class": "js-switch"})) h2serializeheaders = BooleanField(required=False, widget=CheckboxInput(attrs={"class": "js-switch"})) h2upgrade = BooleanField(required=False, widget=CheckboxInput(attrs={"class": "js-switch"})) def __init__(self, *args, **kwargs): super(WorkerForm, self).__init__(*args, **kwargs) self = bootstrap_tooltips(self) class Meta: document = Worker widgets = { 'name': TextInput(attrs={'class': 'form-control'}), 'gracefulshutdowntimeout': TextInput(attrs={'class': 'form-control'}), 'maxconnectionsperchild': TextInput(attrs={'class': 'form-control'}), 'minsparethreads': TextInput(attrs={'class': 'form-control'}), 'maxsparethreads': TextInput(attrs={'class': 'form-control'}), 'serverlimit': TextInput(attrs={'class': 'form-control'}), 'threadsperchild': TextInput(attrs={'class': 'form-control'}), 'rate_limit': TextInput(attrs={'class': 'form-control'}), 'req_timeout_header': TextInput(attrs={'class': 'form-control'}), 'req_timeout_body': TextInput(attrs={'class': 'form-control'}), 'req_timeout_header_rate': TextInput(attrs={'class': 'form-control'}), 'req_timeout_body_rate': TextInput(attrs={'class': 'form-control'}), 'timeout': TextInput(attrs={'class': 'form-control'}), 'maxkeepaliverequests': TextInput(attrs={'class': 'form-control'}), 'keepalivetimeout': TextInput(attrs={'class': 'form-control'}), 'h2maxsessionstreams': TextInput(attrs={'class': 'form-control'}), 'h2maxworkeridleseconds': TextInput(attrs={'class': 'form-control'}), 'h2minworkers': TextInput(attrs={'class': 'form-control'}), 'h2maxworkers': TextInput(attrs={'class': 'form-control'}), 'h2streammaxmemsize': TextInput(attrs={'class': 'form-control'}), 'h2tlscooldownsecs': TextInput(attrs={'class': 'form-control'}), 'h2tlswarmupsize': TextInput(attrs={'class': 'form-control'}), 'h2windowsize': TextInput(attrs={'class': 'form-control'}), }
class ReaderStudyCopyForm(Form): title = CharField(required=True) copy_images = BooleanField(required=False, initial=True) copy_hanging_list = BooleanField(required=False, initial=True) copy_case_text = BooleanField(required=False, initial=True) copy_questions = BooleanField(required=False, initial=True) copy_readers = BooleanField(required=False, initial=True) copy_editors = BooleanField(required=False, initial=True) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper(self) self.helper.layout.append(Submit("save", "Copy")) def clean(self, *args, **kwargs): if ( self.cleaned_data["copy_hanging_list"] or self.cleaned_data["copy_case_text"] ) and not self.cleaned_data["copy_images"]: self.add_error( error="Hanging list and case text can only be copied if the images are copied as well", field=None, )
class CuratorSearchForm(Form): fullSearch = CharField(max_length=255, required=False) useMUIS = BooleanField(initial=False, required=False) useDIGAR = BooleanField(initial=False, required=False) useMKA = BooleanField(initial=False, required=False) useETERA = BooleanField(initial=False, required=False) useFlickr = BooleanField(initial=False, required=False) useUTLIB = BooleanField(initial=False, required=False) useFinna = BooleanField(initial=False, required=False) useCommons = BooleanField(initial=False, required=False) useEuropeana = BooleanField(initial=False, required=False) useFotis = BooleanField(initial=False, required=False) # Also used for Finna and Fotis flickrPage = IntegerField(initial=1, required=False) filterExisting = BooleanField(initial=True, required=False) ids = NotValidatedMultipleChoiceField(coerce=str, required=False)
def test_boolean_picklable(self): self.assertIsInstance(pickle.loads(pickle.dumps(BooleanField())), BooleanField)
class SummaryActionForm(QueryActionForm): size = IntegerField(initial=40) offset = IntegerField(initial=0) aggregations = BooleanField(initial=True, required=False)
class ScriptForm(ModelForm): script_type = CharField( label="Script type", required=False, help_text="Script type", initial=str(SCRIPT_TYPE.TESTING), ) hardware_type = CharField( label="Hardware type", required=False, help_text="The hardware type the script configures or tests.", initial=str(HARDWARE_TYPE.NODE), ) parallel = CharField( label="Parallel", required=False, help_text="Whether the script may run in parallel with other scripts.", initial=str(SCRIPT_PARALLEL.DISABLED), ) packages = CharField( label="Packages", required=False, help_text="Packages to be installed with script.", initial="", ) timeout = DurationField( label="Timeout", required=False, help_text="Timeout", initial=timedelta(0), ) script = VersionedTextFileField(label="Script", help_text="Script content") comment = CharField( label="Comment", required=False, help_text="Description of change", initial="", ) for_hardware = CharField( label="For hardware", required=False, help_text="Hardware identifiers this script requires to run.", initial="", ) apply_configured_networking = BooleanField(required=False) class Meta: model = Script fields = ( "name", "title", "description", "tags", "script_type", "hardware_type", "parallel", "packages", "timeout", "destructive", "script", "for_hardware", "may_reboot", "recommission", "apply_configured_networking", ) def __init__(self, instance=None, data=None, edit_default=False, **kwargs): self.edit_default = edit_default if instance is None: script_data_key = "data" else: script_data_key = "new_data" data = data.copy() if "comment" in data and "script" in data: script_data = { "comment": data.get("comment"), script_data_key: data.get("script"), } data["script"] = script_data data.pop("comment") # Alias type to script_type to allow for consistent naming in the API. if "type" in data and "script_type" not in data: data["script_type"] = data["type"] # self.data is a QueryDict. pop returns a list containing the value # while directly accessing it returns just the value. data.pop("type") super().__init__(instance=instance, data=data, **kwargs) if instance is None: for field in ["name", "script"]: self.fields[field].required = True else: for field in ["name", "script"]: self.fields[field].required = False self.fields["script"].initial = instance.script # Reading the embedded YAML must happen at the end of initialization # so the fields set are validated. if "script" in self.data: self._read_script() def _validate_results(self, results={}): valid = True if isinstance(results, list): for result in results: if not isinstance(result, str): set_form_error( self, "results", "Each result in a result list must be a string.", ) valid = False elif isinstance(results, dict): for result in results.values(): if not isinstance(result, dict): set_form_error( self, "results", "Each result in a result dictionary must be a " "dictionary.", ) elif "title" not in result: set_form_error( self, "results", "title must be included in a result dictionary.", ) valid = False else: for key in ["title", "description"]: if key in result and not isinstance(result[key], str): set_form_error(self, "results", "%s must be a string." % key) valid = False else: set_form_error( self, "results", "results must be a list of strings or a dictionary of " "dictionaries.", ) valid = False return valid def _clean_script(self, parsed_yaml): """Clean script data and validate input.""" # Tags and timeout may not be updated from new embedded YAML. This # allows users to receive updated scripts from an upstream maintainer, # such as Canonical, while maintaining user defined tags and timeout. # Tags must be a comma seperated string for the form. tags = parsed_yaml.pop("tags", None) if (tags is not None and self.instance.id is None and "tags" not in self.data): tags_valid = True if isinstance(tags, str): self.data["tags"] = tags elif isinstance(tags, list): for tag in tags: if not isinstance(tag, str): tags_valid = False continue if tags_valid: self.data["tags"] = ",".join(tags) else: tags_valid = False if not tags_valid: set_form_error( self, "tags", "Embedded tags must be a string of comma seperated " "values, or a list of strings.", ) # Timeout must be a string for the form. timeout = parsed_yaml.pop("timeout", None) if (timeout is not None and self.instance.id is None and "timeout" not in self.data): self.data["timeout"] = str(timeout) # Packages and for_hardware must be a JSON string for the form. for key in ["packages", "for_hardware"]: value = parsed_yaml.pop(key, None) if value is not None and key not in self.data: self.data[key] = json.dumps(value) for key, value in parsed_yaml.items(): if key in self.fields: error = False if key not in self.data: self.data[key] = value elif key == "script_type": # The deprecated Commissioning API always sets the # script_type to commissioning as it has always only # accepted commissioning scripts while the form sets # the default type to testing. If the YAML matches the # type allow it. try: if translate_script_type( value) != translate_script_type( self.data[key]): error = True except ValidationError: error = True elif value != self.data[key]: # Only allow form data for fields defined in the YAML if # the data matches. error = True if error: set_form_error( self, key, "May not override values defined in embedded YAML.", ) def _read_script(self): """Read embedded YAML configuration in a script. Search for supported MAAS script metadata in the script and read the values. Leading '#' are ignored. If the values are fields they will be entered in the form. """ yaml_delim = re.compile( r"\s*#\s*-+\s*(Start|End) MAAS (?P<version>\d+\.\d+) " r"script metadata\s+-+", re.I, ) found_version = None yaml_content = "" if isinstance(self.data["script"], dict): if "new_data" in self.data["script"]: script = self.data["script"]["new_data"] else: script = self.data["script"]["data"] else: script = self.data["script"] script_splitlines = script.splitlines() if len(script_splitlines) >= 1 and not script_splitlines[0].startswith( "#!/"): set_form_error(self, "script", "Must start with shebang.") for line in script_splitlines[1:]: m = yaml_delim.search(line) if m is not None: if found_version is None and m.group("version") == "1.0": # Found the start of the embedded YAML found_version = m.group("version") continue elif found_version == m.group("version"): # Found the end of the embedded YAML break elif found_version is not None and line.strip() != "": # Capture all lines inbetween the deliminator if "#" not in line: set_form_error(self, "script", 'Missing "#" on YAML line.') return yaml_content += "%s\n" % line.split("#", 1)[1] try: parsed_yaml = yaml.safe_load(yaml_content) except yaml.YAMLError as err: set_form_error(self, "script", "Invalid YAML: %s" % err) return if not isinstance(parsed_yaml, dict): return self.instance.results = parsed_yaml.pop("results", {}) self.instance.parameters = parsed_yaml.pop("parameters", {}) self._clean_script(parsed_yaml) def clean_packages(self): if self.cleaned_data["packages"] == "": return self.instance.packages else: packages = json.loads(self.cleaned_data["packages"]) # Automatically convert into a list incase only one package is # needed. for key in ["apt", "snap", "url"]: if key in packages and not isinstance(packages[key], list): packages[key] = [packages[key]] for key in ["apt", "url"]: if key in packages: for package in packages[key]: if not isinstance(package, str): set_form_error( self, "packages", "Each %s package must be a string." % key, ) if "snap" in packages: for package in packages["snap"]: if isinstance(package, dict): if "name" not in package or not isinstance( package["name"], str): set_form_error( self, "packages", "Snap package name must be defined.", ) if "channel" in package and package["channel"] not in [ "stable", "edge", "beta", "candidate", ]: set_form_error( self, "packages", "Snap channel must be stable, edge, beta, " "or candidate.", ) if "mode" in package and package["mode"] not in [ "classic", "dev", "jail", ]: set_form_error( self, "packages", "Snap mode must be classic, dev, or jail.", ) elif not isinstance(package, str): set_form_error(self, "packages", "Snap package must be a string.") return packages def clean_for_hardware(self): """Convert from JSON and validate for_hardware input.""" if self.cleaned_data["for_hardware"] == "": return self.instance.for_hardware try: for_hardware = json.loads(self.cleaned_data["for_hardware"]) except JSONDecodeError: for_hardware = self.cleaned_data["for_hardware"] if isinstance(for_hardware, str): for_hardware = for_hardware.split(",") if not isinstance(for_hardware, list): set_form_error(self, "for_hardware", "Must be a list or string") return regex = re.compile( r"^modalias:.+|pci:[\da-f]{4}:[\da-f]{4}|" r"usb:[\da-f]{4}:[\da-f]{4}|" r"system_vendor:.*|" r"system_product:.*|" r"system_version:.*|" r"mainboard_vendor:.*|" r"mainboard_product:.*$", re.I, ) for hw_id in for_hardware: if regex.search(hw_id) is None: set_form_error( self, "for_hardware", "Hardware identifier '%s' must be a modalias, PCI ID, " "USB ID, system vendor, system product, system version, " "mainboard vendor, or mainboard product." % hw_id, ) return for_hardware def clean(self): cleaned_data = super().clean() # If a field wasn't passed in keep the old values when updating. if self.instance.id is not None: for field in self._meta.fields: if field not in self.data: cleaned_data[field] = getattr(self.instance, field) script_type = cleaned_data["script_type"] if script_type == "": cleaned_data["script_type"] = self.instance.script_type else: try: cleaned_data["script_type"] = translate_script_type( script_type) except ValidationError as e: set_form_error(self, "script_type", e) hardware_type = cleaned_data["hardware_type"] if hardware_type == "": cleaned_data["hardware_type"] = self.instance.hardware_type else: try: cleaned_data["hardware_type"] = translate_hardware_type( hardware_type) except ValidationError as e: set_form_error(self, "hardware_type", e) parallel = cleaned_data["parallel"] if parallel == "": cleaned_data["parallel"] = self.instance.parallel else: try: cleaned_data["parallel"] = translate_script_parallel(parallel) except ValidationError as e: set_form_error(self, "parallel", e) return cleaned_data def is_valid(self): valid = super().is_valid() if valid and self.instance.default and not self.edit_default: for field in self.Meta.fields: if field in ["tags", "timeout"]: continue if field in self.data: set_form_error( self, field, "Not allowed to change on default scripts.", ) valid = False name = self.data.get("name") # none is used to tell the API to not run testing_scripts during # commissioning. if name is not None and name.lower() == "none": set_form_error(self, "name", '"none" is a reserved name.') valid = False # The name can't be a digit as MAAS allows scripts to be selected by # id. if name is not None and name.isdigit(): set_form_error(self, "name", "Cannot be a number.") valid = False if name is not None and pipes.quote(name) != name: set_form_error( self, "name", "Name contains disallowed characters, e.g. space or quotes.", ) valid = False # If comment and script exist __init__ combines both fields into a dict # to pass to VersionedTextFileField. if "comment" in self.data: set_form_error( self, "comment", '"comment" may only be used when specifying a "script" ' "as well.", ) valid = False if "script" in self.data: if not self._validate_results(self.instance.results): valid = False if "parameters" in self.data: params_form = ParametersForm(data=self.data.get("parameters")) if not params_form.is_valid(): valid = False if (not valid and self.instance.script_id is not None and self.initial.get("script") != self.instance.script_id and self.instance.script.id is not None): # If form validation failed cleanup any new VersionedTextFile # created by the VersionedTextFileField. self.instance.script.delete() return valid def save(self, *args, **kwargs): request = kwargs.pop("request", None) endpoint = kwargs.pop("endpoint", None) script = super(ScriptForm, self).save(*args, **kwargs) # Create audit event log if endpoint and request supplied. if request is not None and endpoint is not None: create_audit_event( EVENT_TYPES.SETTINGS, endpoint, request, None, description="Saved script '%s'." % script.name, ) return script
class UpdateCaseForm(XMLRPCUpdateCaseForm): is_automated_proposed = BooleanField(label='Autoproposed', required=False, widget=XMLRPCCheckboxInput)
class EntityModelCreateForm(ModelForm): default_coa = BooleanField(required=False, initial=False, label=_('Populate Default CoA')) activate_all_accounts = BooleanField(required=False, initial=False, label=_('Activate All Accounts')) generate_sample_data = BooleanField(required=False, initial=False, label=_('Fill With Sample Data?')) def clean_name(self): name = self.cleaned_data.get('name') if not name: raise ValidationError( _('Please provide a valid name for new Entity.')) if len(name) < 3: raise ValidationError( _('Looks like this entity name is too short...')) return name def clean(self): populate_coa = self.cleaned_data['default_coa'] activate_all_accounts = self.cleaned_data['activate_all_accounts'] sample_data = self.cleaned_data['generate_sample_data'] if sample_data and not all([populate_coa, activate_all_accounts]): raise ValidationError( f'Filling sample data requires using default CoA and activate all accounts.' ) validate_cszc(self.cleaned_data) class Meta: model = EntityModel fields = [ 'name', 'address_1', 'address_2', 'city', 'state', 'zip_code', 'country', 'email', 'website', 'phone', 'default_coa', 'activate_all_accounts', ] labels = { 'name': _('Entity Name'), } widgets = { 'name': TextInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-large', 'placeholder': _('Entity name...'), 'required': True }), 'address_1': TextInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('Address line 1') }), 'address_2': TextInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('Address line 2') }), 'city': TextInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('City') }), 'state': TextInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('State') }), 'zip_code': TextInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('Zip Code') }), 'country': TextInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('Country') }), 'phone': TextInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('Phone number...') }), 'email': EmailInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('Entity email...') }), 'website': URLInput( attrs={ 'class': DJANGO_LEDGER_FORM_INPUT_CLASSES, 'placeholder': _('http://www.mywebsite.com...') }), 'default_coa': CheckboxInput(attrs={'class': 'checkbox'}) }
def __init__(self, *args, **kwargs): """Custom model form constructor to account for custom related objects, i.e. instances of (subclasses of) `FilterSetFilterField`. """ super(FilterSetForm, self).__init__(*args, **kwargs) # Define all filter set filter fields for which we want form fields # The "choice" field contains the list of available options. # Multiselect form fields that load their choices asynchronously define # a "load" value, used to identify the DMARC aggregate report field # from which the options should be loaded. # See `views.choices_async` and `widgets.MultiSelectWidget` # The "label" value is used as text for the corresponding HTML label # element, "class" identifies the corresponding model class (subclass # of `FilterSetFilterField`), and "type" defines the allowed type of # the form value, used for Django's auto sanitization/type coercion. self.multiselect_filter_fields = { "report_sender": { "load": "reporter", "label": "Reporter(s)", "class": ReportSender, "type": str }, "report_receiver_domain": { "load": "reportee", "label": "Reportee(s)", "class": ReportReceiverDomain, "type": str }, "raw_dkim_domain": { "load": "dkim_domain", "label": "DKIM Domain(s)", "class": RawDkimDomain, "type": str }, "raw_spf_domain": { "load": "spf_domain", "label": "SPF Domain(s)", "class": RawSpfDomain, "type": str }, "raw_dkim_result": { "choices": choices.DKIM_RESULT, "label": "DKIM Result(s)", "class": RawDkimResult, "type": int }, "raw_spf_result": { "choices": choices.SPF_RESULT, "label": "SPF Result(s)", "class": RawSpfResult, "type": int }, "aligned_dkim_result": { "choices": choices.DMARC_RESULT, "label": "Aligned DKIM Result(s)", "class": AlignedDkimResult, "type": int }, "aligned_spf_result": { "choices": choices.DMARC_RESULT, "label": "Aligned SPF Result(s)", "class": AlignedSpfResult, "type": int }, "disposition": { "choices": choices.DISPOSITION_TYPE, "label": "Disposition(s)", "class": Disposition, "type": int } } # Initialize `FilterSetFilterField` form fields from dict defined above # The keyword arguments passed to the `MultiSelectWidget` constructor # are only relevant for multiselect elements that load their options # dynamically. for field_name, field_dict in self.multiselect_filter_fields.items(): self.fields[field_name] = AsyncTypedMultipleChoiceField( coerce=field_dict.get("type"), required=False, label=field_dict.get("label"), choices=field_dict.get("choices", ()), widget=MultiSelectWidget( **{ "load": field_dict.get("load", ""), "action": reverse("choices_async") })) # If the corresponding model objects already exists (i.e. on edit) # the selected options should also be shown as selected in the form if self.instance.id: self.fields[field_name].initial = list( field_dict["class"].objects.filter( foreign_key=self.instance.id).values_list("value", flat=True)) # For multiselect fields that load their options asynchronously # we have to add already selected options to the field's # choices attribute if not field_dict.get("choices"): self.fields[field_name].choices = [ (value, value) for value in self.fields[field_name].initial ] # Define additional non-multiselect filter set filter field form fields # Currently, there can at most be one `SourceIP` and one `MultipleDkim` # object on a filter set. See `models.FilterSetFilterField`'s docstring # for more info about why these filters have their own class and aren't # attributes of a `FilterSet` object. self.fields["source_ip"] = GenericIPAddressField( required=False, label="Mail Sender IP") self.fields["multiple_dkim"] = BooleanField(required=False, label="Multiple DKIM only") # Fill fields with existing data if any if self.instance.id: source_ip_initial = SourceIP.objects.filter( foreign_key=self.instance.id).values_list("value", flat=True) if len(source_ip_initial): self.fields["source_ip"].initial = source_ip_initial[0] multiple_dkim_initial = MultipleDkim.objects.filter( foreign_key=self.instance.id).values_list("value", flat=True) if len(multiple_dkim_initial): self.fields["multiple_dkim"].initial = multiple_dkim_initial[0]
class MemberForm(Form): becomeMember = BooleanField(label=_('Become member'), help_text=_('Want to become a member?'))
class RegistrationForm(Form): agreement_accepted = BooleanField(required=True) agreement_accepted.label = "I have read the contract and agree to terms"
class EditUserProfileForm(ModelForm): MANDATORY_FIELDS = ['username', 'group'] class Meta: model = UserProfile fields = localized_fields = [ 'username', 'first_name', 'last_name', 'email', 'tally', ] widgets = { 'username': TextInput(attrs={'size': 50}), 'first_name': TextInput(attrs={'size': 50}), 'last_name': TextInput(attrs={'size': 50}), 'email': TextInput(attrs={'size': 50}), } qs = Group.objects.exclude( name__in=[groups.SUPER_ADMINISTRATOR, groups.TALLY_MANAGER]) group = ModelChoiceField(queryset=qs, required=True) reboot_password = BooleanField(label=_('Reset password'), widget=CheckboxInput()) def __init__(self, *args, **kwargs): if 'instance' in kwargs and kwargs['instance']: initial = kwargs.setdefault('initial', {}) initial['group'] = kwargs['instance'].groups.first() super(EditUserProfileForm, self).__init__(*args, **kwargs) if 'instance' not in kwargs or not kwargs['instance']: self.fields['reboot_password'].widget = HiddenInput() for key in self.fields: if key not in self.MANDATORY_FIELDS: self.fields[key].required = False if self.initial.get('tally_id'): self.fields['tally'].initial = self.initial.get('tally_id') self.fields['tally'].widget = HiddenInput() def save(self): user = super(EditUserProfileForm, self).save() group = self.cleaned_data.get('group') reboot_password = self.cleaned_data.get('reboot_password') user.groups.clear() user.groups.add(group) if self.initial.get('tally_id'): tally = Tally.objects.get(id=self.initial.get('tally_id')) user.tally = tally user.save() if not user.password or reboot_password: user.set_password(user.username) user.reset_password = True user.save() return user
class StudyMetadataForm(ModelForm): start_date = DateField(widget=DateInput(format='%d/%m/%Y'), input_formats=('%d/%m/%Y',), required=False) end_date = DateField(widget=DateInput(format='%d/%m/%Y'), input_formats=('%d/%m/%Y',)) draft = BooleanField(required=False) class Meta: model = Study fields = ('study_type', 'title', 'url', 'blossom', 'requested_by', 'start_date', 'end_date', 'draft', 'lead_author', 'other', 'purpose_and_target', 'additional_information', 'phase_of_policy', 'additional_information_phase', 'foresight_approaches', 'additional_information_foresight', 'stakeholder_participation', 'additional_information_stakeholder', 'environmental_themes', 'geographical_scope', 'countries') def __init__(self, *args, **kwargs): self.study = kwargs.get('instance', None) self.formset = kwargs.pop('formset', None) self.user_id = kwargs.pop('user_id', None) super(StudyMetadataForm, self).__init__(*args, **kwargs) self.fields['purpose_and_target'].required = True self.fields['geographical_scope'].required = True self.fields['foresight_approaches'].required = False self.fields['study_type'].required = False self.fields['geographical_scope'].queryset = ( GeographicalScope.objects.filter(is_deleted=False)) self.fields['countries'].queryset = ( Country.objects.filter(is_deleted=False)) self.fields['environmental_themes'].queryset = ( EnvironmentalTheme.objects .filter(is_deleted=False) .order_by('title') ) def clean(self): cleaned_data = super(StudyMetadataForm, self).clean() if self.study: cleaned_data['study_type'] = self.study.study_type if not self.formset.is_valid(): self._errors['language'] = self.error_class( ['Language and original title are required.']) start_date_data = cleaned_data.get('start_date') blossom_data = cleaned_data.get('blossom') start_date = self.fields['start_date'] if blossom_data == Study.YES: if start_date_data in start_date.empty_values: self._errors['start_date'] = self.error_class( [start_date.error_messages['required']]) cleaned_data.pop('start_date', None) geographical_scope_data = cleaned_data.get('geographical_scope') countries_data = cleaned_data.get('countries') countries = self.fields['countries'] if (geographical_scope_data and geographical_scope_data.require_country): if len(countries_data) == 0: self._errors['countries'] = self.error_class( [countries.error_messages['required']]) cleaned_data.pop('countries', None) if (cleaned_data['study_type'] == 'activity' and not cleaned_data['phase_of_policy']): self._errors['phase_of_policy'] = self.error_class( [self.fields['phase_of_policy'].error_messages['required']]) cleaned_data.pop('phase_of_policy', None) return cleaned_data def save(self): study = super(StudyMetadataForm, self).save(commit=False) study.user_id = self.user_id study.save() # save languages self.formset.save(study) self.save_m2m() return study
def test_booleanfield(self): e = { 'required': 'REQUIRED', } f = BooleanField(error_messages=e) self.assertFormErrors(['REQUIRED'], f.clean, '')
class ChooseExportFieldsForm(forms.Form): recipients = forms.ModelMultipleChoiceField( label=_('Recipients'), queryset=get_user_model().objects.all(), # widget=UsersWidget(), required=True, ) filename = forms.CharField(label=_('File name')) select_all = BooleanField(label=_('Select all'), widget=CheckboxInput(attrs={'class': 'all'}), required=False) def __init__(self, *args, **kwargs): self.selectable_fields = kwargs.pop('selectable_fields') self.permitted_fields = kwargs.pop('permitted_fields', []) # necessary to reset dynamic fields, as they will be added again at page reload self.fields = {} super().__init__(*args, **kwargs) self.static_fields = list(self.fields.keys()) self.prepare_fields() self.build_layouts() self.helper = SingleSubmitFormHelper() self.helper.form_tag = False self.helper.layout = Layout(*self.layouts) def prepare_fields(self): if self.selectable_fields: field_index = 0 for label, field_group in self.selectable_fields.items(): choices = [] for field in field_group: # disable choice if user doesn't have permission if self.permitted_fields == True or field[ 0] in self.permitted_fields: choices.append(field[:2]) else: choices.append((field[0], { 'label': field[1], 'disabled': True })) choices = tuple(choices) initial = tuple([ field[0] for field in field_group if self.permitted_fields == True or field[0] in self.permitted_fields ]) field_key = 'field_group_{}'.format(field_index) self.fields[field_key] = forms.MultipleChoiceField( label='', widget=CheckboxSelectMultipleWithDisabled( attrs={'class': 'option'}), choices=choices, initial=initial, required=False) # group checkbox group_key = 'group_{}'.format(field_index) self.fields[group_key] = BooleanField( label=label, widget=CheckboxInput(attrs={'class': 'group-option'}), required=False) field_index += 1 def build_layouts(self): static_fields_layout = [ Row( Div('recipients', css_class='col-lg-3'), Div('filename', css_class='col-lg-3'), ), ] # build dynamic fields layout dynamic_fields_layout = [] for field_key, field in self.fields.items(): # if not static field if field_key.startswith( 'field') and not field_key in self.static_fields: group_key = field_key.strip('field_') dynamic_fields_layout.append( Div(Legend(group_key), Div(field_key), css_class='col-md-2 group')) if dynamic_fields_layout: static_fields_layout.append( Row(Div('select_all', css_class='col-md-3'), ), ) self.layouts = [ *static_fields_layout, Row(*dynamic_fields_layout), ] def clean(self): cleaned_data = super().clean() cleaned_data.pop('select_all') if self.selectable_fields: export_fields = [] for key in list(cleaned_data.keys()): if key.startswith('group'): cleaned_data.pop(key) elif key not in self.static_fields: export_fields.append(cleaned_data[key]) if not any(export_fields): raise forms.ValidationError(_('Select at least one option')) return cleaned_data
class EditPlanForm(XMLRPCEditPlanForm): is_active = BooleanField(label="Active", required=False, widget=XMLRPCCheckboxInput)
class Task309Form(Form): a = ComplexField(required=True) b = ComplexField(required=True) c = ComplexField(required=True) can_into_complex = BooleanField(required=False)
class BookingForm(ModelForm): """ Control booking creation here. """ optimised = BooleanField( required=False) # Ask if we want to optimise, e.g. combine tables. class Meta: model = Booking fields = ['restaurant', 'party_size', 'date', 'time', 'length'] widgets = { 'restaurant': TextInput(), 'date': SelectDateWidget(years=(datetime.date.today().year, datetime.date.today().year + 1)) } def __init__(self, restaurant, *args, **kwargs): """ Assign the restaurant and it's initial value - annoyingly this is it's pk for now. Make this readonly. Set time limits for booking: - Only allow bookings within the opening hours of a restaurant. - Don't allow more than 2 hours (Assuming this is a limit). :param restaurant: Restaurant obj, the restaurant we are booking for. """ super(BookingForm, self).__init__(*args, **kwargs) self.restaurant = restaurant self.fields['restaurant'].initial = restaurant self.fields['restaurant'].widget.attrs['readonly'] = True self.fields['time'].widget = Select(choices=available_times( restaurant.opening_time, restaurant.closing_time)) self.fields['length'].widget = Select(choices=available_times( datetime.time(0, 0, 0), datetime.time(3, 15, 0))) def clean(self): """ Do some sense checking. Make sure the bookings are within the time limits of the restaurant. Ensure a time has been given. Figure out extra parameters for the Booking and remove excess stuff (e.g. optimise). """ cleaned_data = super(BookingForm, self).clean() time = cleaned_data.get('time') length = cleaned_data.get('length') if time and length: if (datetime.datetime.combine(datetime.date.today(), time) + length).time() > self.restaurant.closing_time: raise ValidationError( 'Booking exceeds restaurant closing time.') else: raise ValidationError('No time or length given.') # Great, timing is valid - lets get the end time to store. end_time = (datetime.datetime.combine(datetime.date.today(), time) + length).time() cleaned_data['end_time'] = end_time # Now we need to find any spare tables. date = cleaned_data.get('date') party = cleaned_data.get('party_size') cleaned_data['table'] = self.restaurant.find_table( date, time, end_time, party, optimise=cleaned_data.pop('optimised')) if not cleaned_data[ 'table']: # If there aren't any tables lets alert the customer raise ValidationError('No tables available for this time.')
class UserForm(ModelForm): password = CharField( widget=PasswordInput(attrs={ 'class': 'form-control', 'placeholder': 'Escribe contraseña' }), label='Contraseña', validators=[password_validator], error_messages={ 'required': 'La contraseña es requerida, favor de completarla.' }) password_re = CharField( widget=PasswordInput(attrs={ 'class': 'form-control', 'placeholder': 'Repite contraseña' }), label='Confirmación de contraseña', validators=[password_validator], error_messages={ 'required': 'La confirmación de la contraseña es requerida, favor de completarla.' }) username = CharField( widget=TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Nombre de usuario' }), label='Nombre de usuario', error_messages={ 'required': 'El nombre de usuario es requerido,' + ' favor de completarlo.' }, validators=[username_validator]) email = CharField( widget=TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Correo electrónico' }), label='Correo electrónico', error_messages={ 'invalid': 'Favor de ingresar un formato de correo válido.', 'required': 'El correo es requerido, favor de completarlo.' }) is_superuser = BooleanField(widget=CheckboxInput(), label="¿Es director operativo?", required=False) class Meta: model = User fields = [ 'username', 'email', 'password', 'password_re', 'is_superuser' ] def save(self, commit=True): user = super(UserForm, self).save(commit=False) user.set_password(self.cleaned_data['password']) if commit: user.save() return user def clean_password(self): if not self.data['password']: raise ValidationError( "La contraseña es requerida, favor de completarla.") return self.data['password'] def clean_password_re(self): if self.data['password_re'].find(" ") > 0: raise ValidationError(PASSWORD_INCORRECTO) if self.data['password_re'] != self.data['password']: raise ValidationError("Las contraseñas no coinciden.") if not self.data['password_re']: raise ValidationError( "La confirmación de la contraseña es requerida, favor de completarla." ) return self.data['password_re'] def clean_username(self): username = self.data['username'] r = User.objects.filter(username=username) if r.count(): raise ValidationError( "El usuario ingresado ya se encuentra registrado en el" + " sistema, favor de verificarlo.") if len(username) > 20: raise ValidationError( "El nombre de usuario debe contener máximo 20 caracteres.") if len(username) < 8: raise ValidationError( "El nombre de usuario debe contener mínimo 8 caracteres.") if not self.data['username']: raise ValidationError( "El nombre de usuario es requerido, favor de completarlo.") return username def clean_email(self): email = self.cleaned_data['email'].lower() r = User.objects.filter(email=email) if r.count(): raise ValidationError( "El correo electrónico ingresado ya se encuentra " + "registrado en el sistema. Favor de verificarlo.") return email
class TripForm(TripFormBase): day_11 = BooleanField(label='day_11', required=False) day_12 = BooleanField(label='day_12', required=False) day_13 = BooleanField(label='day_13', required=False) day_14 = BooleanField(label='day_14', required=False) day_15 = BooleanField(label='day_15', required=False) day_16 = BooleanField(label='day_16', required=False) day_17 = BooleanField(label='day_17', required=False) day_21 = BooleanField(label='day_21', required=False) day_22 = BooleanField(label='day_22', required=False) day_23 = BooleanField(label='day_23', required=False) day_24 = BooleanField(label='day_24', required=False) day_25 = BooleanField(label='day_25', required=False) day_26 = BooleanField(label='day_26', required=False) day_27 = BooleanField(label='day_27', required=False) class Meta(TripFormBase.Meta): fields = TripFormBase.Meta.fields + [ 'text', 'day_11', 'day_12', 'day_13', 'day_14', 'day_15', 'day_16', 'day_17', 'day_21', 'day_22', 'day_23', 'day_24', 'day_25', 'day_26', 'day_27', ] widgets = { 'week': NumberInput(attrs={"class": "week-style"}), 'year': NumberInput(attrs={"class": "year-style"}), 'price': NumberInput(attrs={ "step": "0.01", "class": "price-style" }), 'text': Textarea(attrs={ 'rows': 3, 'cols': 10, 'placeholder': _('add note').capitalize() }) } def __init__(self, user, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['driver'].queryset = Person.objects.filter( user=user).order_by('name') self.fields['passenger'].queryset = Person.objects.filter( user=user).order_by('name') def clean_year(self): data = self.cleaned_data['year'] if (data < 2000) or (data > 2100): self.add_error('year', 'Ожидался год в диапазоне от 2000 до 2100') return data def clean_week(self): data = self.cleaned_data['week'] if (data < 1) or (data > 53): self.add_error('week', 'Ожидался номер недели в диапазоне от 1 до 53') return data def clean_price(self): data = self.cleaned_data['price'] if (data == 0): self.add_error('price', 'Цена должна быть указана') return data def clean(self): super().clean() if (self.cleaned_data['driver'] == self.cleaned_data['passenger']): self.add_error('passenger', 'Укажите другого пассажира') raise ValidationError('Водитель и Пассажир должны отличаться') self.cleaned_data['days'] = 0 for i in range(2): for j in range(7): if self.cleaned_data['day_' + str(i + 1) + str(j + 1)]: self.cleaned_data['days'] = self.cleaned_data['days'] + ( 1 << (i + j * 2)) if (self.cleaned_data['oper'] == 0) and (self.cleaned_data['days'] == 0): raise ValidationError('Не отмечены дни недели')
class UserCaseListSelectForm(ModelForm): selected = BooleanField(initial=False, required=False) class Meta: model = UserCaseList fields = '__all__'
class ChmodForm(forms.Form): op = "chmod" path = PathField(label=_("Path to change permissions")) # By default, BooleanField only validates when # it's checked. user_read = BooleanField(required=False) user_write = BooleanField(required=False) user_execute = BooleanField(required=False) group_read = BooleanField(required=False) group_write = BooleanField(required=False) group_execute = BooleanField(required=False) other_read = BooleanField(required=False) other_write = BooleanField(required=False) other_execute = BooleanField(required=False) sticky = BooleanField(required=False) recursive = BooleanField(required=False) names = ("user_read", "user_write", "user_execute", "group_read", "group_write", "group_execute", "other_read", "other_write", "other_execute", "sticky") def __init__(self, initial, *args, **kwargs): logging.info(dir(self)) logging.info(dir(type(self))) # Convert from string representation. mode = initial.get("mode") if mode is not None: mode = int(mode, 8) bools = rwx.expand_mode(mode) for name, b in zip(self.names, bools): initial[name] = b logging.debug(initial) kwargs['initial'] = initial forms.Form.__init__(self, *args, **kwargs) def full_clean(self): forms.Form.full_clean(self) if hasattr(self, "cleaned_data"): self.cleaned_data["mode"] = rwx.compress_mode( map(lambda name: self.cleaned_data[name], self.names))
class TempUserForm(Form): id = IntegerField() selected = BooleanField(required=False)
class CompleteProductBooleanForm(Form): checkbox = BooleanField(label = '', required = False)
class CasesSelectForm(ModelForm): selected = BooleanField(initial=False, required=False) class Meta: model = Case fields = '__all__'