def test_int_minmax_optional(): messages = Int().message iv = Int(min=5, max=10, if_empty=None) assert iv.to_python("") == None assert iv.to_python(None) == None assert iv.to_python('7') == 7 assert validate(iv, "1") == messages('tooLow', None, min=5) assert validate(iv, "15") == messages('tooHigh', None, max=10)
class RegionConfiguration(Configuration, metaclass=RegionConfigurationMeta): """Local configuration for the MAAS region.""" maas_url = ConfigurationOption( "maas_url", "The HTTP URL for the MAAS region.", ExtendedURL(require_tld=False, if_missing="http://localhost:5240/MAAS")) # Database options. database_host = ConfigurationOption( "database_host", "The address of the PostgreSQL database.", UnicodeString(if_missing="localhost", accept_python=False)) database_port = ConfigurationOption( "database_port", "The port of the PostgreSQL database.", Int(if_missing=5432, accept_python=False, min=1, max=65535)) database_name = ConfigurationOption( "database_name", "The name of the PostgreSQL database.", UnicodeString(if_missing="maasdb", accept_python=False)) database_user = ConfigurationOption( "database_user", "The user to connect to PostgreSQL as.", UnicodeString(if_missing="maas", accept_python=False)) database_pass = ConfigurationOption( "database_pass", "The password for the PostgreSQL user.", UnicodeString(if_missing="", accept_python=False)) database_conn_max_age = ConfigurationOption( "database_conn_max_age", "The lifetime of a database connection, in seconds.", Int(if_missing=(5 * 60), accept_python=False, min=0)) # Worker options. num_workers = ConfigurationOption( "num_workers", "The number of regiond worker process to run.", Int(if_missing=4, accept_python=False, min=1)) # Debug options. debug = ConfigurationOption( "debug", "Enable debug mode for detailed error and log reporting.", StringBool(if_missing=False)) debug_queries = ConfigurationOption( "debug_queries", "Enable query debugging. Reports number of queries and time for all " "actions performed. Requires debug to also be True. mode for detailed " "error and log reporting.", StringBool(if_missing=False)) debug_http = ConfigurationOption( "debug_http", "Enable HTTP debugging. Logs all HTTP requests and HTTP responses.", StringBool(if_missing=False))
class ConfigTFTP(Schema): """Configuration validator for the TFTP service.""" if_key_missing = None root = String(if_missing="/var/lib/maas/tftp") port = Int(min=1, max=65535, if_missing=69) generator = String(if_missing=b"http://localhost/MAAS/api/1.0/pxeconfig/")
class LockssomaticSpaceSchema(TypeOfSpaceSchema): collection_iri = UnicodeString(max=256) content_provider_id = UnicodeString(max=32) checksum_type = UnicodeString(max=64) keep_local = Bool() au_size = Int() sd_iri = URL(max=256) external_domain = URL()
class SettingsSchema(Schema): allow_extra_fields = True filter_extra_fields = False seriesname = String(not_empty=True) largestcarnumber = Int(min=99) minevents = Int(if_empty=0) useevents = Int(not_empty=True) sponsorlink = String() pospointlist = String() champsorting = String() superuniquenumbers = Bool() indexafterpenalties = Bool() locked = Bool() usepospoints = Bool() sponsorimage = Validator() seriesimage = Validator() cardimage = Validator()
class ConfigFrontend(Schema): """Configuration validator for the front-end service.""" if_key_missing = None port = Int(min=1, max=65535, if_missing=8001) prefix = String(if_missing=None) interface = String(if_missing=None)
class UpdateHomePageForm(Schema): """NewPageForm is a Schema for validating the data entered at the Edit Home Page page.""" allow_extra_fields = True filter_extra_fields = True name = UnicodeString() heading = UnicodeString() content = UnicodeString() ID = Int()
class AddProyecto(AddRecordForm): """ Define el formato del formulario para crear un nuevo proyecto""" __model__ = Proyecto __omit_fields__ = ['id', 'fases', 'fichas', 'estado', 'fecha_inicio', \ 'fecha_fin'] nombre = All(NotEmpty(), ValidarExpresion(r'^[A-Za-z][A-Za-z0-9 ]*$'), \ Unico()) nro_fases = All(NotEmpty(), Int(min=0)) __dropdown_field_names__ = {'lider': 'nombre_usuario'}
class LocationSchema(Schema): description = UnicodeString(max=256) purpose = OneOf(_flatten(models.Location.PURPOSE_CHOICES)) relative_path = UnicodeString() quota = Int(min=0) enabled = Bool() space = ResourceURI(model_cls=models.Space) pipeline = ForEach(ResourceURI(model_cls=models.Pipeline)) replicators = ForEach(ResourceURI(model_cls=models.Location))
class ConfigBroker(Schema): """Configuration validator for message broker options.""" if_key_missing = None host = String(if_missing=b"localhost") port = Int(min=1, max=65535, if_missing=5672) username = String(if_missing=b"guest") password = String(if_missing=b"guest") vhost = String(if_missing="/")
class SlaveDetailsSchema(ReportSchema): """Slave Details Report Schema.""" int_size = IntervalValidator() last_int_size = IntervalValidator() slave_id = Int(min=0) type = OneOf(list=['busy', 'all'], if_empty='all', if_missing='all') chained_validators = ReportSchema.chained_validators + [ IntSizeInit(divisions=24), IntSizeInit(divisions=24, int_size_param='last_int_size') ]
class UserPasswordForm(UserForm): """ Form validation schema for user password change.""" user_id = Int() # used in forms hidden field username = None first_name = None last_name = None email = None group_id = None chained_validators = [ FieldsMatch('password', 'password_confirm'), ]
class EventSchema(Schema): allow_extra_fields = True filter_extra_fields = True name = String(not_empty=True) date = SDate() location = String() sponsor = String() host = String() designer = String() chair = String() practice = Bool() ispro = Bool() courses = Int(min=1, not_empty=True) runs = Int(min=1, not_empty=True) countedruns = Int(if_empty=0) conepen = Number(if_empty=2.0) gatepen = Number(if_empty=10.0) segments = String() regopened = SDateTime() regclosed = SDateTime() perlimit = Int(min=1) doublespecial = Bool() totlimit = Int(min=0) paypal = String() cost = Int() notes = String()
class PackageSchema(Schema): current_location = ResourceURI(model_cls=models.Location) current_path = UnicodeString() description = UnicodeString(max=256) encryption_key_fingerprint = UnicodeString(max=512) misc_attributes = UnicodeString() origin_pipeline = ResourceURI(model_cls=models.Pipeline) package_type = OneOf(_flatten(models.Package.PACKAGE_TYPE_CHOICES)) pointer_file_location = ResourceURI(model_cls=models.Location) pointer_file_path = UnicodeString() related_packages = ForEach(ResourceURI(model_cls=models.Package)) replicated_package = ResourceURI(model_cls=models.Package) size = Int(min=0) status = OneOf(_flatten(models.Package.STATUS_CHOICES))
def add_auth(app, app_conf, prefix='auth.'): """ Add authentication and authorization middleware to the ``app``. :param app_conf: The application's local configuration. Normally specified in the [app:<name>] section of the Paste ini file (where <name> defaults to main). :param prefix: Prefix for the config related to the auth. :type prefix: :class:`str` """ # Cookie form plugin form_plugin = make_redirecting_plugin( login_form_path='/login', login_handler_path='/login_handler', logout_handler_path='/logout', rememberer_name='cookie', ) if prefix+'cookie_secret' not in app_conf: raise Exception("Missing config option: %s" % prefix+'cookie_secret') cookie_secret = app_conf.get(prefix+'cookie_secret') cookie_timeout = Int.to_python(app_conf.get(prefix+'cookie_timeout')) cookie_reissue_time = Int.to_python(app_conf.get(prefix+'cookie_reissue_time')) # Perform type conversion, sice timeout and reisue_time must be int or None if cookie_timeout is not None: cookie_timeout = int(cookie_timeout) if cookie_reissue_time is not None: cookie_reissue_time = int(cookie_reissue_time) return setup_sql_auth(app, AuthUser, AuthGroup, AuthPermission, Session, form_plugin=form_plugin, cookie_secret=cookie_secret, cookie_timeout=cookie_timeout, cookie_reissue_time=cookie_reissue_time)
class UserForm(Schema): """ Form validation schema for users.""" filter_extra_fields = True allow_extra_fields = True username = UsernamePlainText(min=5, max=16, strip=True) first_name = FirstNameString(not_empty=True, strip=True) last_name = LastNameString(not_empty=True, strip=True) email = Email() password = SecurePassword(min=6, max=80, strip=True) password_confirm = String(strip=True) group_id = Int(not_empty=True) chained_validators = [ FieldsMatch('password', 'password_confirm'), UniqueAuthUsername(), UniqueAuthEmail(), ]
class RegionConfiguration(Configuration, metaclass=RegionConfigurationMeta): """Local configuration for the MAAS region.""" maas_url = ConfigurationOption( "maas_url", "The HTTP URL for the MAAS region.", ExtendedURL(require_tld=False, if_missing="http://localhost:5240/MAAS")) # Database options. database_host = ConfigurationOption( "database_host", "The address of the PostgreSQL database.", UnicodeString(if_missing="localhost", accept_python=False)) database_port = ConfigurationOption( "database_port", "The port of the PostgreSQL database.", Int(if_missing=5432, accept_python=False, min=1, max=65535)) database_name = ConfigurationOption( "database_name", "The name of the PostgreSQL database.", UnicodeString(if_missing="maasdb", accept_python=False)) database_user = ConfigurationOption( "database_user", "The user to connect to PostgreSQL as.", UnicodeString(if_missing="maas", accept_python=False)) database_pass = ConfigurationOption( "database_pass", "The password for the PostgreSQL user.", UnicodeString(if_missing="", accept_python=False))
def init(self): self.add_processor('towho', UnicodeString()) self.add_processor('num', Int())
def test_int_min(): messages = Int().message iv = Int(min=5) assert iv.to_python("5") == 5 assert validate(iv, "1") == messages('tooLow', None, min=5)
def init(self): self.add_processor('towho', UnicodeString()) self.add_processor('num', Int(), show_msg=True) self.add_processor('num2', Int(), custom_msg='num: must be an integer') self.strict_args = True
def test_int_max(): messages = Int().message iv = Int(max=10) assert iv.to_python("10") == 10 assert validate(iv, "15") == messages('tooHigh', None, max=10)
def init(self): self.add_processor('nums', Int(), show_msg=True, takes_list=True)
class UserEditForm(UserForm): """ Form validation schema for user edit.""" user_id = Int() # used in forms hidden field password = None password_confirm = None
def test_validator_fe_instance(self): form = Form('f') form.add_text('units', 'Units') form.add_validator(Int()) assert isinstance(form._validators[0][0], Int)
class AddLuckyNumberForm(Schema): date = DateConverter(month_style='dd/mm/yyyy') number = Int()
def setUp(self): self.validator = AllAndNotOneValidator( validators=[Int(min=3), Int(max=5)], number=4)
class SpaceUpdateSchema(Schema): size = Int(min=0) path = UnicodeString(max=256) staging_path = UnicodeString(max=256)
def test_arg_validation(): class TestView(View): def init(self): self.add_processor('a', int) self.add_processor('b', int) self.add_processor('c', int) self.add_processor('d', Int) self.add_processor('f[]', int, pass_as='f') self.add_processor('g[]', pass_as='g') # this makes sure that pass_as is handled corretly even if the # value isn't sent self.add_processor('h[]', pass_as='h') self.add_processor('z', int) try: # try a bad processor type self.add_processor('e', 5) assert False except TypeError as e: if 'processor must be a Formencode validator or a callable' != str(e): raise # pragma: no cover def default(self, a, c, d, f, g, b=5, z=None): eq_(a, 1) eq_(c, 2) eq_(d, 3) # if an argument fails validation it is completely ignored, as if # the client did not send it. Therefore, we can set default # values and they will apply if the argument is not sent or if the # value that is sent fails validation eq_(b, 5) # the argument wasn't sent at all eq_(z, None) # these arguments have a different name in the get string eq_(f, 6) eq_(g, 'hi') TestView({'c': u'2'}).process() # try multiple validators for the same item class TestView(View): def init(self): self.add_processor('e', (String, Email)) def default(self, e): eq_(e, '*****@*****.**') TestView({}).process() class TestView(View): def init(self): self.add_processor('e', (Number, Email)) def default(self, e=None): eq_(e, None) TestView({}).process() # the next test makes sure we don't alter FormEncode validator classes # Int should accept a None value because its empty and by default Int should # allow empty values. However, b/c of a bug in processor handling, the # required value could alter the Int class. Int.to_python(None) class TestView(View): def init(self): self.add_processor('a', Int, required=True) def default(self, a): eq_(a, 1) TestView({}).process() # As long as this doesn't throw a ValueError b/c None is empty, the Int # class was not altered. Int.to_python(None)
class RegionConfiguration(Configuration, metaclass=RegionConfigurationMeta): """Local configuration for the MAAS region.""" maas_url = ConfigurationOption( "maas_url", "The HTTP URL for the MAAS region.", ExtendedURL(require_tld=False, if_missing="http://localhost:5240/MAAS"), ) # Database options. database_host = ConfigurationOption( "database_host", "The address of the PostgreSQL database.", UnicodeString(if_missing="localhost", accept_python=False), ) database_port = ConfigurationOption( "database_port", "The port of the PostgreSQL database.", Int(if_missing=5432, accept_python=False, min=1, max=65535), ) database_name = ConfigurationOption( "database_name", "The name of the PostgreSQL database.", UnicodeString(if_missing="maasdb", accept_python=False), ) database_user = ConfigurationOption( "database_user", "The user to connect to PostgreSQL as.", UnicodeString(if_missing="maas", accept_python=False), ) database_pass = ConfigurationOption( "database_pass", "The password for the PostgreSQL user.", UnicodeString(if_missing="", accept_python=False), ) database_conn_max_age = ConfigurationOption( "database_conn_max_age", "The lifetime of a database connection, in seconds.", Int(if_missing=(5 * 60), accept_python=False, min=0), ) database_keepalive = ConfigurationOption( "database_keepalive", "Whether keepalive for database connections is enabled.", OneWayStringBool(if_missing=True), ) database_keepalive_idle = ConfigurationOption( "database_keepalive_idle", "Time (in seconds) after which keepalives will be started.", Int(if_missing=15), ) database_keepalive_interval = ConfigurationOption( "database_keepalive_interval", "Interval (in seconds) between keepaliveds.", Int(if_missing=15), ) database_keepalive_count = ConfigurationOption( "database_keepalive_count", "Number of keeaplives that can be lost before connection is reset.", Int(if_missing=2), ) # Worker options. num_workers = ConfigurationOption( "num_workers", "The number of regiond worker process to run.", Int(if_missing=4, accept_python=False, min=1), ) # Debug options. debug = ConfigurationOption( "debug", "Enable debug mode for detailed error and log reporting.", OneWayStringBool(if_missing=False), ) debug_queries = ConfigurationOption( "debug_queries", "Enable query debugging. Reports number of queries and time for all " "actions performed. Requires debug to also be True. mode for detailed " "error and log reporting.", OneWayStringBool(if_missing=False), ) debug_http = ConfigurationOption( "debug_http", "Enable HTTP debugging. Logs all HTTP requests and HTTP responses.", OneWayStringBool(if_missing=False), )
def test_arg_validation(): class TestView(View): def init(self): self.add_processor('a', int) self.add_processor('b', int) self.add_processor('c', int) self.add_processor('d', Int) self.add_processor('f[]', int, pass_as='f') self.add_processor('g[]', pass_as='g') # this makes sure that pass_as is handled corretly even if the # value isn't sent self.add_processor('h[]', pass_as='h') self.add_processor('z', int) try: # try a bad processor type self.add_processor('e', 5) assert False except TypeError as e: if 'processor must be a Formencode validator or a callable' != str( e): raise # pragma: no cover def default(self, a, c, d, f, g, b=5, z=None): eq_(a, 1) eq_(c, 2) eq_(d, 3) # if an argument fails validation it is completely ignored, as if # the client did not send it. Therefore, we can set default # values and they will apply if the argument is not sent or if the # value that is sent fails validation eq_(b, 5) # the argument wasn't sent at all eq_(z, None) # these arguments have a different name in the get string eq_(f, 6) eq_(g, 'hi') TestView({'c': u'2'}).process() # try multiple validators for the same item class TestView(View): def init(self): self.add_processor('e', (String, Email)) def default(self, e): eq_(e, '*****@*****.**') TestView({}).process() class TestView(View): def init(self): self.add_processor('e', (Number, Email)) def default(self, e=None): eq_(e, None) TestView({}).process() # the next test makes sure we don't alter FormEncode validator classes # Int should accept a None value because its empty and by default Int should # allow empty values. However, b/c of a bug in processor handling, the # required value could alter the Int class. Int.to_python(None) class TestView(View): def init(self): self.add_processor('a', Int, required=True) def default(self, a): eq_(a, 1) TestView({}).process() # As long as this doesn't throw a ValueError b/c None is empty, the Int # class was not altered. Int.to_python(None)
def setUp(self): self.validator = CustomCompoundValidator( validators=[Int(min=3), Int(max=5)])
class FTPStorageForm(StorageForm): event = events.Admin.Storage.FTPStorageForm fields = StorageForm.fields + [ ListFieldSet( 'ftp', suppress_label=True, legend=N_('FTP Server Details:'), children=[ TextField('server', label_text=N_('Server Hostname')), TextField('user', label_text=N_('Username')), TextField('password', label_text=N_('Password')), TextField( 'upload_dir', label_text=N_('Subdirectory on server to upload to')), TextField( 'upload_integrity_retries', label_text=N_( 'How many times should MediaDrop try to verify the FTP upload before declaring it a failure?' ), validator=Int()), TextField( 'http_download_uri', label_text=N_('HTTP URL to access remotely stored files')), TextField( 'rtmp_server_uri', label_text=N_( 'RTMP Server URL to stream remotely stored files (Optional)' )), ]), ] + StorageForm.buttons def display(self, value, engine, **kwargs): """Display the form with default values from the given StorageEngine. If the value dict is not fully populated, populate any missing entries with the values from the given StorageEngine's :attr:`_data <mediadrop.lib.storage.StorageEngine._data>` dict. :param value: A (sparse) dict of values to populate the form with. :type value: dict :param engine: An instance of the storage engine implementation. :type engine: :class:`mediadrop.lib.storage.StorageEngine` subclass """ data = engine._data ftp = value.setdefault('ftp', {}) ftp.setdefault('server', data.get(FTP_SERVER, None)) ftp.setdefault('user', data.get(FTP_USERNAME, None)) ftp.setdefault('password', data.get(FTP_PASSWORD, None)) ftp.setdefault('upload_dir', data.get(FTP_UPLOAD_DIR, None)) ftp.setdefault('upload_integrity_retries', data.get(FTP_MAX_INTEGRITY_RETRIES, None)) ftp.setdefault('http_download_uri', data.get(HTTP_DOWNLOAD_URI, None)) ftp.setdefault('rtmp_server_uri', data.get(RTMP_SERVER_URI, None)) return StorageForm.display(self, value, engine, **kwargs) def save_engine_params(self, engine, **kwargs): """Map validated field values to engine data. Since form widgets may be nested or named differently than the keys in the :attr:`mediadrop.lib.storage.StorageEngine._data` dict, it is necessary to manually map field values to the data dictionary. :type engine: :class:`mediadrop.lib.storage.StorageEngine` subclass :param engine: An instance of the storage engine implementation. :param \*\*kwargs: Validated and filtered form values. :raises formencode.Invalid: If some post-validation error is detected in the user input. This will trigger the same error handling behaviour as with the @validate decorator. """ StorageForm.save_engine_params(self, engine, **kwargs) ftp = kwargs['ftp'] engine._data[FTP_SERVER] = ftp['server'] engine._data[FTP_USERNAME] = ftp['user'] engine._data[FTP_PASSWORD] = ftp['password'] engine._data[FTP_UPLOAD_DIR] = ftp['upload_dir'] engine._data[FTP_MAX_INTEGRITY_RETRIES] = ftp[ 'upload_integrity_retries'] engine._data[HTTP_DOWNLOAD_URI] = ftp['http_download_uri'] engine._data[RTMP_SERVER_URI] = ftp['rtmp_server_uri']
class DeclarativeAllValidator(All): """A CompoundValidator with subvalidators given as attributes.""" first_validator = Int(min=3) second_validator = Int(max=5)
class AddProyecto(AddRecordForm): """ Define el formato del formulario para crear un nuevo proyecto""" __model__ = Proyecto __omit_fields__ = ['id', 'fases', 'fichas', 'estado', 'fecha_inicio', \ 'fecha_fin'] nombre = All(NotEmpty(), ValidarExpresion(r'^[A-Za-z][A-Za-z0-9 ]*$'), \ Unico()) nro_fases = All(NotEmpty(), Int(min=0)) __dropdown_field_names__ = {'lider': 'nombre_usuario'} add_proyecto_form = AddProyecto(DBSession) form_validator = Schema(nro_fases = All(NroValido(), NotEmpty(), Int(min = \ 0)), ignore_key_missing = True) class CantidadFasesField(TextField): """ Clase correspondiente a un validador que se utiliza para deshabilitar la modificación del número de fases de un proyecto si el mismo ya ha iniciado. """ def update_params(self, d): """ Realiza el control citado anteriormente. """ id_proy = unicode(request.url.split("/")[-2]) pr = DBSession.query(Proyecto).get(id_proy) if pr.estado != u"Nuevo": d.disabled = True