Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacite/lupo
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Sep 29, 2020
2 parents 0095262 + ed61150 commit f9da0ea
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,12 @@ def safe_params
xml = xml.strip
end

fail(ActionController::UnpermittedParameters, ["creators must be an Array"]) if p[:creators]&.respond_to?(:keys)
p[:creators]&.each do |c|
fail(ActionController::UnpermittedParameters, ["nameIdentifiers must be an Array"]) if c[:nameIdentifiers]&.respond_to?(:keys)
end

fail(ActionController::UnpermittedParameters, ["contributors must be an Array"]) if p[:contributors]&.respond_to?(:keys)
p[:contributors]&.each do |c|
fail(ActionController::UnpermittedParameters, ["nameIdentifiers must be an Array"]) if c[:nameIdentifiers]&.respond_to?(:keys)
end
Expand Down
45 changes: 41 additions & 4 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ def self.gql_query(query, options={})
filter << { terms: { "client.certificate" => options[:certificate].split(",") }} if options[:certificate].present?
filter << { term: { "creators.nameIdentifiers.nameIdentifier" => "https://orcid.org/#{orcid_from_url(options[:user_id])}" }} if options[:user_id].present?
filter << { term: { "creators.nameIdentifiers.nameIdentifierScheme" => "ORCID" }} if options[:has_person].present?

# match either one of has_affiliation, has_organization, has_funder or has_member
if options[:has_organization].present?
should << { term: { "creators.nameIdentifiers.nameIdentifierScheme" => "ROR" }}
Expand Down Expand Up @@ -1067,7 +1067,7 @@ def self.query(query, options={})
filter << { terms: { "client.certificate" => options[:certificate].split(",") }} if options[:certificate].present?
filter << { term: { "creators.nameIdentifiers.nameIdentifier" => "https://orcid.org/#{orcid_from_url(options[:user_id])}" }} if options[:user_id].present?
filter << { term: { "creators.nameIdentifiers.nameIdentifierScheme" => "ORCID" }} if options[:has_person].present?

# match either one of has_affiliation, has_organization, or has_funder
if options[:has_organization].present?
should << { term: { "creators.nameIdentifiers.nameIdentifierScheme" => "ROR" }}
Expand Down Expand Up @@ -2133,7 +2133,7 @@ def set_defaults
end

def update_agency
if agency.blank? || agency.casecmp?("datacite")
if agency.blank? || agency.casecmp?("datacite")
self.agency = "datacite"
self.type = "DataciteDoi"
elsif agency.casecmp?("crossref")
Expand Down Expand Up @@ -2310,4 +2310,41 @@ def self.migrate_landing_page(options={})

"Finished migrating landing pages."
end
end

def self.add_index_type(options={})
return nil unless options[:from_id].present?


from_id = options[:from_id].to_i
until_id = (options[:until_id] || (from_id + 499)).to_i

# get every id between from_id and end_id
count = 0

Rails.logger.info "[migration_index_types] adding type information for DOIs with IDs #{from_id} - #{until_id}."

Doi.where(id: from_id..until_id).where('type' => nil).find_each(batch_size: 500) do |doi|
begin
if doi.agency.casecmp?("datacite")
type = "DataciteDoi"
elsif doi.agency.casecmp?("crossref")
type = "OtherDoi"
else
type = "DataciteDoi"
end

doi.update_columns("type" => type)

count += 1
Rails.logger.info "Updated #{doi.doi} (#{doi.id})"

rescue => error
Rails.logger.error "Error updating #{doi.doi} (#{doi.id}), #{error.message}"
end
end

"Finished updating dois, total #{count}"
end


end
9 changes: 9 additions & 0 deletions lib/tasks/doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,13 @@ namespace :doi do
count = Doi.delete_dois_by_prefix(ENV['PREFIX'])
puts "#{count} DOIs with prefix #{ENV['PREFIX']} deleted."
end

desc 'Add type information to dois based on id range'
task :add_index_type => :environment do
options = {
from_id: ENV['FROM_ID'],
until_id: ENV['UNTIL_ID']
}
puts Doi.add_index_type(options)
end
end
66 changes: 66 additions & 0 deletions spec/fixtures/files/nasa_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"data": {
"type":"dois",
"id":"10.14454/rdnt.1x18sn",
"attributes":{
"doi": "10.14454/rdnt.1x18sn",
"prefix":"10.14454",
"creators":[
{
"name":"Maskey, Manil",
"nameType":"Personal",
"givenName":"Manil",
"familyName":"Maskey"
},
{
"name":"Ramachandran, Rahul",
"nameType":"Personal",
"givenName":"Rahul",
"familyName":"Ramachandran"
},
{
"name":"Gurung, Iksha",
"nameType":"Personal",
"givenName":"Iksha",
"familyName":"Gurung"
},
{
"name":"Ramasubramanian, Muthukumaran",
"nameType":"Personal",
"givenName":"Muthukumaran",
"familyName":"Ramasubramanian"
}
],
"titles":{
"title":"Tropical Cyclone Satellite Imagery and Wind Speed Dataset"
},
"publisher": "Radiant MLHub",
"publicationYear": 2020,
"types":{
"resourceTypeGeneral":"Dataset"
},
"version":1.0,
"contributors":{
"nameType":"Organizational",
"name":"NASA Interagency Implementation and Advanced Concepts Team (IMPACT)",
"contributorType":"DataCurator"
},
"dates":{
"dateType":"Valid",
"date": "2000/2019"
},
"fundingReferences":{
"funderName":"NASA Earth Science Data Systems Program"
},
"rightsList":{
"rights":"CC BY 4.0",
"rightsUri":"https://creativecommons.org/licenses/by/4.0/"
},
"url":"http://registry.mlhub.earth/10.34911/rdnt.1x18sn",
"landingPage":{
"url":"http://registry.mlhub.earth/10.34911/rdnt.1x18sn"
},
"event":"publish"
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions spec/requests/datacite_dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,16 @@
end
end

context 'when the request has wrong object in nameIDentifiers' do
let(:valid_attributes) { JSON.parse(file_fixture('nasa_error.json').read) }


it 'fails to create a Doi' do
post '/dois', valid_attributes, headers

expect(last_response.status).to eq(422)
end
end


# context 'when the request is a large xml file' do
Expand Down

0 comments on commit f9da0ea

Please sign in to comment.