class AddForm(BaseForm): name = fields.StringField('Name', validators=[ validators.Required(), validators.UniqueDocument( Account, case_sensitive=False) ]) api_rate_limit_per_second = fields.IntegerField( 'Rate limit (per second)', validators=[validators.Optional()]) api_allowed_ip_addresses = fields.StringListField( 'Allowed IP addresses', validators=[validators.Optional()]) def validate_api_rate_limit_per_second(form, field): if field.data and field.data < 1: raise validators.ValidationError( 'The rate limit must be set to 1 or more') def validate_api_allowed_ip_addresses(form, field): for ip_address in field.data: try: ipaddress.ip_address(ip_address) except ValueError: raise validators.ValidationError('Not a valid IP address: ' + ip_address)
class Filters(BaseForm): account = fields.HiddenField(coerce=utils.to_object_id) account_typeahead = fields.StringField( 'Account', render_kw={ 'autocomplete': 'typeahead', 'data-mh-typeahead': True, 'data-mh-typeahead--list': '/accounts/typeahead', 'data-mh-typeahead--fetch': 'ajax', 'data-mh-typeahead--input': 'setHidden', 'data-mh-typeahead--filter': 'contains', 'data-mh-typeahead--hidden-selector': '[name="filters-account"]', 'data-mh-typeahead--auto-first': True, 'data-mh-typeahead--must-match': True }) type = fields.SelectField('Type', choices=TypeList(), default='') storage = fields.SelectField('Storage', choices=[('', 'Any'), ('public', 'Public'), ('secure', 'Secure')], default='') def filter_account_typeahead(filter_form, form, field): pass def filter_storage(filter_form, form, field): if field.data == 'public': return Q.secure == False if field.data == 'secure': return Q.secure == True
class SettingsForm(BaseForm): access_key = fields.StringField('Access key', [validators.Required()]) secret_key = fields.StringField('Secret key', [validators.Required()]) bucket = fields.StringField('Bucket', [validators.Required()]) region = fields.StringField('Region', [validators.Required()], default='eu-west-1') def validate_access_key(form, field): access_key = field.data secret_key = form.secret_key.data bucket = form.bucket.data region = form.region.data if access_key and secret_key and bucket and region: client = botocore.session.get_session().create_client( 's3', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region) try: data = b'Hey, Hope you have a nice day! your friend Hangar51.' key = f'hangar51-settings-test-{uuid.uuid4().hex}' # Check we can write to the the bucket client.put_object(Bucket=bucket, Key=key, Body=data) # Check we can retrieve from the bucket client.get_object(Bucket=bucket, Key=key) # Check we can delete from the bucket client.delete_object(Bucket=bucket, Key=key) except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: raise validators.ValidationError(str(e))
class ListForm(BaseForm): page = fields.IntegerField('Page', default=1) q = fields.StringField('Search') sort_by = fields.SelectField('Sort by', choices=sort_by_choices([('created', 'Created')]), default='-created') filters = fields.FormField(Filters)
class ManyForm(PaginationForm): after = fields.HiddenField(coerce=uid_to_object_id) before = fields.HiddenField(coerce=uid_to_object_id) q = fields.StringField( 'Q', validators=[validators.Optional()] ) backend = fields.StringField( 'backend', validators=[validators.AnyOf('any', 'public', 'secure')], default='any' ) type = fields.StringField( 'type', validators=[validators.Optional()] )
class ListForm(BaseForm): page = fields.IntegerField('Page', default=1) q = fields.StringField('Search') sort_by = fields.SelectField( 'Sort by', choices=sort_by_choices([ ('name', 'Name') ]), default='name' )
class SettingsForm(BaseForm): width = fields.IntegerField('width', validators=[validators.NumberRange(min=0)]) height = fields.IntegerField('width', validators=[validators.NumberRange(min=0)]) resample = fields.StringField( 'resample', validators=[ validators.Optional(), validators.AnyOf(['NEAREST', 'BILINEAR', 'BICUBIC', 'LANCZOS']) ])
class SettingsForm(BaseForm): files_path = fields.StringField('Files path', [validators.Required()]) def validate_files_path(form, field): # Check the path given for the asset root exists if not field.data: return if field.data[0] != '/': raise validators.ValidationError('Path must be absolute') if not os.path.exists(field.data): raise validators.ValidationError('Path does not exist.')
class PutForm(BaseForm): name = fields.StringField( 'name', validators=[validators.Optional()] ) expire = fields.IntegerField( 'expire', validators=[ validators.Optional(), validators.NumberRange(min=1) ] ) secure = fields.BooleanField('secure')
class SettingsForm(BaseForm): image_format = fields.StringField( 'image_format', validators=[ validators.Required(), validators.AnyOf(IMAGE_FORMATS.keys()) ] ) quality = fields.IntegerField( 'quality', validators=[ validators.Optional(), validators.NumberRange(min=0, max=100) ], default=80 ) lossless = fields.BooleanField('lossless') progressive = fields.BooleanField('progressive') versioned = fields.BooleanField('versioned')