Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if name.endswith('/'):
name = name[:-1]
name = name.split('/')[-1]
if _is_name_unique(context, name, key, converted_data, package):
return name
else:
name, extension = os.path.splitext(name)
for i in range(2, 1000):
new_name = "{0}_{1}{2}".format(name, i, extension)
if _is_name_unique(context, new_name, key, converted_data, package):
return new_name
# If we get here then 2...999 were all taken.
raise toolkit.Invalid(toolkit._("I got bored trying to come up with "
"a unique name for this file, you'll have to supply one yourself"))
def update_field_index_validator(key, data, errors, context):
index = data[key]
try:
index = int(index)
except (ValueError, TypeError):
raise toolkit.Invalid(toolkit._("index must be an int"))
if index < 0:
raise toolkit.Invalid(toolkit._("You can't have a negative index"))
# Make sure the resource has a field with this index.
resource_id = data[('resource_id',)]
# We're assuming that resource_id has already been validated and is valid,
# so resource_schema_show() won't raise an exception here.
schema = toolkit.get_action('resource_schema_show')(
context, {'resource_id': resource_id})
matching_fields = []
for field in schema.get('fields', []):
if field['index'] == index:
matching_fields.append(field)
if len(matching_fields) == 0:
raise toolkit.Invalid(toolkit._("There's no field with the given "
"index"))
if len(matching_fields) > 1:
raise toolkit.Invalid(toolkit._("There's more than one field with the "
def get_issue_subject(issue):
site_title = get_site_title()
dataset = model.Package.get(issue['dataset_id'])
return toolkit._(
'[{site_title} Issue] {dataset}').format(
site_title=site_title,
dataset=dataset.title,
)
def _comment_or_issue_report(issue_or_comment, user_ref, dataset_id, session):
user_obj = model.User.get(user_ref)
try:
issue_or_comment.report_abuse(session, user_obj.id)
except IntegrityError:
session.rollback()
raise ReportAlreadyExists(
p.toolkit._('Issue has already been reported by this user')
)
try:
# if you're an org admin/editor (can edit the dataset), it gets marked
# as abuse immediately
context = {
'user': user_ref,
'session': session,
'model': model,
}
p.toolkit.check_access('package_update', context,
data_dict={'id': dataset_id})
issue_or_comment.change_visibility(session, u'hidden')
issue_or_comment.abuse_status = issuemodel.AbuseStatus.abuse.value
return {'visibility': issue_or_comment.visibility,
'abuse_reports': issue_or_comment.abuse_reports,
def is_valid_status(value, context):
if value in issuemodel.ISSUE_STATUS:
return value
else:
raise toolkit.Invalid(toolkit._(
'{0} is not a valid status'.format(value))
)
def user_create(context, data_dict):
msg = p.toolkit._('Users cannot be created.')
return _no_permissions(context, msg)
try:
issue = toolkit.get_action('issue_update')(
data_dict={
'issue_number': issue_number,
'assignee_id': assignee['id'],
'dataset_id': dataset_id
}
)
notifications = p.toolkit.asbool(
config.get('ckanext.issues.send_email_notifications')
)
if notifications:
subject = get_issue_subject(issue)
body = toolkit._('Assigned to {user}'.format(
user=assignee['display_name']))
user_obj = model.User.get(assignee_id)
try:
mailer.mail_user(user_obj, subject, body)
except mailer.MailerException, e:
log.debug(e.message)
except toolkit.NotAuthorized:
msg = _('Unauthorized to assign users to issue'.format(
issue_number))
toolkit.abort(401, msg)
except toolkit.ValidationError, e:
toolkit.abort(404)
return p.toolkit.redirect_to('issues_show',
if index < 0:
raise toolkit.Invalid(toolkit._("You can't have a negative index"))
# Make sure the resource has a field with this index.
resource_id = data[('resource_id',)]
# We're assuming that resource_id has already been validated and is valid,
# so resource_schema_show() won't raise an exception here.
schema = toolkit.get_action('resource_schema_show')(
context, {'resource_id': resource_id})
matching_fields = []
for field in schema.get('fields', []):
if field['index'] == index:
matching_fields.append(field)
if len(matching_fields) == 0:
raise toolkit.Invalid(toolkit._("There's no field with the given "
"index"))
if len(matching_fields) > 1:
raise toolkit.Invalid(toolkit._("There's more than one field with the "
"given index (this shouldn't happen, "
"something has gone wrong)"))
data[key] = index
def foreign_key_field_validator(key, data, errors, context):
fkey_fields = data[key]
resource_id = data[('resource_id', )]
schema = toolkit.get_action('resource_schema_show')(
context, {'resource_id': resource_id})
if isinstance(fkey_fields, basestring):
fkey_fields = [fkey_fields]
field_names = [field['name'] for field in schema.get('fields', [])]
for field_name in fkey_fields:
if field_name not in field_names:
raise toolkit.Invalid(toolkit._("No field with that name"))