Update custom fields of a file with REST API #1245

This commit is contained in:
karel.picman@lbcfree.net 2021-06-11 10:38:54 +02:00
parent b4c94e56c4
commit 7c45b8a752
5 changed files with 66 additions and 22 deletions

View File

@ -30,7 +30,7 @@ class DmsfFilesController < ApplicationController
before_action :authorize before_action :authorize
before_action :permissions before_action :permissions
accept_api_auth :show, :view, :delete accept_api_auth :show, :view, :delete, :create_revision
helper :custom_fields helper :custom_fields
helper :dmsf_workflows helper :dmsf_workflows
@ -100,9 +100,7 @@ class DmsfFilesController < ApplicationController
def create_revision def create_revision
if params[:dmsf_file_revision] if params[:dmsf_file_revision]
if @file.locked_for_user? unless @file.locked_for_user?
flash[:error] = l(:error_file_is_locked)
else
revision = DmsfFileRevision.new revision = DmsfFileRevision.new
revision.title = params[:dmsf_file_revision][:title] revision.title = params[:dmsf_file_revision][:title]
revision.name = params[:dmsf_file_revision][:name] revision.name = params[:dmsf_file_revision][:name]
@ -148,6 +146,7 @@ class DmsfFilesController < ApplicationController
end end
@file.name = revision.name @file.name = revision.name
ok = true
if revision.save if revision.save
revision.assign_workflow params[:dmsf_workflow_id] revision.assign_workflow params[:dmsf_workflow_id]
@ -158,43 +157,48 @@ class DmsfFilesController < ApplicationController
Rails.logger.error e.message Rails.logger.error e.message
flash[:error] = e.message flash[:error] = e.message
revision.destroy revision.destroy
redirect_back_or_default dmsf_folder_path(id: @project.id, folder_id: @folder) ok = false
return
end end
end end
if @file.locked? && !@file.locks.empty? if ok && @file.locked? && !@file.locks.empty?
begin begin
@file.unlock! @file.unlock!
flash[:notice] = "#{l(:notice_file_unlocked)}, " flash[:notice] = "#{l(:notice_file_unlocked)}, "
rescue => e rescue => e
Rails.logger.error "Cannot unlock the file: #{e.message}" Rails.logger.error "Cannot unlock the file: #{e.message}"
ok = false
end end
end end
if @file.save if ok && @file.save
@file.set_last_revision revision @file.set_last_revision revision
Redmine::Hook.call_hook :dmsf_helper_upload_after_commit, { file: @file } Redmine::Hook.call_hook :dmsf_helper_upload_after_commit, { file: @file }
flash[:notice] = (flash[:notice].nil? ? '' : flash[:notice]) + l(:notice_file_revision_created)
begin begin
recipients = DmsfMailer.deliver_files_updated(@project, [@file]) recipients = DmsfMailer.deliver_files_updated(@project, [@file])
if Setting.plugin_redmine_dmsf['dmsf_display_notified_recipients'] if Setting.plugin_redmine_dmsf['dmsf_display_notified_recipients']
if recipients.any? if recipients.any?
to = recipients.collect{ |r| r.name }.first(DMSF_MAX_NOTIFICATION_RECEIVERS_INFO).join(', ') to = recipients.collect{ |r| r.name }.first(DMSF_MAX_NOTIFICATION_RECEIVERS_INFO).join(', ')
to << ((recipients.count > DMSF_MAX_NOTIFICATION_RECEIVERS_INFO) ? ',...' : '.') to << ((recipients.count > DMSF_MAX_NOTIFICATION_RECEIVERS_INFO) ? ',...' : '.')
flash[:warning] = l(:warning_email_notifications, to: to)
end end
end end
rescue => e rescue => e
Rails.logger.error "Could not send email notifications: #{e.message}" Rails.logger.error "Could not send email notifications: #{e.message}"
end end
else
flash[:error] = @file.errors.full_messages.to_sentence
end end
else
flash[:error] = revision.errors.full_messages.to_sentence
end end
end end
end end
redirect_back_or_default dmsf_folder_path(id: @project.id, folder_id: @folder)
respond_to do |format|
format.html do
flash[:error] = l(:error_file_is_locked) if @file.locked_for_user?
flash[:warning] = l(:warning_email_notifications, to: to) if to
flash[:error] = @file.errors.full_messages.to_sentence if @file.errors.any?
flash[:error] = revision.errors.full_messages.to_sentence if revision.errors.any?
flash[:notice] = (flash[:notice].nil? ? '' : flash[:notice]) + l(:notice_file_revision_created) if ok
redirect_back_or_default dmsf_folder_path(id: @project.id, folder_id: @folder)
end
format.api
end
end end
def delete def delete

View File

@ -0,0 +1,3 @@
api.dmsf_file_revision do
api.id @file.last_revision&.id
end

View File

@ -44,10 +44,13 @@
#curl --data-binary "@cat.gif" -H "Content-Type: application/octet-stream" -X POST -u ${1}:${2} http://localhost:3000/projects/12/dmsf/upload.xml?filename=cat.gif #curl --data-binary "@cat.gif" -H "Content-Type: application/octet-stream" -X POST -u ${1}:${2} http://localhost:3000/projects/12/dmsf/upload.xml?filename=cat.gif
#curl -v -H "Content-Type: application/xml" -X POST --data "@file.xml" -u ${1}:${2} http://localhost:3000/projects/12/dmsf/commit.xml #curl -v -H "Content-Type: application/xml" -X POST --data "@file.xml" -u ${1}:${2} http://localhost:3000/projects/12/dmsf/commit.xml
# 5. List folder content & check folder existence (by folder title) # 5. Create a new revision
#curl -v -H "Content-Type: application/xml" -X POST --data "@revision.xml" -u ${1}:${2} http://localhost:3000/dmsf/files/232565/revision/create.xml
# 6. List folder content & check folder existence (by folder title)
# curl -v -H "Content-Type: application/json" -X GET -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000/projects/1/dmsf.json?folder_title=Updated%20title # curl -v -H "Content-Type: application/json" -X GET -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000/projects/1/dmsf.json?folder_title=Updated%20title
# 6. List folder content & check folder existence (by folder id) # 7. List folder content & check folder existence (by folder id)
# curl -v -H "Content-Type: application/json" -X GET -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000/projects/1/dmsf.json?folder_id=3 # curl -v -H "Content-Type: application/json" -X GET -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000/projects/1/dmsf.json?folder_id=3
# both returns 404 not found, or json with following structure: # both returns 404 not found, or json with following structure:
# { # {
@ -59,7 +62,7 @@
# } # }
#} #}
# 7. Update a folder # 8. Update a folder
# curl -v -H "Content-Type: application/json" -X POST --data "@update-folder-payload.json" -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000//projects/#{project_id}/dmsf/save.json?folder_id=#{folder_id} # curl -v -H "Content-Type: application/json" -X POST --data "@update-folder-payload.json" -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000//projects/#{project_id}/dmsf/save.json?folder_id=#{folder_id}
# update-folder-payload.json # update-folder-payload.json
@ -70,17 +73,17 @@
# }, # },
# } # }
# 8. Delete a folder # 9. Delete a folder
# a) Move to trash only # a) Move to trash only
# curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/projects/2387/dmsf/delete.xml?folder_id=#{folder_id} # curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/projects/2387/dmsf/delete.xml?folder_id=#{folder_id}
# b) Delete permanently # b) Delete permanently
# curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} "http://localhost:3000/projects/2387/dmsf/delete.xml?folder_id=#{folder_id}&commit=yes" # curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} "http://localhost:3000/projects/2387/dmsf/delete.xml?folder_id=#{folder_id}&commit=yes"
# 9. Delete a file # 10. Delete a file
# a) Move to trash only # a) Move to trash only
# curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml # curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml
# b) Delete permanently # b) Delete permanently
# curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml?commit=yes" # curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml?commit=yes"
# 10. Create a symbolic link # 11. Create a symbolic link
# curl -v -H "Content-Type: application/xml" -X POST --data "@link.xml" -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000/dmsf_links.xml # curl -v -H "Content-Type: application/xml" -X POST --data "@link.xml" -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000/dmsf_links.xml

10
extra/api/revision.xml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<dmsf_file_revision>
<title>test</title>
<name>test.sql</name>
<description>SQL script</description>
<comment>REST API</comment>
<custom_field_values>
<custom_field_value1>Validation</custom_field_value1>
</custom_field_values>
</dmsf_file_revision>

View File

@ -24,7 +24,7 @@ require File.expand_path('../../../test_helper', __FILE__)
class DmsfFileApiTest < RedmineDmsf::Test::IntegrationTest class DmsfFileApiTest < RedmineDmsf::Test::IntegrationTest
include Redmine::I18n include Redmine::I18n
fixtures :dmsf_files, :dmsf_file_revisions, :dmsf_locks fixtures :dmsf_files, :dmsf_file_revisions, :dmsf_locks, :custom_fields
def setup def setup
super super
@ -200,4 +200,28 @@ class DmsfFileApiTest < RedmineDmsf::Test::IntegrationTest
assert_equal DmsfFile::STATUS_ACTIVE, @file1.deleted assert_equal DmsfFile::STATUS_ACTIVE, @file1.deleted
end end
def test_create_revision
# curl -v -H "Content-Type: application/xml" -X POST --data "@revision.xml" -u ${1}:${2} http://localhost:3000/dmfs/files/1/revision/create.xml
payload = %{
<?xml version="1.0" encoding="utf-8" ?>
<dmsf_file_revision>
<title>#{@file1.name}</title>
<name>#{@file1.name}</name>
<description>SQL script</description>
<comment>REST API</comment>
<custom_field_values>
<custom_field_value>User documentation</custom_field_value>
</custom_field_values>
</dmsf_file_revision>
}
post "/dmsf/files/#{@file1.id}/revision/create.xml?key=#{@token.value}", params: payload,
headers: { 'CONTENT_TYPE' => 'application/xml' }
assert_response :success
# <?xml version="1.0" encoding="UTF-8"?>
# <dmsf_file_revision>
# <id>293566</id>
# </dmsf_file_revision>
assert_select 'dmsf_file_revision > id', text: @file1.last_revision.id.to_s
end
end end