def deserialize(self, cstruct=null): """Deserialize the :term:`cstruct` into an :term:`appstruct`.""" if self.readonly and cstruct != null: raise Invalid(self, 'This field is ``readonly``.') return super().deserialize(cstruct)
def repo_url_type_schema_validator(node, value): valid_prefixes = ['git://', 'http://', 'https://', 'ssh://'] if not any([value.startswith(prefix) for prefix in valid_prefixes]): raise Invalid(node, '%r is not a valid repo_url' % (value, ))
def existing_user(node, param): user = principals.find_user(param) if user is not None and user.active: return True else: raise Invalid(node, _('Email does not exist'))
def URI(node, value): res = urllib.parse.urlparse(value) if not res.path.startswith("/"): raise Invalid(node, '{} has wrong path {}'.format(value, res.path))
def PythonIdentifier(node, value): if identifier_regex.match(value) is None: raise Invalid(node, "%r is not a valid Python identifier".format(value))
def validator(self, node, cstruct): if not valid_wifi_pattern(cstruct): raise Invalid(node, 'Invalid wifi key')
def _check(node, value): if value != current_login_name: if confirmed.get_by_login(value, None) is not None: raise Invalid('Login name not available')
def required_validator(node, value): if not value: raise Invalid(node, 'Required')
def url_validator(node, value: str): if not validators.url(value): raise Invalid(node, f"URL {value} is not valid")
def validate_user_email_is_unique(node, value): if locator.get_user_by_email(value): raise Invalid(node, 'The user login email is not unique', value=value)
def positive(node, value): if value <= 0: raise Invalid(node, 'Value must be greater than zero.')
def validate_user_name_is_unique(node, value): if locator.get_user_by_login(value): raise Invalid(node, 'The user login name is not unique', value=value)
def statute_validator(node, value): if not value: raise Invalid( node, _(u'You must confirm to have access ' u'to the C3S SCE statute'))
def date_today_or_earlier(node, value): today = date.today() if today < value: raise Invalid(node, '%s is a date in the future' % value)
def deserialize(self, node, cstruct): if isinstance(cstruct, basestring) or isinstance(cstruct, int): return cstruct raise Invalid(node, "Ticket should be String or Int")
def image_validator(node, value: str): width, height = get_size(value.get("fp").read()) if width < 600 or height < 600: raise Invalid(node, f"Image must be at least 600x600px")
def __call__(self, node, value): try: self.db.get_model_definition(value) except ModelNotFound: msg = u"Model '%s' not found." % value raise Invalid(node, msg)
def esgfsearch_validator(node, value): search = json.loads(value) if search.get('hit-count', 0) > 100: raise Invalid( node, 'More than 100 datasets selected: %r.' % search['hit-count'])
def _check(node, value): if not pwd_mgr.checkPassword(old_password, value): raise Invalid('Old password incorrect')
def validator(node, value): if value not in enabled_locales: raise Invalid(node, _('Please select a language.'))
def check_assign_badge_permisson(node, value): if not request.has_permission('assign_badge', value): badge_path = resource_path(value) raise Invalid( node, 'Your are missing the `assign_badge` ' ' permission for: ' + badge_path)
def _raise_if_no_password_reset(node: SchemaNode, value: IPasswordReset): if not IPasswordReset.providedBy(value): raise Invalid(node, 'This is not a valid password reset.')
def Path(node, value): URI(node, value) if not value.startswith("/"): raise Invalid(node, "not a path: %r".format(value))
def _raise_if_outdated(node: SchemaNode, value: IPasswordReset, creation_date: datetime): if (now() - creation_date).days >= 7: value.__parent__ = None # commit_suicide msg = 'This password reset is older than 7 days.' raise Invalid(node, msg)
def get_schema(self, schema_type): raise Invalid(self[self.type_key], "Invalid value for key %s" % self.type_key)
class RecaptchaWidget(MappingWidget): """ A Deform widget for Google reCaptcha. In `c2cgeoform` this widget can be used by setting the `show_captcha` flag when calling `register_schema()`. Example usage: .. code-block:: python register_schema( 'comment', model.Comment, show_confirmation=False, show_captcha=True, recaptcha_public_key=settings.get('recaptcha_public_key'), recaptcha_private_key=settings.get('recaptcha_private_key')) **Attributes/arguments** public_key (required) The Google reCaptcha site key. private_key (required) The Google reCaptcha secret key. """ template = 'recaptcha' readonly_template = 'recaptcha' url = "https://www.google.com/recaptcha/api/siteverify" def populate(self, session, request): self.request = request def serialize(self, field, cstruct, **kw): kw.update({ 'public_key': self.public_key, 'locale_name': self.request.locale_name }) return MappingWidget.serialize(self, field, cstruct, **kw) def deserialize(self, field, pstruct): if pstruct is null: return null # get the verification token that is inserted into a hidden input # field created by the reCaptcha script. the value is available in # `pstruct` because we are inheriting from `MappingWidget`. response = pstruct.get('g-recaptcha-response') or '' if not response: raise Invalid(field.schema, _('Please verify that you are a human!'), pstruct) remoteip = self.request.remote_addr data = urllib.urlencode({ 'secret': self.private_key, 'response': response, 'remoteip': remoteip }) try: resp = urllib2.urlopen(self.url, data) except urllib2.URLError, e: log.error('reCaptcha connection problem: %s', e.reason) raise Invalid(field.schema, _("Connection problem"), pstruct) error_msg = _("Verification has failed") if not resp.code == 200: log.error('reCaptcha validation error: %s', resp.code) raise Invalid(field.schema, error_msg, pstruct) content = resp.read() data = json.loads(content) if not data['success']: error_reason = '' if 'error-codes' in data: error_reason = ','.join(data['error-codes']) log.error('reCaptcha validation error: %s', error_reason) raise Invalid(field.schema, error_msg, pstruct) return pstruct
def serialize(self, val): """Serialize.""" from colander import Invalid if self.exc: raise Invalid(self, self.exc) return val
def validate_current_password(node, param): """ Validator to make sure the current password was given and matches """ request = utils.get_request() if (request.user is not None and request.user.password is not None and not request.user.validate_password(param)): raise Invalid(node, _(u'Password does not match'))
def validate_bar(node, value): if value != 'open': raise Invalid(node, "The bar is not open.")
def validate_actions_names(node, value): for action in value[1:]: if action not in [security.Allow, security.Deny, None]: msg = 'Invalid action: {0}'.format(action) raise Invalid(node, msg, value=action)