def _node(schema, name, *children, **kw): if 'validator' in kw: kw['validator'] = Function(kw['validator']) return SchemaNode(schema, *children, name=name, **kw)
class Dimension(MappingSchema): label = SchemaNode(String(), missing=drop) description = SchemaNode(String(), missing='') label_attribute = SchemaNode(String(), missing=None) key_attribute = SchemaNode(String(), missing=None) facet = SchemaNode(Boolean(), missing=False) # cardinality = SchemaNode(Int(), missing=None, default=None) attributes = Attributes(validator=Function(require_one_child))
def deferred_verif_email_unico(node, kw): """ Verifica se o email inserido para cadastro já existe no banco """ request = kw.get('request') emails = request.db["usrTree"].keys() return All( Email('E-mail inválido'), Function(lambda x: not (x in emails), u"Email já cadastrado") )
def create_password_validator(node, kw): request = kw.get('request') ctx_validator = request.registry.password_validator def inner_password_validator(value): error_msg = ctx_validator(value) if not error_msg: return True else: return error_msg return Function(inner_password_validator)
class SingleLine(SchemaNode): r"""UTF-8 encoded String without line breaks. Disallowed characters are linebreaks like: \n, \r. Example value: This is a something. """ schema_type = StringType default = '' missing = drop validator = Function(string_has_no_newlines_validator, msg='New line characters are not allowed.')
class FormCadastrar(CSRFSchema): """ Formulário para cadastro de novo usuário falta acesso para termos e condições de uso """ nome = SchemaNode( String(), validator=All( Length(max=32), #Function(verif_nome_unico, u"Nome já cadastrado"), Regex("^(\w)*$", "Usar apenas letras, números ou _"), ), description='Digite seu nome de usuário' ) email = SchemaNode( String(), validator=deferred_verif_email_unico, description='Digite seu e-mail', widget=widget.CheckedInputWidget( subject='Email', confirm_subject='Confirmar e-mail', size=40) ) senha = SchemaNode( String(), validator=Length(min=5, max=32), widget=widget.CheckedPasswordWidget(size=20), description='Digite sua senha (no mínimo 5 caracteres) e a confirme' ) confirmar = SchemaNode( Boolean(), #description='Aceitar termos e condições', label='Aceitar termos e condições', widget=widget.CheckboxWidget(), title='Confirmar', validator=Function(lambda x: x, u'É necessário aceitar as condições'), )
def _dataset_name(name): """ These are names that have a special meaning in URLs and cannot be used for dataset names. """ if name is not None and name.lower() in RESERVED_TERMS: return "'%s' is a reserved word and cannot be used here" % name if not re.match(r"^\w[\w\_\-]+$", name): return ("Name must include only " "letters, numbers, dashes and underscores") if '__' in name: return "Double underscores are not allowed in dataset names." return True dataset_name = All(Length(min=2, max=30), Function(_dataset_name)) def _field_name(name): """ These are names that have a special meaning in URLs and cannot be used for dataset names. """ if not re.match(r"^\w[\w\_]+$", name): return ("Name must include only letters, numbers and underscores") if '__' in name: return "Double underscores are not allowed in field names." return True field_name = All(Length(min=2, max=60), Function(_field_name))
def make_profile_form(request, edit=False): target_user = getattr(request, 'target_user', None) user = target_user or request.user email_validator = dict(should_exist=False, msg="Email address already in use.", db_session=request.db_session) username_validator = dict(should_exist=False, db_session=request.db_session) if edit: email_validator['for_edit'] = True # We need to attach the current value of the user's email # so we know if they're trying to change it during validation schema = EditProfileSchema() for fld in schema: if fld.name == 'email': fld.current_value = user.email if request.user.is_superuser: is_superuser = SchemaNode( Bool(), title='Is this user an admin?', ) user_disabled = SchemaNode( Bool(), title='User disabled?', ) schema['user_disabled'] = user_disabled schema['is_superuser'] = is_superuser # Can't compare SQLA objects here, so use the usernames. # Admin users editing their own profile still need a password. username = request.user.username target_username = target_user.username if username == target_username or not request.user.is_superuser: password = SchemaNode( String(), required=False, missing=null, description=u"Password only required if changing email.", widget=PasswordWidget(), title='Password', name='password', ) schema['password'] = password else: schema = Profile() if not request.user: # Only include these if the user isn't logged in agree_to_policy = SchemaNode( Bool(), title='I agree to the site policy.', validator=Function( usage_policy_validator, message='Agreement with the site policy is required.'), ) captcha = SchemaNode( String(), widget=deferred_recaptcha_widget, ) schema['agree_to_policy'] = agree_to_policy schema['captcha'] = captcha elif request.user.is_superuser: is_superuser = SchemaNode( Bool(), title='Is this user an admin?', ) schema['is_superuser'] = is_superuser form = Form( buttons=('submit', ), resource_registry=password_registry, renderer=renderer, schema=schema.bind(request=request, email_validator=email_validator, username_validator=username_validator), bootstrap_form_style='form-vertical', ) return form
class Model(Schema): dimensions = Dimensions() measures = Measures(validator=Function(require_one_child)) def schema_type(self, **kw): return Mapping(unknown='preserve')
class Dimensions(MappingSchema): _named = Dimension(validator=Function(attributes_exist))
class Dimension(MappingSchema): label = SchemaNode(String(), missing=drop) description = SchemaNode(String(), missing='') label_attribute = SchemaNode(String(), missing=None) key_attribute = SchemaNode(String(), missing=None) attributes = Attributes(validator=Function(require_one_child))