Skip to content
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

Dummy works rake task #512

Merged
merged 13 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ fits.log
# VScode settings
/.vscode

coverage
coverage

spec/fixtures/dummy_works/*/*
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ gem "ffi", "~> 1.15"

gem 'json-canonicalization', '0.3.1' # https://github.com/dryruby/json-canonicalization/issues/2

gem 'prawn'

group :development, :test do
# gem 'pry' # temporily removing, seems to break something with sidekiq in development mode
gem 'pry' # temporily removing, seems to break something with sidekiq in development mode
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'solr_wrapper', '>= 0.3'
gem 'launchy'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,16 @@ GEM
passenger (6.0.17)
rack
rake (>= 0.8.1)
pdf-core (0.9.0)
pg (1.5.3)
posix-spawn (0.3.15)
power_converter (0.1.2)
prawn (2.4.0)
pdf-core (~> 0.9.0)
ttfunk (~> 1.7)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
psych (3.3.4)
public_suffix (5.0.3)
qa (5.10.0)
Expand Down Expand Up @@ -953,6 +960,7 @@ GEM
timeout (0.4.0)
tinymce-rails (5.10.7.1)
railties (>= 3.1.1)
ttfunk (1.7.0)
turbolinks (5.2.1)
turbolinks-source (~> 5.2)
turbolinks-source (5.2.0)
Expand Down Expand Up @@ -1039,6 +1047,8 @@ DEPENDENCIES
orderly
passenger (= 6.0.17)
pg
prawn
pry
rails (~> 5.2.8.1)
recaptcha
redis (~> 4.0)
Expand Down
164 changes: 164 additions & 0 deletions lib/tasks/generate_dummy_works.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
require 'rake'

namespace :gwss do

desc "Creates dummy works for development"
# Takes integer arguments for number of each work type to generate
# i.e.
# bundle exec rails gwss:create_dummy_works public_works=2 private_works=2 authenticated_works=1 RAILS_ENV=production
task :create_dummy_works => :environment do
# Sets these counts to either the argument passed in or 0 if no argument
public_work_count = ENV['public_works'].to_i || 0
private_work_count = ENV['private_works'].to_i || 0
authenticated_work_count = ENV['authenticated_works'].to_i || 0

# Finding first admin user
admin_user = Role.find_by(name: "admin").users.first

# Finding admin set
admin_set = Hyrax::AdminSetCreateService.find_or_create_default_admin_set
admin_set_collection_type = Hyrax::CollectionType.find_or_create_admin_set_type

# Check if PDF already exists at path, otherwise generate pdf
public_work_count.times do |index|
file_path = Rails.root.join('spec', 'fixtures', 'dummy_works', 'public', "public_work_#{index}.pdf")
if !File.file?(file_path)
Prawn::Document.generate file_path do |pdf|
pdf.text "This is public work #{index}", size: 80
end
end
end

private_work_count.times do |index|
file_path = Rails.root.join('spec', 'fixtures', 'dummy_works', 'private', "private_work_#{index}.pdf")
if !File.file?(file_path)
Prawn::Document.generate file_path do |pdf|
pdf.text "This is private work #{index}", size: 80
end
end
end

authenticated_work_count.times do |index|
file_path = Rails.root.join('spec', 'fixtures', 'dummy_works', 'authenticated', "authenticated_work_#{index}.pdf")
if !File.file?(file_path)
Prawn::Document.generate file_path do |pdf|
pdf.text "This is authenticated work #{index}", size: 80
end
end
end

# Create arrays of the file paths
public_files = Dir[File.join(Rails.root, 'spec', 'fixtures', 'dummy_works', 'public', '*')]
private_files = Dir[File.join(Rails.root, 'spec', 'fixtures', 'dummy_works', 'private', '*')]
authenticated_files = Dir[File.join(Rails.root, 'spec', 'fixtures', 'dummy_works', 'authenticated', '*')]

public_uploads = []
public_works = []

private_uploads = []
private_works = []

authenticated_uploads = []
authenticated_works = []

# Iterate through the file paths, create ETDs, attach files
public_files.each_with_index do |file_path, index|
next if GwEtd.exists?("public_work_#{index}")
file = File.open(file_path)
title = file_path.split('/').last.split('.').first.titleize

public_uploads << Hyrax::UploadedFile.create(user: admin_user, file: file)

public_works << create_public_etd(admin_user,
"public_work_#{index}",
title: [title],
description: ["This is a test public ETD"],
creator: ["Professor Test"],
keyword: ['Test', 'Public'],
rights_statement: 'http://rightsstatements.org/vocab/InC/1.0/',
publisher: ["A Fake Publisher Inc"],
language: ["English"],
contributor: ["Assistant Test"],
gw_affiliation: [""],
advisor: ["Advisor Test"],
resource_type: ["Article"])

AttachFilesToWorkJob.perform_now(public_works[index], [public_uploads[index]])
end

private_files.each_with_index do |file_path, index|
next if GwEtd.exists?("private_work_#{index}")
file = File.open(file_path)
title = file_path.split('/').last.split('.').first.titleize

private_uploads << Hyrax::UploadedFile.create(user: admin_user, file: file)

private_works << create_private_etd(admin_user,
"private_work_#{index}",
title: [title],
description: ["This is a test private ETD"],
creator: ["Professor Test"],
keyword: ['Test', 'Private'],
rights_statement: 'http://rightsstatements.org/vocab/InC/1.0/',
publisher: ["A Fake Publisher Inc"],
language: ["English"],
contributor: ["Assistant Test"],
gw_affiliation: [""],
advisor: ["Advisor Test"],
resource_type: ["Article"])

AttachFilesToWorkJob.perform_now(private_works[index], [private_uploads[index]])
end

authenticated_files.each_with_index do |file_path, index|
next if GwEtd.exists?("authenticated_work_#{index}")
file = File.open(file_path)
title = file_path.split('/').last.split('.').first.titleize

authenticated_uploads << Hyrax::UploadedFile.create(user: admin_user, file: file)

authenticated_works << create_authenticated_etd(admin_user,
"authenticated_work_#{index}",
title: [title],
description: ["This is a test authenticated ETD"],
creator: ["Professor Test"],
keyword: ['Test', 'Authenticated'],
rights_statement: 'http://rightsstatements.org/vocab/InC/1.0/',
publisher: ["A Fake Publisher Inc"],
language: ["English"],
contributor: ["Assistant Test"],
gw_affiliation: [""],
advisor: ["Advisor Test"],
resource_type: ["Article"])

AttachFilesToWorkJob.perform_now(authenticated_works[index], [authenticated_uploads[index]])
end

end
end

def create_etd(user, id, options)
work = GwEtd.where(id: id)
return work.first if work.present?
actor = Hyrax::CurationConcern.actor
attributes_for_actor = options
work = GwEtd.new(id: id)
actor_environment = Hyrax::Actors::Environment.new(work, Ability.new(user), attributes_for_actor)
actor.create(actor_environment)
work
end

def create_public_etd(user, id, options)
options[:visibility] = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC
create_etd(user, id, options)
end

def create_private_etd(user, id, options)
options[:visibility] = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE
create_etd(user, id, options)
end

def create_authenticated_etd(user, id, options)
options[:visibility] = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_AUTHENTICATED
create_etd(user, id, options)
end
2 changes: 1 addition & 1 deletion spec/features/deposit_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe "Deposit a PDF through dashboard" do

let(:admin_user) { FactoryBot.create(:admin_user) }
let(:pdf_path) { "#{Rails.root}/spec/fixtures/public_etds/hamlet.pdf" }
let(:pdf_path) { "#{Rails.root}/spec/fixtures/fixture_dummy.pdf" }

it 'can deposit a pdf' do
visit "/users/sign_in"
Expand Down
Binary file removed spec/fixtures/authenticated_etds/John-milton.jpeg
Binary file not shown.
Binary file removed spec/fixtures/authenticated_etds/paradise lost.pdf
Binary file not shown.
Binary file removed spec/fixtures/journal_collection/Random numbers.pdf
Binary file not shown.
Binary file not shown.
Binary file removed spec/fixtures/private_etds/sonnet 130.pdf
Binary file not shown.
Binary file removed spec/fixtures/public_etds/book report.pptx
Binary file not shown.
Binary file removed spec/fixtures/public_etds/galaxy.tif
Binary file not shown.
Binary file removed spec/fixtures/public_etds/hamlet.pdf
Binary file not shown.
Loading