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

pulibrary / pdc_describe / 4a26368f-294e-46cb-adec-4720abaf1502

pending completion
4a26368f-294e-46cb-adec-4720abaf1502

Pull #1060

circleci

jrgriffiniii
wip
Pull Request #1060: [wip] Integrating Support for Upload Snapshots

142 of 142 new or added lines in 6 files covered. (100.0%)

2006 of 2049 relevant lines covered (97.9%)

201.53 hits per line

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

99.57
/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
460✔
21
    return true if action_name == "show" && request.format.symbol == :json && Work.find(params[:id]).state == "approved"
458✔
22
    false
457✔
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?
25✔
36
      render "new_submission"
8✔
37
    else
38
      @work = Work.new(created_by_user_id: current_user.id, collection: current_user.default_collection)
17✔
39
    end
40
  end
41

42
  def create
1✔
43
    # TODO: We need to process files submitted by the user in this method.
44
    # We currently do not and therefore there are not saved to the work.
45
    # See https://github.com/pulibrary/pdc_describe/issues/1041
46
    @work = Work.new(created_by_user_id: current_user.id, collection_id: params_collection_id, user_entered_doi: params["doi"].present?)
20✔
47
    @work.resource = FormToResourceService.convert(params, @work)
20✔
48
    if @work.valid?
20✔
49
      @work.draft!(current_user)
18✔
50
      redirect_to work_url(@work), notice: "Work was successfully created."
18✔
51
    else
52
      render :new, status: :unprocessable_entity
2✔
53
    end
54
  end
55

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

65
  ##
66
  # Show the information for the dataset with the given id
67
  # When requested as .json, return the internal json resource
68
  def show
1✔
69
    @work = Work.find(params[:id])
83✔
70
    @changes =  WorkActivity.changes_for_work(@work.id)
83✔
71
    @messages = WorkActivity.messages_for_work(@work.id)
83✔
72

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

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

86
  def file_list
1✔
87
    if params[:id] == "NONE"
117✔
88
      # This is a special case when we render the file list for a work being created
89
      # (i.e. it does not have an id just yet)
90
      render json: []
18✔
91
    else
92
      @work = Work.find(params[:id])
99✔
93
      render json: @work.uploads
99✔
94
    end
95
  end
96

97
  def resolve_doi
1✔
98
    @work = Work.find_by_doi(params[:doi])
3✔
99
    redirect_to @work
2✔
100
  end
101

102
  def resolve_ark
1✔
103
    @work = Work.find_by_ark(params[:ark])
3✔
104
    redirect_to @work
2✔
105
  end
106

107
  # GET /works/1/edit
108
  def edit
1✔
109
    @work = Work.find(params[:id])
43✔
110
    if current_user && @work.editable_by?(current_user)
43✔
111
      if @work.approved? && !@work.administered_by?(current_user)
40✔
112
        Honeybadger.notify("Can not edit work: #{@work.id} is approved but #{current_user} is not admin")
1✔
113
        redirect_to root_path, notice: I18n.t("works.uneditable.approved")
1✔
114
      else
115
        @uploads = @work.uploads
39✔
116
        @wizard_mode = wizard_mode?
39✔
117
        render "edit"
39✔
118
      end
119
    else
120
      Honeybadger.notify("Can not edit work: #{@work.id} is not editable by #{current_user}")
3✔
121
      redirect_to root_path, notice: I18n.t("works.uneditable.privs")
3✔
122
    end
123
  end
124

125
  def update
1✔
126
    @work = Work.find(params[:id])
47✔
127
    if current_user.blank? || !@work.editable_by?(current_user)
47✔
128
      Honeybadger.notify("Can not update work: #{@work.id} is not editable by #{current_user}")
1✔
129
      redirect_to root_path, notice: I18n.t("works.uneditable.privs")
1✔
130
    elsif !@work.editable_in_current_state?(current_user)
46✔
131
      Honeybadger.notify("Can not update work: #{@work.id} is not editable in current state by #{current_user}")
1✔
132
      redirect_to root_path, notice: I18n.t("works.uneditable.approved")
1✔
133
    else
134
      update_work
45✔
135
    end
136
  end
137

138
  # Prompt to select how to submit their files
139
  def attachment_select
1✔
140
    @work = Work.find(params[:id])
7✔
141
    @wizard_mode = true
7✔
142
  end
143

144
  # User selected a specific way to submit their files
145
  def attachment_selected
1✔
146
    @work = Work.find(params[:id])
8✔
147
    @wizard_mode = true
8✔
148
    @work.files_location = params["attachment_type"]
8✔
149
    @work.save!
8✔
150

151
    # create a directory for the work if the curator will need to move files by hand
152
    @work.s3_query_service.create_directory if @work.files_location != "file_upload"
8✔
153

154
    next_url = case @work.files_location
8✔
155
               when "file_upload"
156
                 work_file_upload_url(@work)
2✔
157
               when "file_cluster"
158
                 work_file_cluster_url(@work)
1✔
159
               else
160
                 work_file_other_url(@work)
5✔
161
               end
162
    redirect_to next_url
8✔
163
  end
164

165
  # Allow user to upload files directly
166
  def file_upload
1✔
167
    @work = Work.find(params[:id])
3✔
168
  end
169

170
  def file_uploaded
1✔
171
    @work = Work.find(params[:id])
4✔
172
    if pre_curation_uploads_param
4✔
173
      @work.pre_curation_uploads.attach(pre_curation_uploads_param)
2✔
174
      @work.save!
1✔
175
    end
176

177
    redirect_to(work_review_path)
3✔
178
  rescue StandardError => active_storage_error
179
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
180
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
181

182
    redirect_to work_file_upload_path(@work)
1✔
183
  end
184

185
  # Allow user to indicate where their files are located in the PUL Research Cluster
186
  def file_cluster
1✔
187
    @work = Work.find(params[:id])
1✔
188
  end
189

190
  # Allow user to indicate where their files are located in the WWW
191
  def file_other
1✔
192
    @work = Work.find(params[:id])
5✔
193
  end
194

195
  def review
1✔
196
    @work = Work.find(params[:id])
6✔
197
    if request.method == "POST"
6✔
198
      @work.location_notes = params["location_notes"]
5✔
199
      @work.save!
5✔
200
    end
201
  end
202

203
  def validate
1✔
204
    @work = Work.find(params[:id])
23✔
205
    @work.submission_notes = params["submission_notes"]
23✔
206
    @uploads = @work.uploads
23✔
207
    @wizard_mode = true
23✔
208
    @work.complete_submission!(current_user)
23✔
209
    redirect_to user_url(current_user)
21✔
210
  end
211

212
  def approve
1✔
213
    @work = Work.find(params[:id])
10✔
214
    @work.approve!(current_user)
10✔
215
    redirect_to work_path(@work)
6✔
216
  end
217

218
  def withdraw
1✔
219
    @work = Work.find(params[:id])
2✔
220
    @work.withdraw!(current_user)
2✔
221
    redirect_to work_path(@work)
1✔
222
  end
223

224
  def resubmit
1✔
225
    @work = Work.find(params[:id])
2✔
226
    @work.resubmit!(current_user)
2✔
227
    redirect_to work_path(@work)
1✔
228
  end
229

230
  def assign_curator
1✔
231
    work = Work.find(params[:id])
4✔
232
    work.change_curator(params[:uid], current_user)
4✔
233
    if work.errors.count > 0
3✔
234
      render json: { errors: work.errors.map(&:type) }, status: :bad_request
1✔
235
    else
236
      render json: {}
2✔
237
    end
238
  rescue => ex
239
    Rails.logger.error("Error changing curator for work: #{work.id}. Exception: #{ex.message}")
1✔
240
    render json: { errors: ["Cannot save dataset"] }, status: :bad_request
1✔
241
  end
242

243
  def add_message
1✔
244
    work = Work.find(params[:id])
6✔
245
    if params["new-message"].present?
6✔
246
      new_message_param = params["new-message"]
6✔
247
      sanitized_new_message = html_escape(new_message_param)
6✔
248

249
      work.add_message(sanitized_new_message, current_user.id)
6✔
250
    end
251
    redirect_to work_path(id: params[:id])
6✔
252
  end
253

254
  def add_provenance_note
1✔
255
    work = Work.find(params[:id])
1✔
256
    if params["new-provenance-note"].present?
1✔
257
      new_date = params["new-provenance-date"]
1✔
258
      new_note = html_escape(params["new-provenance-note"])
1✔
259

260
      work.add_provenance_note(new_date, new_note, current_user.id)
1✔
261
    end
262
    redirect_to work_path(id: params[:id])
1✔
263
  end
264

265
  # Outputs the Datacite XML representation of the work
266
  def datacite
1✔
267
    work = Work.find(params[:id])
2✔
268
    render xml: work.to_xml
2✔
269
  end
270

271
  def datacite_validate
1✔
272
    @errors = []
1✔
273
    @work = Work.find(params[:id])
1✔
274
    datacite_xml = Nokogiri::XML(@work.to_xml)
1✔
275
    schema_location = Rails.root.join("config", "schema")
1✔
276
    Dir.chdir(schema_location) do
1✔
277
      xsd = Nokogiri::XML::Schema(File.read("datacite_4_4.xsd"))
1✔
278
      xsd.validate(datacite_xml).each do |error|
1✔
279
        @errors << error
1✔
280
      end
281
    end
282
  end
283

284
  def readme_select
1✔
285
    @work = Work.find(params[:id])
10✔
286
    @wizard = true
10✔
287
  end
288

289
  def readme_uploaded
1✔
290
    @work = Work.find(params[:id])
10✔
291
    @wizard = true
10✔
292
    readme = Readme.new(@work)
10✔
293
    readme_error = readme.attach(readme_file_param)
10✔
294
    if readme_error.nil?
10✔
295
      redirect_to work_attachment_select_url(@work)
7✔
296
    else
297
      flash[:notice] = readme_error
3✔
298
      redirect_to work_readme_select_url(@work)
3✔
299
    end
300
  end
301

302
  private
1✔
303

304
    def work_params
1✔
305
      params[:work] || params
90✔
306
    end
307

308
    def patch_params
1✔
309
      return {} unless params.key?(:patch)
30✔
310

311
      params[:patch]
18✔
312
    end
313

314
    def pre_curation_uploads_param
1✔
315
      return if patch_params.nil?
5✔
316

317
      patch_params[:pre_curation_uploads_added]
5✔
318
    end
319

320
    def readme_file_param
1✔
321
      return if patch_params.nil?
10✔
322

323
      patch_params[:readme_file]
10✔
324
    end
325

326
    def rescue_aasm_error
1✔
327
      yield
57✔
328
    rescue AASM::InvalidTransition => error
329
      message = error.message
8✔
330
      if @work.errors.count > 0
8✔
331
        message = @work.errors.to_a.join(", ")
5✔
332
      end
333
      Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
8✔
334
      @errors = ["Cannot #{@work.current_transition}: #{message}"]
8✔
335
      render error_action, status: :unprocessable_entity
8✔
336
    end
337

338
    def error_action
1✔
339
      if action_name == "create"
8✔
340
        :new
×
341
      elsif action_name == "validate"
8✔
342
        :edit
2✔
343
      else
344
        :show
6✔
345
      end
346
    end
347

348
    def wizard_mode?
1✔
349
      params[:wizard] == "true"
109✔
350
    end
351

352
    def update_work
1✔
353
      @wizard_mode = wizard_mode?
45✔
354
      upload_service = WorkUploadsEditService.new(@work, current_user)
45✔
355
      if @work.approved?
45✔
356
        upload_keys = work_params[:deleted_uploads] || []
5✔
357
        deleted_uploads = upload_service.find_post_curation_uploads(upload_keys: upload_keys)
5✔
358

359
        return head(:forbidden) unless deleted_uploads.empty?
5✔
360
      else
361
        @work = upload_service.update_precurated_file_list(added_files_param, deleted_files_param)
40✔
362
      end
363

364
      process_updates
44✔
365
    end
366

367
    def update_params
1✔
368
      {
369
        collection_id: params_collection_id,
86✔
370
        resource: FormToResourceService.convert(params, @work)
371
      }
372
    end
373

374
    def added_files_param
1✔
375
      Array(work_params[:pre_curation_uploads_added])
40✔
376
    end
377

378
    def deleted_files_param
1✔
379
      deleted_count = (work_params["deleted_files_count"] || "0").to_i
40✔
380
      (1..deleted_count).map { |i| work_params["deleted_file_#{i}"] }.select(&:present?)
45✔
381
    end
382

383
    def process_updates
1✔
384
      resource_before = @work.resource
44✔
385
      if @work.update(update_params)
44✔
386

387
        resource_compare = ResourceCompareService.new(resource_before, update_params[:resource])
42✔
388
        @work.log_changes(resource_compare, current_user.id)
42✔
389

390
        if @wizard_mode
42✔
391
          redirect_to work_readme_select_url(@work)
7✔
392
        else
393
          redirect_to work_url(@work), notice: "Work was successfully updated."
35✔
394
        end
395
      else
396
        @uploads = @work.uploads
2✔
397
        render :edit, status: :unprocessable_entity
2✔
398
      end
399
    end
400

401
    def params_collection_id
1✔
402
      # Do not allow a nil for the collection id
403
      @params_collection_id ||= begin
106✔
404
        collection_id = params[:collection_id]
62✔
405
        if collection_id.blank?
62✔
406
          collection_id = current_user.default_collection.id
4✔
407
          Honeybadger.notify("We got a nil collection as part of the parameters #{params} #{request}")
4✔
408
        end
409
        collection_id
62✔
410
      end
411
    end
412
end
413
# 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