• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / dd159394-4976-4e16-95a7-cb4145135a97

pending completion
dd159394-4976-4e16-95a7-cb4145135a97

Pull #962

circleci

Hector Correa
Oops, that property does not exist yet
Pull Request #962: Fetch pre-curation files from directly from AWS

14 of 14 new or added lines in 3 files covered. (100.0%)

1791 of 1860 relevant lines covered (96.29%)

129.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.63
/app/controllers/works_controller.rb
1
# frozen_string_literal: true
2

3
require "nokogiri"
1✔
4
require "open-uri"
1✔
5

6
# rubocop:disable Metrics/ClassLength
7
class WorksController < ApplicationController
1✔
8
  include ERB::Util
1✔
9
  around_action :rescue_aasm_error, only: [:approve, :withdraw, :resubmit, :validate, :create]
1✔
10

11
  skip_before_action :authenticate_user!
1✔
12
  before_action :authenticate_user!, unless: :public_request?
1✔
13

14
  ##
15
  # Public requests are requests that do not require authentication.
16
  # This is to enable PDC Discovery to index approved content via the RSS feed and
17
  # .json calls to individual works without needing to log in as a user.
18
  # Note that only approved works can be fetched for indexing.
19
  def public_request?
1✔
20
    return true if action_name == "index" && request.format.symbol == :rss
174✔
21
    return true if action_name == "show" && request.format.symbol == :json && Work.find(params[:id]).state == "approved"
172✔
22
    false
171✔
23
  end
24

25
  def index
1✔
26
    @works = Work.all
4✔
27
    respond_to do |format|
4✔
28
      format.html
4✔
29
      format.rss { render layout: false }
6✔
30
    end
31
  end
32

33
  # Renders the "step 0" information page before creating a new dataset
34
  def new
1✔
35
    if wizard_mode?
21✔
36
      render "new_submission"
8✔
37
    else
38
      @work = Work.new(created_by_user_id: current_user.id, collection: current_user.default_collection)
13✔
39
    end
40
  end
41

42
  def create
1✔
43
    @work = Work.new(created_by_user_id: current_user.id, collection_id: params_collection_id, user_entered_doi: params["doi"].present?)
2✔
44
    @work.resource = FormToResourceService.convert(params, @work)
2✔
45
    if @work.valid?
2✔
46
      @work.draft!(current_user)
2✔
47
      redirect_to work_url(@work), notice: "Work was successfully created."
2✔
48
    else
49
      render :new, status: :unprocessable_entity
×
50
    end
51
  end
52

53
  # Creates the new dataset
54
  def new_submission
1✔
55
    default_collection_id = current_user.default_collection.id
6✔
56
    work = Work.new(created_by_user_id: current_user.id, collection_id: default_collection_id)
6✔
57
    work.resource = FormToResourceService.convert(params, work)
6✔
58
    work.draft!(current_user)
6✔
59
    redirect_to edit_work_path(work, wizard: true)
6✔
60
  end
61

62
  ##
63
  # Show the information for the dataset with the given id
64
  # When requested as .json, return the internal json resource
65
  def show
1✔
66
    @work = Work.find(params[:id])
44✔
67
    # check if anything was added in S3 since we last viewed this object
68
    @work.attach_s3_resources
44✔
69
    @changes =  WorkActivity.changes_for_work(@work.id)
39✔
70
    @messages = WorkActivity.messages_for_work(@work.id)
39✔
71

72
    respond_to do |format|
39✔
73
      format.html do
39✔
74
        # Ensure that the Work belongs to a Collection
75
        @collection = @work.collection
37✔
76
        raise(Work::InvalidCollectionError, "The Work #{@work.id} does not belong to any Collection") unless @collection
37✔
77

78
        @can_curate = current_user.can_admin?(@collection)
36✔
79
        @work.mark_new_notifications_as_read(current_user.id)
36✔
80
      end
81
      format.json { render json: @work.to_json }
41✔
82
    end
83
  end
84

85
  def resolve_doi
1✔
86
    @work = Work.find_by_doi(params[:doi])
3✔
87
    redirect_to @work
2✔
88
  end
89

90
  def resolve_ark
1✔
91
    @work = Work.find_by_ark(params[:ark])
3✔
92
    redirect_to @work
2✔
93
  end
94

95
  # GET /works/1/edit
96
  def edit
1✔
97
    @work = Work.find(params[:id])
28✔
98
    if current_user && @work.editable_by?(current_user)
28✔
99
      if @work.approved? && !@work.administered_by?(current_user)
26✔
100
        redirect_to root_path, notice: I18n.t("works.approved.uneditable")
1✔
101
      else
102
        @uploads = @work.uploads
25✔
103
        @wizard_mode = wizard_mode?
2✔
104
        render "edit"
2✔
105
      end
106
    else
107
      Rails.logger.warn("Unauthorized attempt to edit work #{@work.id} by user #{current_user.uid}")
2✔
108
      redirect_to root_path
2✔
109
    end
110
  end
111

112
  def update
1✔
113
    @work = Work.find(params[:id])
22✔
114
    if current_user.blank? || !@work.editable_by?(current_user)
22✔
115
      Rails.logger.warn("Unauthorized attempt to update work #{@work.id} by user #{current_user.uid}")
1✔
116
      redirect_to root_path
1✔
117
    elsif !@work.editable_in_current_state?(current_user)
21✔
118
      redirect_to root_path, notice: I18n.t("works.approved.uneditable")
1✔
119
    else
120
      update_work
20✔
121
    end
122
  end
123

124
  # Prompt to select how to submit their files
125
  def attachment_select
1✔
126
    @work = Work.find(params[:id])
1✔
127
    @wizard_mode = true
1✔
128
  end
129

130
  # User selected a specific way to submit their files
131
  def attachment_selected
1✔
132
    @work = Work.find(params[:id])
3✔
133
    @wizard_mode = true
3✔
134
    @work.files_location = params["attachment_type"]
3✔
135
    @work.save!
3✔
136
    next_url = case @work.files_location
3✔
137
               when "file_upload"
138
                 work_file_upload_url(@work)
1✔
139
               when "file_cluster"
140
                 work_file_cluster_url(@work)
1✔
141
               else
142
                 work_file_other_url(@work)
1✔
143
               end
144
    redirect_to next_url
3✔
145
  end
146

147
  # Allow user to upload files directly
148
  def file_upload
1✔
149
    @work = Work.find(params[:id])
1✔
150
  end
151

152
  def file_uploaded
1✔
153
    @work = Work.find(params[:id])
3✔
154
    if pre_curation_uploads_param
3✔
155
      @work.pre_curation_uploads.attach(pre_curation_uploads_param)
2✔
156
      @work.save!
1✔
157
    end
158

159
    redirect_to(work_review_path)
2✔
160
  rescue StandardError => active_storage_error
161
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
162
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
163

164
    redirect_to work_file_upload_path(@work)
1✔
165
  end
166

167
  # Allow user to indicate where their files are located in the PUL Research Cluster
168
  def file_cluster
1✔
169
    @work = Work.find(params[:id])
1✔
170
  end
171

172
  # Allow user to indicate where their files are located in the WWW
173
  def file_other
1✔
174
    @work = Work.find(params[:id])
1✔
175
  end
176

177
  def review
1✔
178
    @work = Work.find(params[:id])
1✔
179
    if request.method == "POST"
1✔
180
      @work.location_notes = params["location_notes"]
1✔
181
      @work.save!
1✔
182
    end
183
  end
184

185
  def validate
1✔
186
    @work = Work.find(params[:id])
2✔
187
    @work.submission_notes = params["submission_notes"]
2✔
188
    @uploads = @work.uploads
2✔
189
    @wizard_mode = true
×
190
    @work.complete_submission!(current_user)
×
191
    redirect_to user_url(current_user)
×
192
  end
193

194
  def approve
1✔
195
    @work = Work.find(params[:id])
10✔
196
    @work.approve!(current_user)
10✔
197
    redirect_to work_path(@work)
6✔
198
  end
199

200
  def withdraw
1✔
201
    @work = Work.find(params[:id])
2✔
202
    @work.withdraw!(current_user)
2✔
203
    redirect_to work_path(@work)
1✔
204
  end
205

206
  def resubmit
1✔
207
    @work = Work.find(params[:id])
2✔
208
    @work.resubmit!(current_user)
2✔
209
    redirect_to work_path(@work)
1✔
210
  end
211

212
  def assign_curator
1✔
213
    work = Work.find(params[:id])
4✔
214
    work.change_curator(params[:uid], current_user)
4✔
215
    if work.errors.count > 0
3✔
216
      render json: { errors: work.errors.map(&:type) }, status: :bad_request
1✔
217
    else
218
      render json: {}
2✔
219
    end
220
  rescue => ex
221
    Rails.logger.error("Error changing curator for work: #{work.id}. Exception: #{ex.message}")
1✔
222
    render json: { errors: ["Cannot save dataset"] }, status: :bad_request
1✔
223
  end
224

225
  def add_message
1✔
226
    work = Work.find(params[:id])
2✔
227
    if params["new-message"].present?
2✔
228
      new_message_param = params["new-message"]
2✔
229
      sanitized_new_message = html_escape(new_message_param)
2✔
230

231
      work.add_message(sanitized_new_message, current_user.id)
2✔
232
    end
233
    redirect_to work_path(id: params[:id])
2✔
234
  end
235

236
  def add_provenance_note
1✔
237
    work = Work.find(params[:id])
1✔
238
    if params["new-provenance-note"].present?
1✔
239
      new_date = params["new-provenance-date"]
1✔
240
      new_note = html_escape(params["new-provenance-note"])
1✔
241

242
      work.add_provenance_note(new_date, new_note, current_user.id)
1✔
243
    end
244
    redirect_to work_path(id: params[:id])
1✔
245
  end
246

247
  # Outputs the Datacite XML representation of the work
248
  def datacite
1✔
249
    work = Work.find(params[:id])
2✔
250
    render xml: work.to_xml
2✔
251
  end
252

253
  def datacite_validate
1✔
254
    @errors = []
1✔
255
    @work = Work.find(params[:id])
1✔
256
    datacite_xml = Nokogiri::XML(@work.to_xml)
1✔
257
    schema_location = Rails.root.join("config", "schema")
1✔
258
    Dir.chdir(schema_location) do
1✔
259
      xsd = Nokogiri::XML::Schema(File.read("datacite_4_4.xsd"))
1✔
260
      xsd.validate(datacite_xml).each do |error|
1✔
261
        @errors << error
1✔
262
      end
263
    end
264
  end
265

266
  private
1✔
267

268
    def work_params
1✔
269
      params[:work] || params
20✔
270
    end
271

272
    def patch_params
1✔
273
      return {} unless params.key?(:patch)
8✔
274

275
      params[:patch]
6✔
276
    end
277

278
    def pre_curation_uploads_param
1✔
279
      return if patch_params.nil?
4✔
280

281
      patch_params[:pre_curation_uploads]
4✔
282
    end
283

284
    def rescue_aasm_error
1✔
285
      yield
18✔
286
    rescue AASM::InvalidTransition => error
287
      message = error.message
6✔
288
      if @work.errors.count > 0
6✔
289
        message = @work.errors.to_a.join(", ")
3✔
290
      end
291
      logger.warn("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
6✔
292
      @errors = ["Cannot #{@work.current_transition}: #{message}"]
6✔
293
      render error_action, status: :unprocessable_entity
6✔
294
    end
295

296
    def error_action
1✔
297
      if action_name == "create"
6✔
298
        :new
×
299
      elsif action_name == "validate"
6✔
300
        :edit
×
301
      else
302
        :show
6✔
303
      end
304
    end
305

306
    def wizard_mode?
1✔
307
      params[:wizard] == "true"
43✔
308
    end
309

310
    def update_work
1✔
311
      @wizard_mode = wizard_mode?
20✔
312
      upload_service = WorkUploadsEditService.new(@work, current_user)
20✔
313
      if @work.approved?
20✔
314
        upload_keys = work_params[:deleted_uploads] || []
5✔
315
        deleted_uploads = upload_service.find_post_curation_uploads(upload_keys: upload_keys)
5✔
316

317
        return head(:forbidden) unless deleted_uploads.empty?
5✔
318
      else
319
        @work = upload_service.update_precurated_file_list(work_params)
15✔
320
      end
321

322
      process_updates
19✔
323
    end
324

325
    def update_params
1✔
326
      {
327
        collection_id: params_collection_id,
36✔
328
        resource: FormToResourceService.convert(params, @work)
329
      }
330
    end
331

332
    def process_updates
1✔
333
      resource_before = @work.resource
19✔
334
      if @work.update(update_params)
19✔
335

336
        resource_compare = ResourceCompareService.new(resource_before, update_params[:resource])
17✔
337
        @work.log_changes(resource_compare, current_user.id)
17✔
338

339
        if @wizard_mode
17✔
340
          redirect_to work_attachment_select_url(@work)
1✔
341
        else
342
          redirect_to work_url(@work), notice: "Work was successfully updated."
16✔
343
        end
344
      else
345
        @uploads = @work.uploads
2✔
346
        render :edit, status: :unprocessable_entity
×
347
      end
348
    end
349

350
    def params_collection_id
1✔
351
      # Do not allow a nil for the collection id
352
      @params_collection_id ||= begin
38✔
353
        collection_id = params[:collection_id]
20✔
354
        if collection_id.blank?
20✔
355
          collection_id = current_user.default_collection.id
2✔
356
          Honeybadger.notify("We got a nil collection as part of the parameters #{params} #{request}")
2✔
357
        end
358
        collection_id
20✔
359
      end
360
    end
361
end
362
# rubocop:enable Metrics/ClassLength
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc