Skip to content

Commit

Permalink
Add repositories csv export
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhallett committed Sep 20, 2019
1 parent a06b7ea commit 60f1ef8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 49 deletions.
115 changes: 69 additions & 46 deletions app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class RepositoriesController < ApplicationController
include ActionController::MimeResponds
include Countable

before_action :set_repository, only: [:show, :update, :destroy]
Expand All @@ -19,21 +20,21 @@ def index
page = page_from_params(params)

if params[:id].present?
response = Client.find_by_id(params[:id])
response = Client.find_by_id(params[:id])
elsif params[:ids].present?
response = Client.find_by_id(params[:ids], page: page, sort: sort)
else
response = Client.query(params[:query],
year: params[:year],
response = Client.query(params[:query],
year: params[:year],
provider_id: params[:provider_id],
consortium_id: params[:consortium_id],
re3data_id: params[:re3data_id],
opendoar_id: params[:opendoar_id],
software: params[:software],
certificate: params[:certificate],
repository_type: params[:repository_type],
certificate: params[:certificate],
repository_type: params[:repository_type],
client_type: params[:client_type],
page: page,
page: page,
sort: sort)
end

Expand All @@ -46,43 +47,65 @@ def index
certificates = total > 0 ? facet_by_key(response.response.aggregations.certificates.buckets) : nil
client_types = total > 0 ? facet_by_key(response.response.aggregations.client_types.buckets) : nil
repository_types = total > 0 ? facet_by_key(response.response.aggregations.repository_types.buckets) : nil

options = {}
options[:meta] = {
total: total,
"totalPages" => total_pages,
page: page[:number],
years: years,
providers: providers,
"clientTypes" => client_types,
"repositoryTypes" => repository_types,
certificates: certificates,
software: software
}.compact

options[:links] = {
self: request.original_url,
next: response.results.blank? ? nil : request.base_url + "/clients?" + {
query: params[:query],
"provider-id" => params[:provider_id],
software: params[:software],
certificate: params[:certificate],
"client-type" => params[:client_type],
"repository-type" => params[:repository_type],
year: params[:year],
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
options[:params] = { current_ability: current_ability }

fields = fields_from_params(params)
if fields
render json: RepositorySerializer.new(response.results, options.merge(fields: fields)).serialized_json, status: :ok
else
render json: RepositorySerializer.new(response.results, options).serialized_json, status: :ok
respond_to do |format|
format.json do
options = {}
options[:meta] = {
total: total,
"totalPages" => total_pages,
page: page[:number],
years: years,
providers: providers,
"clientTypes" => client_types,
"repositoryTypes" => repository_types,
certificates: certificates,
software: software
}.compact

options[:links] = {
self: request.original_url,
next: response.results.blank? ? nil : request.base_url + "/clients?" + {
query: params[:query],
"provider-id" => params[:provider_id],
software: params[:software],
certificate: params[:certificate],
"client-type" => params[:client_type],
"repository-type" => params[:repository_type],
year: params[:year],
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
options[:params] = { current_ability: current_ability }

fields = fields_from_params(params)
if fields
render json: RepositorySerializer.new(response.results, options.merge(fields: fields)).serialized_json, status: :ok
else
render json: RepositorySerializer.new(response.results, options).serialized_json, status: :ok
end
end
header = %w(
accountName
fabricaAccountId
parentFabricaAccountId
salesForceId
parentSalesForceId
isActive
created
updated
re3data_id
client_type
alternate_name
description
url
software
system_email)
puts response.records.to_a[0].to_json
format.csv { render request.format.to_sym => response.records.to_a, header: header }
end
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Raven.capture_exception(exception)
Expand All @@ -98,7 +121,7 @@ def show
fail ActiveRecord::RecordNotFound unless repository.present?

options = {}
options[:meta] = {
options[:meta] = {
dois: doi_count(client_id: params[:id]),
"resourceTypes" => resource_type_count(client_id: params[:id]) }.compact
options[:include] = @include
Expand All @@ -117,7 +140,7 @@ def create
options = {}
options[:is_collection] = false
options[:params] = { current_ability: current_ability }

render json: RepositorySerializer.new(@client, options).serialized_json, status: :created
else
logger.warn @client.errors.inspect
Expand All @@ -132,7 +155,7 @@ def update
options[:meta] = { dois: doi_count(client_id: params[:id]) }
options[:is_collection] = false
options[:params] = { current_ability: current_ability }

render json: RepositorySerializer.new(@client, options).serialized_json, status: :ok
else
logger.warn @client.errors.inspect
Expand Down Expand Up @@ -160,7 +183,7 @@ def destroy

def totals
page = { size: 0, number: 1}

state = current_user.present? && current_user.is_admin_or_staff? && params[:state].present? ? params[:state] : "registered,findable"
response = Doi.query(nil, provider_id: params[:provider_id], state: state, page: page, totals_agg: true)
total = response.results.total
Expand Down
29 changes: 27 additions & 2 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class Client < ActiveRecord::Base
normalizer: {
keyword_lowercase: { type: "custom", filter: %w(lowercase) }
},
filter: {
ascii_folding: { type: 'asciifolding', preserve_original: true }
filter: {
ascii_folding: { type: 'asciifolding', preserve_original: true }
}
}
} do
Expand Down Expand Up @@ -256,6 +256,31 @@ def self.query_aggregations
}
end


def csv
client = {
name: name,
client_id: symbol,
provider_id: provider.present? ? provider.symbol : '',
salesforce_id: salesforce_id,
consortium_salesforce_id: provider.present? ? provider.salesforce_id : '',
is_active: is_active == "\x01",
created: created,
updated: updated,
re3data_id: re3data_id,
client_type: client_type,
alternate_name: alternate_name,
description: description,
url: url,
software: software,
system_email: system_email,
# "service_contact" => service_contact,
# "contact_email" => contact_email,
}.values

CSV.generate { |csv| csv << client }
end

def uid
symbol.downcase
end
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
get '/dois/text/x-bibliography', :to => 'dois#index', defaults: { format: :citation }
get '/providers/text/csv', :to => 'providers#index', defaults: { format: :csv }
get 'providers/random', :to => 'providers#random'
get '/organizations/text/csv', :to => 'organizations#index', defaults: { format: :csv }
get '/repositories/text/csv', :to => 'repositories#index', defaults: { format: :csv }


# manage DOIs
post 'dois/validate', :to => 'dois#validate'
Expand Down Expand Up @@ -83,7 +86,7 @@
constraints(-> (req) { req.env["HTTP_ACCEPT"].to_s.exclude?("version=2") }) do
resources :old_events, path: "events"
end

resources :prefixes, constraints: { :id => /.+/ }
resources :provider_prefixes, path: 'provider-prefixes'
resources :random, only: [:index]
Expand Down

0 comments on commit 60f1ef8

Please sign in to comment.