Skip to content

Commit

Permalink
Merge pull request #512 from gwu-libraries/dummy-works-rake
Browse files Browse the repository at this point in the history
Dummy works rake task
  • Loading branch information
alepbloyd authored Feb 26, 2024
2 parents 49c733e + 650c0d5 commit 09325a8
Show file tree
Hide file tree
Showing 22 changed files with 188 additions and 13,644 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ fits.log
# VScode settings
/.vscode

coverage
coverage

# Save dummy works folders, not dummy works
spec/fixtures/dummy_works/*/*
!spec/fixtures/dummy_works/*/.keep
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
169 changes: 169 additions & 0 deletions lib/tasks/create_dummy_works.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
require 'rake'

namespace :gwss do

desc "Creates dummy works for development"
# Takes string argument for admin user email
# Takes integer arguments for number of each work type to generate
# i.e.
# bundle exec rails gwss:create_dummy_works admin_email="[email protected]" 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 user from email
admin_user = User.find_by(email: ENV['admin_email'])

# Validating user
abort("User not found") if admin_user.nil?
abort("User is not admin") if !admin_user.admin?

# 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|
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,
Noid::Rails::Service.new.mint,
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"],
license: ["http://www.europeana.eu/portal/rights/rr-r.html"],
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|
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,
Noid::Rails::Service.new.mint,
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"],
license: ["http://www.europeana.eu/portal/rights/rr-r.html"],
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|
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,
Noid::Rails::Service.new.mint,
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"],
license: ["http://www.europeana.eu/portal/rights/rr-r.html"],
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.
Empty file.
Empty file.
Empty file.
File renamed without changes.
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

0 comments on commit 09325a8

Please sign in to comment.