Class: BulkOps::OperationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/bulk_ops/operations_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gitObject

Returns the value of attribute git.



14
15
16
# File 'app/controllers/bulk_ops/operations_controller.rb', line 14

def git
  @git
end

Instance Method Details

#applyObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'app/controllers/bulk_ops/operations_controller.rb', line 329

def apply
  unless @github_authenticated
    flash[:notice] = "Please log in to github before taking this action" 
    redirect_to action: "show", id: @operation.id
  end

  parameters = JSON.parse request.raw_post
  unless parameters['action'] == 'closed' 
    render plain: "IGNORING THIS EVENT" 
    return
  end
  if parameters['pull_request'].blank?
    render plain: "Error - the pull request was not passed from Github", status: 400 
    return
  end
  if parameters['pull_request']['merged'].to_s.downcase == "false"
    render plain: "IGNORING THIS EVENT" 
    return
  end
  verify_github_signature
  op = BulkOps::Operation.find_by(pull_id: parameters['number'])
  puts "if operation \"#{op.name}\" isn't running, it'll get applied now"
  unless ["running","complete"].include? op.stage
    op.apply!
    flash[:notice] = "Applying bulk #{op.operation_type}. Stay tuned to see how it goes!"
  end
  render plain: "OK"
end

#approveObject



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'app/controllers/bulk_ops/operations_controller.rb', line 310

def approve
  unless @github_authenticated
    flash[:notice] = "Please log in to github before taking this action" 
    redirect_to action: "show", id: @operation.id
  end

  begin
    @operation.merge_pull_request @operation.pull_id, message: params['git_message']
    @operation.delete_branch
    @operation.stage = "waiting"
    @operation.save
  rescue Octokit::MethodNotAllowed 
    flash[:error] = "For some reason, github says that it won't let us merge our change into the master branch. This is strange, since it passed our internal verification. Log in to github and check out the branch manually to see if there are any strange files or unexplained commits.}"
  rescue 
    flash[:error] = "There was a confusing error while merging our github branch into the master branch. Please log in to Github and check whether the pull request was approved."
  end
  redirect_to action: "show"
end

#blacklight_configObject



364
365
366
# File 'app/controllers/bulk_ops/operations_controller.rb', line 364

def blacklight_config
  CatalogController.blacklight_config
end

#createObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/bulk_ops/operations_controller.rb', line 90

def create
  unless @github_authenticated
    flash[:notice] = "Please log in to github before taking this action" 
    redirect_to action: "index"
  end

  params.require([:name,:type,:notified_users])

  # Create a unique operation name if the chosen name is taken
  op_name = BulkOps::Operation.unique_name(params['name'].parameterize, current_user)
  
  message = params['git_message'] || "This #{params['type']} is brand new, just created by #{current_user.email}"

  operation = BulkOps::Operation.create(name: op_name, 
                                        status: "new", 
                                        stage: "new", 
                                        operation_type: params['type'], 
                                        message: message, 
                                        user: current_user)

  operation.status = "OK"

  puts "BULK OPS CREATING BRANCH WITH OPTIONS: #{filtered_options_params.inspect}"
  puts "ALL PARAMS: #{params.inspect}"

  operation.create_branch fields: params['fields'], options: filtered_options_params, operation_type: params['type'].to_sym

  case params['type']
  when "ingest"
    operation.stage = "pending"
    operation.message = "Generated blank ingest spreadsheet and created Github branch for this ingest"
  when "update"
    operation.stage = "draft"
    operation.message = "New update draft created. Ready for admins to select which works to edit."
  end

  operation.save

  #redirect to operation show page
  flash[:notice] = "Bulk #{params['type']} created successfully"
  redirect_to action: "show", id: operation.id
end

#create_work_presenterObject

Hacky fix for bug displaying masthead. This will change anyway with next Hyrax upgrade.



21
22
23
# File 'app/controllers/bulk_ops/operations_controller.rb', line 21

def create_work_presenter
  return []
end

#csvObject



358
359
360
361
362
# File 'app/controllers/bulk_ops/operations_controller.rb', line 358

def csv
  response.headers['Content-Type'] = 'text/csv'
  response.headers['Content-Disposition'] = "attachment; filename=#{@operation.name}.csv"    
  render :text => @operation.get_spreadsheet(return_headers: true)
end

#delete_allObject



33
34
35
36
37
38
39
40
41
# File 'app/controllers/bulk_ops/operations_controller.rb', line 33

def delete_all
  unless operation.type == "ingest"
    redirect_to action: "show", id: @operation.id, error: "Can only delete all works from an ingest operation, not an #{operation.type}" 
  end
  # delete all the works
  operation.delete_all
  flash[:notice] = "All works created by this ingest have been deleted from the system. Feel free to re-apply the ingest."
  redirect_to action: "show", id: @operation.id
end

#destroyObject



286
287
288
289
290
291
292
293
294
295
# File 'app/controllers/bulk_ops/operations_controller.rb', line 286

def destroy
  unless @github_authenticated
    flash[:notice] = "Please log in to github before taking this action" 
    redirect_to action: "show", id: @operation.id
  end

  @operation.destroy!
  flash[:notice] = "Bulk #{@operation.type} deleted successfully"
  redirect_to action: "index"
end

#destroy_multipleObject



43
44
45
46
47
# File 'app/controllers/bulk_ops/operations_controller.rb', line 43

def destroy_multiple
  params['operation_ids'].each{|id| BulkOps::Operation.find(id).destroy! }
  flash[:notice] = "Bulk operations deleted successfully"
  redirect_to action: "index"
end

#duplicateObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/bulk_ops/operations_controller.rb', line 49

def duplicate 
  unless @github_authenticated
    flash[:notice] = "Please log in to github before taking this action" 
    redirect_to action: "show", id: @operation.id
  end
  case params['status_to_edit']
  when "all"
    proxies = @operation.work_proxies
  when "error", "errors","failed"
    proxies = @operation.work_proxies.where(status: "error")
    proxies += @operation.work_proxies.where(status: "errors")
  when "complete"
    proxies = @operation.work_proxies.where(status: "complete")
  else
    proxies = @operation.work_proxies
  end

  message = params['git_message'] || "This update was created from a group of works affected by a previous operation."

#      work_ids = proxies.map{|prx| prx.id}
  name_base = (params['name'] || @operation.name).parameterize
  name = BulkOps::Operation.unique_name(name_base, current_user)
  new_operation = BulkOps::Operation.create(name: name, 
                                            status: "new", 
                                            stage: "new", 
                                            operation_type: 'update', 
                                            message: message, 
                                            user: current_user)
  
  new_operation.create_branch fields: params['fields'],  options: filtered_options_params
  new_operation.status = "OK"
  new_operation.stage = "draft"
  new_operation.work_proxies = proxies
  new_operation.message = "New update draft created. Ready for admins to select which works to edit."

  new_operation.save
  
  flash[:notice] = "Successfully created a new bulk update from the works involved in the previous operation."
  redirect_to action: "show", id: new_operation.id
end

#editObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/controllers/bulk_ops/operations_controller.rb', line 195

def edit
  redirect_to action: "show", id: @operation.id unless @operation.draft?
  added = false
  destroyed = false

  if params['add_all_results'].to_s == "true"
    rows = 100
    builder = BulkOps::SearchBuilder.new(scope: self,
                                         collection: params['collection'],
                                         collection_id: params['collection_id'],
                                         admin_set: params['admin_set'],
                                         admin_set_id: params['admin_set_id'],
                                         workflow_state: params['workflow_state'],
                                         keyword_query: params['q']).rows(rows)
    result = repository.search(builder)
    total = result.total
    start = 0
    while start < total
      builder.start = start
      docs = repository.search(builder).documents
      docs.each do |doc|
        unless BulkOps::WorkProxy.find_by(operation_id: @operation.id, work_id: doc.id)
          BulkOps::WorkProxy.create(operation_id: @operation.id, 
                                    work_id: doc.id,
                                    status: "new",
                                    last_event: DateTime.now,
                                    message: params['git_message'] || "Works added to update by #{current_user.name || current_user.email}")
          added = true
        end
      end
      start += rows
    end
    
  elsif params['added_work_ids'] 
    added = false

    params['added_work_ids'].each do |work_id|
      next if work_id.blank?
      unless BulkOps::WorkProxy.find_by(operation_id: @operation.id, work_id: work_id)
        BulkOps::WorkProxy.create(operation_id: @operation.id, 
                                  work_id: work_id,
                                  status: "new",
                                  last_event: DateTime.now,
                                  message: params['git_message'] || "Works added to update by #{current_user.name || current_user.email}")
        added = true
      end
    end
  end

  if params['remove_works'] && params['remove_work_ids']
    destroyed = false
    params['remove_work_ids'].each do |work_id|
      if (proxy = BulkOps::WorkProxy.find_by(operation_id: @operation.id, work_id: work_id))
        proxy.destroy!
        destroyed = true
      end
    end
  end

  flash[:notice] = "Works removed successfully from update" if destroyed
  flash[:notice] = "Works added successfully to update" if added
  redirect_to action: "show", id: @operation.id
end

#finalize_draftObject



186
187
188
189
190
191
192
193
# File 'app/controllers/bulk_ops/operations_controller.rb', line 186

def finalize_draft
  unless @github_authenticated
    flash[:notice] = "Please log in to github before taking this action" 
    redirect_to action: "show", id: @operation.id
  end
  @operation.finalize_draft
  redirect_to action: "show"
end

#find_record_set(type, identifier) ⇒ Object



259
260
261
262
263
264
265
266
267
268
# File 'app/controllers/bulk_ops/operations_controller.rb', line 259

def find_record_set type, identifier
  return unless identifier.is_a? Integer
  type = type.capitalize.constantize
  #    begin
  record = type.find(identifier)
  #    rescue ActiveFedora::ObjectNotFoundError
  #      record = nil
  #    end
  return record
end

#indexObject



25
26
27
28
29
30
31
# File 'app/controllers/bulk_ops/operations_controller.rb', line 25

def index
  branches = BulkOps::GithubAccess.list_branch_names current_user
  BulkOps::Operation.all.each{|op| op.destroy! unless branches.include?(op.name) }
  @active_operations = BulkOps::Operation.where.not(stage: ['completed','discarded'])
  @active_operations.each {|op| op.destroy! unless (op.stage == "running") or branches.include?(op.name) }
  @old_operations = BulkOps::Operation.where(stage: ['completed','discarded']) if params["show_old_ops"]
end

#newObject



133
134
135
136
# File 'app/controllers/bulk_ops/operations_controller.rb', line 133

def new
  @default_fields = BulkOps::Operation. + ['id','collection','filename']
  @all_fields = (BulkOps::Operation. + BulkOps::SPECIAL_COLUMNS)
end

#request_applyObject



297
298
299
300
301
302
303
304
305
306
307
308
# File 'app/controllers/bulk_ops/operations_controller.rb', line 297

def request_apply
  unless @github_authenticated
    flash[:notice] = "Please log in to github before taking this action" 
    redirect_to action: "show", id: @operation.id
  end

  @operation.stage = "verifying"
  @operation.save
  BulkOps::VerificationJob.perform_later(@operation.id)
  flash[:notice] = "We are now running the data from your spreadsheet through an automatic verification process to anticipate any problems before we begin the ingest. This may take a few minutes. You should recieve an email when the process completes."
  redirect_to action: "show"
end

#searchObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/controllers/bulk_ops/operations_controller.rb', line 270

def search
  start = (params['start'] || 0).to_i
  rows = (params['rows'] || 15).to_i
  builder = BulkOps::SearchBuilder.new(scope: self,
                                       collection: params['collection'],
                                       collection_id: params['collection_id'],
                                       admin_set: params['admin_set'],
                                       admin_set_id: params['admin_set_id'],
                                       workflow_state: params['workflow_state'],
                                       keyword_query: params['q'])
  builder = builder.rows(rows).start(start)
  result = repository.search(builder)
  response.headers['Content-Type'] = 'application/json'
  render json: {num_results: result.total, results: result.documents}
end

#showObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/bulk_ops/operations_controller.rb', line 138

def show
  if @operation.running? || @operation.complete?
    @num_works = (cnt = @operation.work_proxies.count) > 0 ? cnt : 1
    @num_queued = @operation.work_proxies.where(status: 'queued').count
    @num_running = @operation.work_proxies.where(status: 'running').count
    @num_failed = @operation.work_proxies.where(status: 'failed').count
    @num_complete = @operation.work_proxies.where(status: 'complete').count
    @num_other = @operation.work_proxies.where.not(status: ['queued','running','failed','complete']).count
  end
  if @operation.stage=="draft"
    @draft_work_count = @operation.work_proxies.count
    @draft_works = @operation.work_proxies.take(10).map{|proxy| get_solr_doc(proxy.work_id)}.select{|doc| doc}
  end
end

#updateObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/bulk_ops/operations_controller.rb', line 153

def update
  unless @github_authenticated
    flash[:notice] = "Please log in to github before taking this action" 
    redirect_to action: "show", id: @operation.id
  end
  
#      params.permit(available_options).permit(ignored_columns: []).permit("edit_options")

  #if a new spreadsheet is uploaded, put it in github
  if params['spreadsheet'] && @operation.name
    @operation.update_spreadsheet params['spreadsheet'], message: params['git_message']
    flash[:notice] = "Spreadsheet updated successfully"
    redirect_to action: "show", id: @operation.id
  end

  #If new options have been defined, update them in github
  if params['edit_options']
    if filtered_options_params && @operation.name
      options = @operation.options
      filtered_options_params.each do |option_name, option_value|
        options[option_name] = option_value
      end
      BulkOps::GithubAccess.update_options(@operation.name, options, message: params['git_message'])
      flash[:notice] = "The operation options were updated successfully"
      redirect_to action: "show", id: @operation.id
    else
      redirect_to action: "show", id: @operation.id
    end
  end
  destroy if params["destroy"]
  finalize_draft if params["finalize"]
end