Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate_data(self, data):
"""Validate identifier and scheme."""
id_ = data.get('identifier')
scheme = data.get('scheme')
if not id_:
raise ValidationError(
'Identifier is required.',
field_names=['identifier']
)
schemes = idutils.detect_identifier_schemes(id_)
if not schemes:
raise ValidationError(
'Not a valid persistent identifier.',
field_names=['identifier']
)
if scheme not in schemes:
raise ValidationError(
'Not a valid {0} identifier.'.format(scheme),
field_names=['identifier']
)
def pid_url(identifier, scheme=None, url_scheme='https'):
"""Convert persistent identifier into a link."""
if scheme is None:
try:
scheme = idutils.detect_identifier_schemes(identifier)[0]
except IndexError:
scheme = None
try:
if scheme and identifier:
return idutils.to_url(identifier, scheme, url_scheme=url_scheme)
except Exception:
current_app.logger.warning('URL generation for identifier {0} failed.'
.format(identifier), exc_info=True)
return ''
def __call__(self, form, field):
"""Validate."""
schemes = idutils.detect_identifier_schemes(field.data)
if not schemes:
raise ValidationError(self.message)
def _deserialize(self, value, attr, data):
"""Deserialize persistent identifier value."""
value = super(PersistentId, self)._deserialize(value, attr, data)
value = value.strip()
schemes = idutils.detect_identifier_schemes(value)
if self.scheme and self.scheme.lower() not in schemes:
self.fail('invalid_scheme', scheme=self.scheme)
if not schemes:
self.fail('invalid_pid')
return idutils.normalize_pid(value, schemes[0]) \
if self.normalize else value
def get_scheme(self, obj):
"""Get scheme."""
scheme = obj.get('scheme')
if not scheme and obj.get('identifier'):
scheme = idutils.detect_identifier_schemes(obj['identifier'])
if scheme:
scheme = scheme[0]
return scheme if scheme else ""
def detect_scheme(self, data):
"""Load scheme."""
id_ = data.get('identifier')
scheme = data.get('scheme')
if not scheme and id_:
scheme = idutils.detect_identifier_schemes(id_)
if scheme:
data['scheme'] = scheme[0]
return data