remove create_new functionality, when exercise exists on CH but is not editable

This commit is contained in:
Karol
2019-12-13 16:43:19 +01:00
parent eb7a4d5933
commit f680916385
8 changed files with 44 additions and 56 deletions

View File

@ -251,7 +251,7 @@ $(document).on('turbolinks:load', function() {
exportExerciseStart($(this).data().exerciseId); exportExerciseStart($(this).data().exerciseId);
}); });
$('body').on('click', '.export-action', function(){ $('body').on('click', '.export-action', function(){
exportExerciseConfirm($(this).data().exerciseId, $(this).data().exportType); exportExerciseConfirm($(this).data().exerciseId);
}); });
} }
@ -279,7 +279,7 @@ $(document).on('turbolinks:load', function() {
}); });
}; };
var exportExerciseConfirm = function(exerciseID, pushType) { var exportExerciseConfirm = function(exerciseID) {
var $exerciseDiv = $('#export-exercise'); var $exerciseDiv = $('#export-exercise');
var $messageDiv = $exerciseDiv.children('.export-message'); var $messageDiv = $exerciseDiv.children('.export-message');
var $actionsDiv = $exerciseDiv.children('.export-exercise-actions'); var $actionsDiv = $exerciseDiv.children('.export-exercise-actions');
@ -287,9 +287,6 @@ $(document).on('turbolinks:load', function() {
return $.ajax({ return $.ajax({
type: 'POST', type: 'POST',
url: '/exercises/' + exerciseID + '/export_external_confirm', url: '/exercises/' + exerciseID + '/export_external_confirm',
data: {
push_type: pushType
},
dataType: 'json', dataType: 'json',
success: function(response) { success: function(response) {
$messageDiv.html(response.message) $messageDiv.html(response.message)

View File

@ -129,7 +129,7 @@ class ExercisesController < ApplicationController
partial: 'export_actions', partial: 'export_actions',
locals: { locals: {
exercise: @exercise, exercise: @exercise,
# exercise_found: codeharbor_check[:exercise_found], exercise_found: codeharbor_check[:exercise_found],
update_right: codeharbor_check[:update_right], update_right: codeharbor_check[:update_right],
error: codeharbor_check[:error], error: codeharbor_check[:error],
exported: false exported: false
@ -139,11 +139,7 @@ class ExercisesController < ApplicationController
end end
def export_external_confirm def export_external_confirm
push_type = params[:push_type] @exercise.uuid = SecureRandom.uuid if @exercise.uuid.nil?
return render json: {}, status: 500 unless %w[create_new export].include? push_type
@exercise.uuid = SecureRandom.uuid if push_type == 'create_new'
error = ExerciseService::PushExternal.call( error = ExerciseService::PushExternal.call(
zip: ProformaService::ExportTask.call(exercise: @exercise), zip: ProformaService::ExportTask.call(exercise: @exercise),
@ -155,6 +151,7 @@ class ExercisesController < ApplicationController
message: t('exercises.export_codeharbor.successfully_exported', id: @exercise.id, title: @exercise.title), message: t('exercises.export_codeharbor.successfully_exported', id: @exercise.id, title: @exercise.title),
actions: render_to_string(partial: 'export_actions', locals: {exercise: @exercise, exported: true, error: error}) actions: render_to_string(partial: 'export_actions', locals: {exercise: @exercise, exported: true, error: error})
} }
@exercise.save
else else
render json: { render json: {
status: 'fail', status: 'fail',
@ -171,10 +168,10 @@ class ExercisesController < ApplicationController
uuid = params[:uuid] uuid = params[:uuid]
exercise = Exercise.find_by(uuid: uuid) exercise = Exercise.find_by(uuid: uuid)
return render json: {exercise_found: false, message: t('exercises.import_codeharbor.check.no_exercise')} if exercise.nil? return render json: {exercise_found: false} if exercise.nil?
return render json: {exercise_found: true, update_right: false, message: t('exercises.import_codeharbor.check.exercise_found_no_right')} unless ExercisePolicy.new(user, exercise).update? return render json: {exercise_found: true, update_right: false} unless ExercisePolicy.new(user, exercise).update?
render json: {exercise_found: true, update_right: true, message: t('exercises.import_codeharbor.check.exercise_found')} render json: {exercise_found: true, update_right: true}
end end
def import_exercise def import_exercise

View File

@ -14,8 +14,13 @@ module ExerciseService
req.body = {uuid: @uuid}.to_json req.body = {uuid: @uuid}.to_json
end end
response_hash = JSON.parse(response.body, symbolize_names: true) response_hash = JSON.parse(response.body, symbolize_names: true)
message = if response_hash[:exercise_found]
response_hash[:update_right] ? I18n.t('exercises.export_codeharbor.check.exercise_found') : I18n.t('exercises.export_codeharbor.check.exercise_found_no_right')
else
I18n.t('exercises.export_codeharbor.check.no_exercise')
end
{error: false}.merge(response_hash.slice(:message, :exercise_found, :update_right)) {error: false, message: message}.merge(response_hash.slice(:exercise_found, :update_right))
rescue Faraday::Error, JSON::ParserError rescue Faraday::Error, JSON::ParserError
{error: true, message: I18n.t('exercises.export_codeharbor.error')} {error: true, message: I18n.t('exercises.export_codeharbor.error')}
end end

View File

@ -4,14 +4,10 @@
= t('exercises.export_codeharbor.buttons.retry') = t('exercises.export_codeharbor.buttons.retry')
- else - else
- unless exported - unless exported
- if update_right - if !exercise_found || update_right
= button_tag type: 'button', class:'btn btn-primary pull-right export-action export-button', data: {exercise_id: exercise.id, export_type: 'export'} do = button_tag type: 'button', class:'btn btn-primary pull-right export-action export-button', data: {exercise_id: exercise.id} do
i.fa.fa-check.confirm-icon i.fa.fa-check.confirm-icon
= t('exercises.export_codeharbor.buttons.export') = t('exercises.export_codeharbor.buttons.export')
- else
= button_tag type: 'button', class:'btn btn-primary pull-right export-action export-button', data: {exercise_id: exercise.id, export_type: 'create_new'} do
i.fa.fa-check.confirm-icon
= t('exercises.export_codeharbor.buttons.create_new')
= button_tag type: 'submit', class:'btn btn-secondary pull-right export-button', data: {dismiss: 'modal'} do = button_tag type: 'submit', class:'btn btn-secondary pull-right export-button', data: {dismiss: 'modal'} do
i.fa.fa-remove.abort-icon i.fa.fa-remove.abort-icon

View File

@ -311,10 +311,6 @@ de:
editor_file_tree: editor_file_tree:
file_root: Dateien file_root: Dateien
import_codeharbor: import_codeharbor:
check:
no_exercise: Auf Codeharbor wurde keine entsprechende Aufgabe gefunden. Mit dem Export der Aufgabe wird eine neue auf Codeharbor angelegt, die mit dieser verbunden ist. Anschließend können Veränderungen an der Aufgabe von beiden Systemen aus jeweils in das andere Übertragen werden.
exercise_found: Auf Codeharbor wurde eine entsprechende Aufgabe gefunden. Mit dem Export der Aufgabe werden alle Veränderungen die auf Codeocean vorgenommen wurden, exportiert und die Aufgabe auf Codeharbor überschrieben.
exercise_found_no_right: Auf Codeharbor wurde eine entsprechende Aufgabe gefunden, Sie haben aber keine Rechte sie zu bearbeiten. Bitte wenden Sie sich an einen Admin, wenn Sie denken, dass Sie die Rechte dazu besitzen sollten.
import_errors: import_errors:
invalid: Fehlerhafte Aufgabe invalid: Fehlerhafte Aufgabe
internal_error: Beim Import der Aufgabe ist ein interner Fehler aufgetreten. internal_error: Beim Import der Aufgabe ist ein interner Fehler aufgetreten.
@ -328,9 +324,12 @@ de:
buttons: buttons:
retry: Erneut probieren retry: Erneut probieren
export: Export export: Export
create_new: Neu erstellen
close: Schließen close: Schließen
abort: Abbrechen abort: Abbrechen
check:
no_exercise: Auf Codeharbor wurde keine entsprechende Aufgabe gefunden. Mit dem Export der Aufgabe wird eine neue auf Codeharbor angelegt, die mit dieser verbunden ist. Anschließend können Veränderungen an der Aufgabe von beiden Systemen aus jeweils in das andere Übertragen werden.
exercise_found: Auf Codeharbor wurde eine entsprechende Aufgabe gefunden. Mit dem Export der Aufgabe werden alle Veränderungen, die auf Codeocean vorgenommen wurden, exportiert und die Aufgabe auf Codeharbor überschrieben.
exercise_found_no_right: Auf Codeharbor wurde eine entsprechende Aufgabe gefunden, Sie haben aber keine Rechte sie zu bearbeiten. Bitte wenden Sie sich an einen Admin, wenn Sie denken, dass Sie die Rechte dazu besitzen sollten.
file_form: file_form:
hints: hints:
feedback_message: Diese Nachricht wird als Tipp zu fehlschlagenden Tests angezeigt. feedback_message: Diese Nachricht wird als Tipp zu fehlschlagenden Tests angezeigt.

View File

@ -311,10 +311,9 @@ en:
editor_file_tree: editor_file_tree:
file_root: Files file_root: Files
import_codeharbor: import_codeharbor:
check: import_errors:
no_exercise: No corresponding exercise found on Codeharbor. Pushing this exercise will create a new exercise on Codeharbor, which will be linked to this one on Codeocean. Any changes to either one can be pushed to the respective other platform. invalid: Invalid exercise
exercise_found: 'A corresponding exercise has been found on Codeharbor. You can export the exercise to transfer all changes made on Codeocean to Codeharbor. Careful: This will overwrite all potential changes made on Codeharbor.' internal_error: An internal error occurred on Codeharbor while importing the exercise.
exercise_found_no_right: A corresponding exercise has been found on Codeharbor, but you don't have the rights to edit it. Please contact an Admin if you think you should be able to edit the exercise on Codeharbor.
export_codeharbor: export_codeharbor:
label: Export to Codeharbor label: Export to Codeharbor
dialogtitle: Export to Codeharbor dialogtitle: Export to Codeharbor
@ -325,12 +324,12 @@ en:
buttons: buttons:
retry: Retry retry: Retry
export: Export export: Export
create_new: Create new
close: Close close: Close
abort: Abort abort: Abort
export_errors: check:
invalid: Invalid exercise no_exercise: No corresponding exercise found on Codeharbor. Pushing this exercise will create a new exercise on Codeharbor, which will be linked to this one on Codeocean. Any changes to either one can be pushed to the respective other platform.
internal_error: An internal error occurred on Codeharbor while importing the exercise. exercise_found: 'A corresponding exercise has been found on Codeharbor. You can export the exercise to transfer all changes made on Codeocean to Codeharbor. Careful: This will overwrite all potential changes made on Codeharbor.'
exercise_found_no_right: A corresponding exercise has been found on Codeharbor, but you don't have the rights to edit it. Please contact an Admin if you think you should be able to edit the exercise on Codeharbor.
file_form: file_form:
hints: hints:
feedback_message: This message is used as a hint for failing tests. feedback_message: This message is used as a hint for failing tests.

View File

@ -350,7 +350,7 @@ describe ExercisesController do
include('button').and(include('Abort')).and(include('Retry')) include('button').and(include('Abort')).and(include('Retry'))
) )
expect(JSON.parse(response.body).symbolize_keys[:actions]).to( expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
not_include('Create new').and(not_include('Export')).and(not_include('Hide')) not_include('Export').and(not_include('Hide'))
) )
end end
end end
@ -362,7 +362,7 @@ describe ExercisesController do
post_request post_request
expect(JSON.parse(response.body).symbolize_keys[:message]).to eq('message') expect(JSON.parse(response.body).symbolize_keys[:message]).to eq('message')
expect(JSON.parse(response.body).symbolize_keys[:actions]).to( expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
include('button').and(include('Abort')).and(include('Create new')) include('button').and(include('Abort'))
) )
expect(JSON.parse(response.body).symbolize_keys[:actions]).to( expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
not_include('Retry').and(not_include('Export')).and(not_include('Hide')) not_include('Retry').and(not_include('Export')).and(not_include('Hide'))
@ -375,8 +375,7 @@ describe ExercisesController do
render_views render_views
let!(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) } let!(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
let(:post_request) { post :export_external_confirm, params: {push_type: push_type, id: exercise.id, codeharbor_link: codeharbor_link.id} } let(:post_request) { post :export_external_confirm, params: {id: exercise.id, codeharbor_link: codeharbor_link.id} }
let(:push_type) { 'create_new' }
let(:error) {} let(:error) {}
let(:zip) { 'zip' } let(:zip) { 'zip' }
@ -407,15 +406,6 @@ describe ExercisesController do
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(not_include('Abort')) expect(JSON.parse(response.body).symbolize_keys[:actions]).to(not_include('Abort'))
end end
end end
context 'without push_type' do
let(:push_type) {}
it 'responds with status 500' do
post_request
expect(response).to have_http_status(:internal_server_error)
end
end
end end
describe '#import_uuid_check' do describe '#import_uuid_check' do
@ -433,7 +423,6 @@ describe ExercisesController do
expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be true expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be true
expect(JSON.parse(response.body).symbolize_keys[:update_right]).to be true expect(JSON.parse(response.body).symbolize_keys[:update_right]).to be true
expect(JSON.parse(response.body).symbolize_keys[:message]).to(include('has been found').and(include('Overwrite')))
end end
context 'when api_key is incorrect' do context 'when api_key is incorrect' do
@ -445,7 +434,7 @@ describe ExercisesController do
end end
end end
context 'when the user is cannot update the exercise' do context 'when the user cannot update the exercise' do
let(:codeharbor_link) { FactoryBot.create(:codeharbor_link, api_key: 'anotherkey') } let(:codeharbor_link) { FactoryBot.create(:codeharbor_link, api_key: 'anotherkey') }
it 'renders correct response' do it 'renders correct response' do
@ -454,7 +443,6 @@ describe ExercisesController do
expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be true expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be true
expect(JSON.parse(response.body).symbolize_keys[:update_right]).to be false expect(JSON.parse(response.body).symbolize_keys[:update_right]).to be false
expect(JSON.parse(response.body).symbolize_keys[:message]).to(include('has been found').and(not_include('Overwrite')))
end end
end end
@ -466,7 +454,6 @@ describe ExercisesController do
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be false expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be false
expect(JSON.parse(response.body).symbolize_keys[:message]).to(include('No corresponding exercise'))
end end
end end
end end

View File

@ -42,17 +42,25 @@ describe ExerciseService::CheckExternal do
end end
context 'when response contains a JSON with expected keys' do context 'when response contains a JSON with expected keys' do
let(:response) { {message: 'message', exercise_found: true, update_right: true}.to_json } let(:response) { {exercise_found: true, update_right: true}.to_json }
it 'returns the correct hash' do it 'returns the correct hash' do
expect(check_external_service).to eql(error: false, message: 'message', exercise_found: true, update_right: true) expect(check_external_service).to eql(error: false, message: I18n.t('exercises.export_codeharbor.check.exercise_found'), exercise_found: true, update_right: true)
end end
context 'with different values' do context 'with exercise_found: false and no update_right' do
let(:response) { {message: 'message', exercise_found: false, update_right: false}.to_json } let(:response) { {exercise_found: false}.to_json }
it 'returns the correct hash' do it 'returns the correct hash' do
expect(check_external_service).to eql(error: false, message: 'message', exercise_found: false, update_right: false) expect(check_external_service).to eql(error: false, message: I18n.t('exercises.export_codeharbor.check.no_exercise'), exercise_found: false)
end
end
context 'with exercise_found: true and update_right: false' do
let(:response) { {exercise_found: true, update_right: false}.to_json }
it 'returns the correct hash' do
expect(check_external_service).to eql(error: false, message: I18n.t('exercises.export_codeharbor.check.exercise_found_no_right'), exercise_found: true, update_right: false)
end end
end end
end end