-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4414 - Fix typo in bulk export api #4415
base: development
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ def help | |
render plain: api_routes.map {|r| "#{r.verb}\t#{r.path.spec}" }.join("\n") | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,60 +17,55 @@ def index | |
else | ||
exports = @api_user.bulk_exports | ||
end | ||
render json: exports.to_json(except: [:updated_at, :user_id, :collection_id], include: {:collection => {only: [:title, :slug]}}) | ||
render json: exports.to_json(except: [:updated_at, :user_id, :collection_id], include: { collection: { only: [:title, :slug] } }) | ||
else | ||
render status: 401, json: 'You must use an API token to access bulk exports' | ||
end | ||
end | ||
|
||
|
||
def start | ||
if @api_user | ||
collection_slug = params[:collection_slug] | ||
if collection_slug | ||
collection = Collection.where(slug: collection_slug).first | ||
if collection.nil? | ||
render status: 404, json: "No collection exists with the slug #{collection_slug}" | ||
return | ||
end | ||
collection = Collection.where(slug: collection_slug).first | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still surprised that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only have :set_api_user in the filter. We also expect :collection_slug instead of :collection_id so it won't be set using our before filters. I think what we have now is ideal. We want to throw the message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've convinced me. |
||
if collection.nil? | ||
render status: 404, json: "No collection exists with the slug #{collection_slug}" | ||
return | ||
end | ||
|
||
if collection.show_to?(@api_user) | ||
# try to parse the bulk_export | ||
bulk_export = BulkExport.new | ||
bulk_export.collection = collection | ||
bulk_export.user = @api_user | ||
bulk_export.status = BulkExport::Status::NEW | ||
bulk_export.plaintext_verbatim_page = !(params[:plaintext_verbatim_page].blank?) | ||
bulk_export.plaintext_verbatim_work = !(params[:plaintext_verbatim_work].blank?) | ||
bulk_export.plaintext_emended_page = !(params[:plaintext_emended_page].blank?) | ||
bulk_export.plaintext_emended_work = !(params[:plaintext_emended_work].blank?) | ||
bulk_export.plaintext_searchable_page = !(params[:plaintext_searchable_page].blank?) | ||
bulk_export.plaintext_searchable_work = !(params[:plaintext_searchable_work].blank?) | ||
bulk_export.tei_work = !(params[:tei_work].blank?) | ||
bulk_export.html_page = !(params[:html_page].blank?) | ||
bulk_export.html_work = !(params[:html_work].blank?) | ||
bulk_export.subject_csv_collection = !(params[:subject_csv_collection].blank?) | ||
bulk_export.subject_details_csv_collection = !(params[:subject_details_csv_collection].blank?) | ||
bulk_export.table_csv_collection = !(params[:table_csv_collection].blank?) | ||
bulk_export.table_csv_work = !(params[:table_csv_work].blank?) | ||
bulk_export.notes_csv = !(params[:notes_csv].blank?) | ||
bulk_export.save | ||
bulk_export.submit_export_process | ||
if collection.show_to?(@api_user) | ||
# try to parse the bulk_export | ||
bulk_export = BulkExport.new | ||
bulk_export.collection = collection | ||
bulk_export.user = @api_user | ||
bulk_export.status = BulkExport::Status::NEW | ||
bulk_export.plaintext_verbatim_page = !params[:plaintext_verbatim_page].blank? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remind me what the precedence is of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ideally I'd like to change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! I wasn't aware that |
||
bulk_export.plaintext_verbatim_work = !params[:plaintext_verbatim_work].blank? | ||
bulk_export.plaintext_emended_page = !params[:plaintext_emended_page].blank? | ||
bulk_export.plaintext_emended_work = !params[:plaintext_emended_work].blank? | ||
bulk_export.plaintext_searchable_page = !params[:plaintext_searchable_page].blank? | ||
bulk_export.plaintext_searchable_work = !params[:plaintext_searchable_work].blank? | ||
bulk_export.tei_work = !params[:tei_work].blank? | ||
bulk_export.html_page = !params[:html_page].blank? | ||
bulk_export.html_work = !params[:html_work].blank? | ||
bulk_export.subject_csv_collection = !params[:subject_csv_collection].blank? | ||
bulk_export.subject_details_csv_collection = !params[:subject_details_csv_collection].blank? | ||
bulk_export.table_csv_collection = !params[:table_csv_collection].blank? | ||
bulk_export.table_csv_work = !params[:table_csv_work].blank? | ||
bulk_export.notes_csv = !params[:notes_csv].blank? | ||
bulk_export.save | ||
bulk_export.submit_export_process | ||
|
||
response = { | ||
:id => bulk_export.id, | ||
:status => bulk_export.status, | ||
:status_uri => api_v1_bulk_export_status_url(bulk_export.id), | ||
:download_uri => api_v1_bulk_export_download_url(bulk_export.id) | ||
} | ||
response = { | ||
id: bulk_export.id, | ||
status: bulk_export.status, | ||
status_uri: api_v1_bulk_export_status_url(bulk_export.id), | ||
download_uri: api_v1_bulk_export_download_url(bulk_export.id) | ||
} | ||
|
||
render status: 202, json: response.to_json | ||
render status: 202, json: response.to_json | ||
|
||
else | ||
render status: 403, json: 'User #{@api_user} is not authorized to view #{collection.title}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one of the status typo. We are not interpolating the string here due to single quotes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's fix it! |
||
end | ||
else | ||
render status: 401, json: 'You must use a collection slug in the URL to create a bulk export' | ||
render status: 403, json: "User #{@api_user} is not authorized to view #{collection.title}" | ||
end | ||
else | ||
render status: 401, json: 'You must use an API token to access bulk exports' | ||
|
@@ -80,59 +75,49 @@ def start | |
def status | ||
if @api_user | ||
bulk_export_id = params[:bulk_export_id] | ||
if bulk_export_id | ||
bulk_export = @api_user.bulk_exports.where(:id => bulk_export_id).first | ||
if bulk_export | ||
response = { | ||
:id => bulk_export.id, | ||
:status => bulk_export.status, | ||
:status_uri => api_v1_bulk_export_status_url(bulk_export.id), | ||
:download_uri => api_v1_bulk_export_download_url(bulk_export.id) | ||
} | ||
render status: 200, json: response.to_json | ||
else | ||
render status: 403, json: 'User #{@api_user} has no bulk export with ID #{bulk_export_id}' | ||
end | ||
bulk_export = @api_user.bulk_exports.find_by(id: bulk_export_id) | ||
|
||
if bulk_export | ||
response = { | ||
id: bulk_export.id, | ||
status: bulk_export.status, | ||
status_uri: api_v1_bulk_export_status_url(bulk_export.id), | ||
download_uri: api_v1_bulk_export_download_url(bulk_export.id) | ||
} | ||
render status: 200, json: response.to_json | ||
else | ||
render status: 401, json: 'You must use a bulk export ID in the URL' | ||
render status: 403, json: "User #{@api_user} has no bulk export with ID #{bulk_export_id}" | ||
end | ||
else | ||
render status: 401, json: 'You must use an API token to access bulk exports' | ||
end | ||
end | ||
|
||
|
||
def download | ||
if @api_user | ||
bulk_export_id = params[:bulk_export_id] | ||
if bulk_export_id | ||
bulk_export = @api_user.bulk_exports.where(:id => bulk_export_id).first | ||
if bulk_export | ||
if bulk_export.status == BulkExport::Status::FINISHED | ||
send_file(bulk_export.zip_file_name, | ||
filename: "fromthepage_export.zip", | ||
:content_type => "application/zip") | ||
else | ||
if bulk_export.status == BulkExport::Status::CLEANED | ||
render status: 410, json: "Bulk export #{bulk_export_id} has been deleted. Please start a new export." | ||
elsif bulk_export.status == BulkExport::Status::ERROR | ||
render status: 410, json: "Bulk export #{bulk_export_id} failed with an error. Please report this to support. Re-running an export with different requested formats might succeed." | ||
else | ||
render status: 409, json: "Bulk export #{bulk_export_id} is not ready to download. It is probably still running, but might have failed with an un-caught error." | ||
end | ||
end | ||
bulk_export = @api_user.bulk_exports.find_by(id: bulk_export_id) | ||
if bulk_export | ||
case bulk_export.status | ||
when BulkExport::Status::FINISHED | ||
send_file( | ||
bulk_export.zip_file_name, | ||
filename: 'fromthepage_export.zip', | ||
content_type: 'application/zip' | ||
) | ||
when BulkExport::Status::CLEANED | ||
render status: 410, json: "Bulk export #{bulk_export_id} has been deleted. Please start a new export." | ||
when BulkExport::Status::ERROR | ||
render status: 410, json: "Bulk export #{bulk_export_id} failed with an error. Please report this to support. Re-running an export with different requested formats might succeed." | ||
else | ||
render status: 403, json: 'User #{@api_user} has no bulk export with ID #{bulk_export_id}' | ||
render status: 409, json: "Bulk export #{bulk_export_id} is not ready to download. It is probably still running, but might have failed with an un-caught error." | ||
end | ||
else | ||
render status: 401, json: 'You must use a bulk export ID in the URL' | ||
render status: 403, json: "User #{@api_user} has no bulk export with ID #{bulk_export_id}" | ||
end | ||
else | ||
render status: 401, json: 'You must use an API token to access bulk exports' | ||
end | ||
end | ||
|
||
private | ||
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
FactoryBot.define do | ||
factory :bulk_export do | ||
collection_id { association(:collection).id } | ||
user_id { association(:user).id } | ||
|
||
trait :new do | ||
status { BulkExport::Status::NEW } | ||
end | ||
|
||
trait :queued do | ||
status { BulkExport::Status::QUEUED } | ||
end | ||
|
||
trait :processing do | ||
status { BulkExport::Status::PROCESSING } | ||
end | ||
|
||
trait :finished do | ||
status { BulkExport::Status::FINISHED } | ||
end | ||
|
||
trait :cleaned do | ||
status { BulkExport::Status::CLEANED } | ||
end | ||
|
||
trait :error do | ||
status { BulkExport::Status::ERROR } | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,24 @@ | |
sequence(:display_name) { |n| "admin_#{n}_login" } | ||
sequence(:login) { |n| "admin_#{n}_login" } | ||
end | ||
|
||
factory :unique_user do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Took the chance to add a temporary solution to one of my minor annoyances with Factories when combined with The issue is, doing just Using SecureRandom should work here. Initially I planned to add this to the already defined factory bots above, but it caused some legacy specs (especially Devise specs which needed to query created user by |
||
sequence(:login) { "user_#{SecureRandom.hex(2)}_login" } | ||
sequence(:email) { "user_#{SecureRandom.hex(2)}@sample.com" } | ||
password { 'password' } | ||
password_confirmation { 'password' } | ||
|
||
trait :with_api_key do | ||
api_key { User.generate_api_key } | ||
end | ||
|
||
trait :owner do | ||
admin { true } | ||
end | ||
|
||
trait :admin do | ||
admin { true } | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'spec_helper' | ||
|
||
describe Api::ApiController do | ||
describe '#help' do | ||
let(:action_path) { api_path } | ||
|
||
let(:subject) { get action_path } | ||
|
||
it 'renders status and plain_text' do | ||
subject | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(response.content_type).to eq('text/plain; charset=utf-8') | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is redundant checking. The route requires /:collection_id so this will always be present. It should suffice that we check if collection.nil? instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds true, but what are we doing differently with a
collection_slug
parameter versus the usualcollection_id
(which should populate@collection
?