def form_init(cls): cls.configure_relationships("users") keys = ("get_requests", "post_requests", "delete_requests", "upper_menu") for key in keys: values = [(k, k) for k, v in app.rbac[key].items() if v == "access"] field_name = " ".join(key.split("_")).capitalize() setattr(cls, key, SelectMultipleField(field_name, choices=values)) menus, pages = [], [] for category, values in app.rbac["menu"].items(): if values["rbac"] == "admin": continue if values["rbac"] == "access": menus.append(category) for page, page_values in values["pages"].items(): if page_values["rbac"] == "admin": continue if page_values["rbac"] == "access": pages.append(page) subpages = page_values.get("subpages", {}) for subpage, subpage_values in subpages.items(): if subpage_values["rbac"] == "admin": continue if subpage_values["rbac"] == "access": pages.append(subpage) setattr(cls, "menu", SelectMultipleField("Menu", choices=choices(menus))) setattr(cls, "pages", SelectMultipleField("Pages", choices=choices(pages)))
class DatabaseDeletionForm(BaseForm): action = "eNMS.administration.databaseDeletion" form_type = HiddenField(default="database_deletion") deletion_choices = [(p, p) for p in app.database["import_export_models"]] deletion_types = SelectMultipleField("Instances to delete", choices=deletion_choices)
class NapalmGettersForm(NapalmForm): form_type = HiddenField(default="napalm_getters_service") getters = SelectMultipleField(choices=( ("get_arp_table", "ARP table"), ("get_interfaces_counters", "Interfaces counters"), ("get_facts", "Facts"), ("get_environment", "Environment"), ("get_config", "Configuration"), ("get_interfaces", "Interfaces"), ("get_interfaces_ip", "Interface IP"), ("get_lldp_neighbors", "LLDP neighbors"), ("get_lldp_neighbors_detail", "LLDP neighbors detail"), ("get_mac_address_table", "MAC address"), ("get_ntp_servers", "NTP servers"), ("get_ntp_stats", "NTP statistics"), ("get_optics", "Transceivers"), ("get_snmp_information", "SNMP"), ("get_users", "Users"), ("get_network_instances", "Network instances (VRF)"), ("get_ntp_peers", "NTP peers"), ("get_bgp_config", "BGP configuration"), ("get_bgp_neighbors", "BGP neighbors"), ("get_ipv6_neighbors_table", "IPv6"), ("is_alive", "Is alive"), )) groups = { "Main Parameters": { "commands": ["getters"], "default": "expanded" }, **NapalmForm.groups, }
class RbacForm(BaseForm): template = "object" form_type = HiddenField(default="rbac") id = HiddenField() name = StringField("Name", [InputRequired()]) email = StringField("Email") menu = SelectMultipleField("Menu", choices=choices(list(rbac["menu"]))) pages = SelectMultipleField("Pages", choices=choices(rbac["pages"])) upper_menu = SelectMultipleField("Upper Menu", choices=choices(rbac["upper_menu"])) get_requests = SelectMultipleField("GET requests", choices=choices( rbac["get_requests"])) post_requests = SelectMultipleField("POST requests", choices=choices( rbac["post_requests"]))
class ResultLogDeletionForm(BaseForm): action = "eNMS.administration.resultLogDeletion" form_type = HiddenField(default="result_log_deletion") deletion_types = SelectMultipleField( "Instances do delete", choices=[("run", "result"), ("changelog", "changelog")], ) date_time = StringField(type="date", label="Delete Records before")
class AccessForm(RbacForm): template = "access" form_type = HiddenField(default="access") description = StringField("Description") menu = SelectMultipleField("Menu", choices=choices(list(app.rbac["menu"]))) pages = SelectMultipleField("Pages", choices=choices(app.rbac["pages"])) upper_menu = SelectMultipleField("Upper Menu", choices=choices( app.rbac["upper_menu"])) get_requests = SelectMultipleField("GET requests", choices=choices( app.rbac["get_requests"])) post_requests = SelectMultipleField("POST requests", choices=choices( app.rbac["post_requests"])) relations = ["pools", "services"]
class DatabaseMigrationsForm(BaseForm): template = "database_migration" form_type = HiddenField(default="database_migration") empty_database_before_import = BooleanField("Empty Database before Import") skip_update_pools_after_import = BooleanField( "Skip the Pool update after Import", default="checked") export_choices = [(p, p) for p in db.import_classes] import_export_types = SelectMultipleField("Instances to migrate", choices=export_choices)
class NapalmGettersForm(NapalmForm): form_type = HiddenField(default="napalm_getters_service") getters = SelectMultipleField(choices=app.NAPALM_GETTERS) groups = { "Main Parameters": { "commands": ["getters"], "default": "expanded" }, **NapalmForm.groups, }
class DatabaseMigrationsForm(BaseForm): template = "database_migration" form_type = HiddenField(default="database_migration") empty_database_before_import = BooleanField("Empty Database before Import") skip_pool_update = BooleanField("Skip the Pool update after Import", default="checked") export_private_properties = BooleanField("Include private properties", default="checked") export_choices = [(p, p) for p in app.database["import_export_models"]] import_export_types = SelectMultipleField("Instances to migrate", choices=export_choices)
class NapalmBackupForm(NapalmForm): form_type = HiddenField(default="napalm_backup_service") property = SelectField( "Configuration Property to Update", choices=list(app.configuration_properties.items()), ) getters = SelectMultipleField(choices=app.NAPALM_GETTERS) replacements = FieldList(FormField(ReplacementForm), min_entries=3) groups = { "Target Property and Getters": { "commands": ["property", "getters"], "default": "expanded", }, "Search Response & Replace": { "commands": ["replacements"], "default": "expanded", }, **NapalmForm.groups, }
class NapalmBackupForm(NapalmForm): form_type = HiddenField(default="napalm_backup_service") configuration_getters = SelectMultipleField(choices=( ("get_arp_table", "ARP table"), ("get_interfaces_counters", "Interfaces counters"), ("get_facts", "Facts"), ("get_environment", "Environment"), ("get_config", "Configuration"), ("get_interfaces", "Interfaces"), ("get_interfaces_ip", "Interface IP"), ("get_lldp_neighbors", "LLDP neighbors"), ("get_lldp_neighbors_detail", "LLDP neighbors detail"), ("get_mac_address_table", "MAC address"), ("get_ntp_servers", "NTP servers"), ("get_ntp_stats", "NTP statistics"), ("get_optics", "Transceivers"), ("get_snmp_information", "SNMP"), ("get_users", "Users"), ("get_network_instances", "Network instances (VRF)"), ("get_ntp_peers", "NTP peers"), ("get_bgp_config", "BGP configuration"), ("get_bgp_neighbors", "BGP neighbors"), ("get_ipv6_neighbors_table", "IPv6"), ("is_alive", "Is alive"), )) operational_data_getters = SelectMultipleField(choices=( ("get_arp_table", "ARP table"), ("get_interfaces_counters", "Interfaces counters"), ("get_facts", "Facts"), ("get_environment", "Environment"), ("get_config", "Configuration"), ("get_interfaces", "Interfaces"), ("get_interfaces_ip", "Interface IP"), ("get_lldp_neighbors", "LLDP neighbors"), ("get_lldp_neighbors_detail", "LLDP neighbors detail"), ("get_mac_address_table", "MAC address"), ("get_ntp_servers", "NTP servers"), ("get_ntp_stats", "NTP statistics"), ("get_optics", "Transceivers"), ("get_snmp_information", "SNMP"), ("get_users", "Users"), ("get_network_instances", "Network instances (VRF)"), ("get_ntp_peers", "NTP peers"), ("get_bgp_config", "BGP configuration"), ("get_bgp_neighbors", "BGP neighbors"), ("get_ipv6_neighbors_table", "IPv6"), ("is_alive", "Is alive"), )) replacements = FieldList(FormField(ReplacementForm), min_entries=3) groups = { "Create Configuration File": { "commands": ["configuration_getters"], "default": "expanded", }, "Create Operational Data File": { "commands": ["operational_data_getters"], "default": "expanded", }, "Search Response & Replace": { "commands": ["replacements"], "default": "expanded", }, **NapalmForm.groups, }
class ExampleForm(ServiceForm): # Each service model must have an corresponding form. # The purpose of a form is twofold: # - Define how the service is displayed in the UI # - Check for each field that the user input is valid. # A service cannot be created/updated until all fields are validated. # The following line is mandatory: the default value must point # to the service. form_type = HiddenField(default="example_service") # string1 is defined as a "SelectField": it will be displayed as a # drop-down list in the UI. string1 = SelectField(choices=[("cisco", "Cisco"), ("juniper", "Juniper"), ("arista", "Arista")]) # String2 is a StringField, which is displayed as a standard textbox. # The "InputRequired" validator is used: this field is mandatory. string2 = StringField("String 2 (required)", [InputRequired()]) # The main address field uses two validators: # - The input length must be comprised between 7 and 25 characters # - The input syntax must match that of an email address. mail_address = StringField( "Mail address", [Length(min=7, max=25), Email()]) # This IP address validator will ensure the user input is a valid IPv4 address. # If it isn't, you can set the error message to be displayed in the GUI. ip_address = StringField( "IP address", [ IPAddress( ipv4=True, message="Please enter an IPv4 address for the IP address field", ) ], ) # MAC address validator mac_address = StringField("MAC address", [MacAddress()]) # The NumberRange validator will ensure the user input is an integer # between 3 and 8. number_in_range = IntegerField("Number in range", [NumberRange(min=3, max=8)]) # The Regexp field will ensure the user input matches the regular expression. regex = StringField("Regular expression", [Regexp(r".*")]) # URL validation, with or without TLD. url = StringField( "URL", [ URL( require_tld=True, message="An URL with TLD is required for the url field", ) ], ) # The NoneOf validator lets you define forbidden value for a field. exclusion_field = StringField( "Exclusion field", [ NoneOf( ("a", "b", "c"), message=("'a', 'b', and 'c' are not valid " "inputs for the exclusion field"), ) ], ) an_integer = IntegerField() a_float = FloatField() # If validator the user input is more complex, you can create a python function # to implement the validation mechanism. # Here, the custom_integer field will be validated by the "validate_custom_integer" # function below. # That function will check that the custom integer value is superior to the product # of "an_integer" and "a_float". # You must raise a "ValidationError" when the validation fails. custom_integer = IntegerField("Custom Integer") # A SelectMultipleField will be displayed as a drop-down list that allows # multiple selection. a_list = SelectMultipleField( choices=[("value1", "Value 1"), ("value2", "Value 2"), ("value3", "Value 3")]) a_dict = DictField() # A BooleanField is displayed as a check box. boolean1 = BooleanField() boolean2 = BooleanField("Boolean N°1") def validate_custom_integer(self, field): product = self.an_integer.data * self.a_float.data if field.data > product: raise ValidationError("Custom integer must be less than the " "product of 'An integer' and 'A float'")