Skip to content

Commit

Permalink
Merge pull request #139 from ontoportal-lirmm/feature/add-edit-delete…
Browse files Browse the repository at this point in the history
…-submissions-for-users

Feature: Add edit and  delete submissions for users
  • Loading branch information
syphax-bouazzouni authored Jan 1, 2023
2 parents 982adc6 + 6e8ea3c commit 864d0a3
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 21 deletions.
8 changes: 4 additions & 4 deletions app/components/concept_details_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def initialize(id:, acronym:, properties:, top_keys:, bottom_keys:, exclude_keys
@exclude_keys = exclude_keys
@id = id

@concept_properties = concept_properties2hash(@properties)
@concept_properties = concept_properties2hash(@properties) if @properties
end

def render_properties(properties_set, ontology_acronym, &block)
out = ''
properties_set.each do |key, data|
properties_set&.each do |key, data|
next if exclude_relation?(key) || !data[:values]

values = data[:values]
Expand Down Expand Up @@ -49,14 +49,14 @@ def render_properties(properties_set, ontology_acronym, &block)
end

def properties_set_by_keys(keys, concept_properties, exclude_keys = [])
concept_properties.select do |k, v|
concept_properties&.select do |k, v|
(keys.include?(k) || !keys.select { |key| v[:key].to_s.include?(key) }.empty?) && !exclude_keys.include?(k) &&
exclude_keys.select { |key| v[:key].to_s.include?(key) }.empty?
end
end

def filter_properties(top_keys, bottom_keys, exclude_keys, concept_properties)
all_keys = concept_properties.keys
all_keys = concept_properties&.keys || []
top_set = properties_set_by_keys(top_keys, concept_properties, exclude_keys)
bottom_set = properties_set_by_keys(bottom_keys, concept_properties, exclude_keys)
leftover = properties_set_by_keys(all_keys - top_keys - bottom_keys, concept_properties, exclude_keys)
Expand Down
24 changes: 17 additions & 7 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class AdminController < ApplicationController
include TurboHelper
layout :determine_layout
before_action :cache_setup

Expand Down Expand Up @@ -180,21 +181,19 @@ def delete_ontologies
end

def delete_submission
response = {errors: '', success: ''}

response = { errors: '', success: '' }
submission_id = params["id"]
begin
ont = params["acronym"]
ontology = LinkedData::Client::Models::Ontology.find_by_acronym(ont).first

if ontology
submissions = ontology.explore.submissions
submission = submissions.select {|o| o.submissionId == params["id"].to_i}.first
submission = ontology.explore.submissions({ display: 'submissionId' }, submission_id)

if submission
error_response = submission.delete

if response_error?(error_response)
errors = response_errors(error_response) # see application_controller::response_errors
errors = response_errors(error_response)
_process_errors(errors, response, true)
else
response[:success] << "Submission #{params["id"]} for ontology #{ont} was deleted successfully"
Expand All @@ -208,7 +207,18 @@ def delete_submission
rescue Exception => e
response[:errors] << "Problem deleting submission #{params["id"]} for ontology #{ont} - #{e.class}: #{e.message}"
end
render :json => response

if params[:turbo_stream]
if response[:errors].empty?
render_turbo_stream( alert_success { response[:success] }, remove('submission_' + submission_id.to_s))

else
render_turbo_stream alert_error { response[:errors] }
end
else
render :json => response
end

end

def users
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ def check_ftp_file(uri)
file_exists
end

def parse_response_body(response)
return nil if response.nil?

OpenStruct.new(JSON.parse(response.body, symbolize_names: true))
end

def response_errors(error_struct)
error_struct = parse_response_body(error_struct)
errors = {error: "There was an error, please try again"}
return errors unless error_struct
return errors unless error_struct.respond_to?(:errors)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ontologies_metadata_curator_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def update

errors = nil
if error_responses.compact.any? { |x| x.status != 204 }
errors = error_responses.map { |error_response| response_errors(OpenStruct.new(JSON.parse(error_response.body, symbolize_names: true))) }
errors = error_responses.map { |error_response| response_errors(error_response)) }
end

respond_to do |format|
Expand Down
7 changes: 4 additions & 3 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ def create

# Called when form to "Edit submission" is submitted
def edit
display_submission_attributes params[:ontology_id], params[:properties]&.split(','), required: params[:required]&.eql?('true'),
display_submission_attributes params[:ontology_id], params[:properties]&.split(','), submissionId: params[:id],
required: params[:required]&.eql?('true'),
show_sections: params[:show_sections].nil? || params[:show_sections].eql?('true'),
inline_save: params[:inline_save]&.eql?('true')
end

# When editing a submission (called when submit "Edit submission information" form)
def update
error_responses = []
_, submission_params = params[:submission].each.first
_, submission_params = params[:submission].each.first

error_responses << update_submission(submission_params)

if error_responses.compact.any? { |x| x.status != 204 }
@errors = error_responses.map { |error_response| response_errors(OpenStruct.new(JSON.parse(error_response.body, symbolize_names: true))) }
@errors = error_responses.map { |error_response| response_errors(error_response) }
end

if params[:attribute]
Expand Down
4 changes: 3 additions & 1 deletion app/helpers/schemes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ def get_scheme(ontology, scheme_uri)
end

def get_scheme_label(scheme)
return '' if scheme.nil?

if scheme['prefLabel'].nil? || scheme['prefLabel'].empty?
extract_label_from(scheme['@id']).html_safe
extract_label_from(scheme['@id']).html_safe if scheme['@id']
else
scheme['prefLabel']
end
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/turbo_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def prepend(id, options = {}, &block)
def replace(id, options = {}, &block)
turbo_stream.replace(id, options, &block)
end

def remove(id)
turbo_stream.remove(id)
end
def render_turbo_stream(*streams)
render turbo_stream: streams
end
Expand Down
9 changes: 9 additions & 0 deletions app/javascript/application_esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ import "./controllers"
import "./component_controllers"


Turbo.setConfirmMethod((message) => {
return new Promise((resolve, reject) => {
alertify.confirm(message, (e) => {
resolve(e)
})
})
})


1 change: 1 addition & 0 deletions app/javascript/controllers/turbo_frame_error_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class extends Turbo_frame_controller {
showError(event) {
let response = event.detail.fetchResponse
if (!response.succeeded) {
event.preventDefault()
response = response.response.clone()
response.text().then(text => {
this.#hideContent()
Expand Down
23 changes: 18 additions & 5 deletions app/views/ontologies/_submissions.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@



- more_colspan = 4
- more_colspan = 3 if @ont_restricted
- more_colspan = 6
- more_colspan = 5 if @ont_restricted

%div.click_versions_collapse
= render_alerts_container(AdminController)
%table#ontology_versions.table.table-sm.table-striped
%thead
%tr
Expand All @@ -33,14 +34,17 @@
= generate_attribute_text("creationDate", "Uploaded")
- unless @ont_restricted
%th.align-middle Downloads
- if @ontology.admin?(session[:user])
%th.align-middle Actions

- begin
- submission_ready = @ontology.explore.latest_submission({:include_status => 'ready'})
- submission_ready = @ontology.explore.latest_submission({:include_status => 'ready', display: 'submissionId'})
- submission_readyId = submission_ready.submissionId unless submission_ready.nil?
- rescue
- rescue
- submission_readyId = -1
- @submissions.each_with_index do |sub, index|
- hidden_row_class = index >= 5 ? "hidden_ont hidden_select" : ""
%tr{class: "#{hidden_row_class}"}
%tr{class: "#{hidden_row_class}", id: "submission_#{sub.submissionId}"}
%td
= raw status_link(sub, sub.submissionId==submission_readyId)
%td
Expand All @@ -52,6 +56,15 @@
- unless @ont_restricted
%td
= raw download_link(sub, @ontology)
- if @ontology.admin?(session[:user])
%td
%div.d-flex
%a.btn.btn-sm.btn-link{:href => "#{@ontology.acronym}/submissions/#{sub.submissionId}/edit"}
%span Edit
- unless index.zero?
- alert_text = "Are you sure you want to delete submission <span style='color:red;font-weight:bold;'>" + sub.submissionId.to_s + "</span> for ontology <span style='color:red;font-weight:bold;'>" + @ontology.acronym + "</span>?<br/><b>This action CAN NOT be undone!!!</b>"
= button_to "Delete", "/admin/ontologies/#{@ontology.acronym}/submissions/#{sub.submissionId}?turbo_stream=true", method: :delete, class:'btn btn-sm btn-link', form: {data: { turbo: true, turbo_confirm: alert_text, turbo_frame: '_top'}}

- if @submissions.length >= 5
%tr
%td{colspan: more_colspan, class: "show_more_subs"}
Expand Down

0 comments on commit 864d0a3

Please sign in to comment.