From 292b91ca5ee5bb96e69257dff03633693eea6a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Fri, 23 Feb 2024 09:45:57 +0100 Subject: [PATCH] 'Redmine like' error handling in macros --- config/locales/cs.yml | 2 + config/locales/de.yml | 2 + config/locales/en.yml | 2 + config/locales/es.yml | 2 + config/locales/fa.yml | 2 + config/locales/fr.yml | 2 + config/locales/hu.yml | 2 + config/locales/it.yml | 2 + config/locales/ja.yml | 2 + config/locales/ko.yml | 2 + config/locales/nl.yml | 2 + config/locales/pl.yml | 2 + config/locales/pt-BR.yml | 2 + config/locales/sl.yml | 2 + config/locales/uk.yml | 2 + config/locales/zh-TW.yml | 2 + config/locales/zh.yml | 2 + lib/redmine_dmsf/macros.rb | 78 +++++++++++-------- .../unit/lib/redmine_dmsf/dmsf_macros_test.rb | 12 +-- 19 files changed, 87 insertions(+), 37 deletions(-) diff --git a/config/locales/cs.yml b/config/locales/cs.yml index 87fcf0ab..de4db014 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -476,6 +476,8 @@ cs: notice_entries_copied: Kopírování se podařilo notice_entries_moved: Přesun se podařil label_dmsf_file_revision: DMS Dokument rev. + error_not_supported_image_format: Nepodporovaný formát obrázku + error_not_supported_video_format: Nepodporovaný formát videa easy_pages: modules: diff --git a/config/locales/de.yml b/config/locales/de.yml index 4fc08b02..bc6669ff 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -471,6 +471,8 @@ de: notice_entries_copied: Kopieren ist gelungen notice_entries_moved: Verschiebung ist gelungen label_dmsf_file_revision: DMS Dokument Rev. + error_not_supported_image_format: Nicht unterstütztes Bildformat + error_not_supported_video_format: Nicht unterstütztes Videoformat easy_pages: modules: diff --git a/config/locales/en.yml b/config/locales/en.yml index 5eab2771..b410533a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -475,6 +475,8 @@ en: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/es.yml b/config/locales/es.yml index 2f7256f2..1bdd9039 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -475,6 +475,8 @@ es: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/fa.yml b/config/locales/fa.yml index a45af73c..a62f748b 100644 --- a/config/locales/fa.yml +++ b/config/locales/fa.yml @@ -454,6 +454,8 @@ fa: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 7c282068..533c583b 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -476,6 +476,8 @@ fr: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/hu.yml b/config/locales/hu.yml index f45254c1..eebaec07 100644 --- a/config/locales/hu.yml +++ b/config/locales/hu.yml @@ -476,6 +476,8 @@ hu: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/it.yml b/config/locales/it.yml index 78f1971c..08a1d914 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -475,6 +475,8 @@ it: # Italian strings thx 2 Matteo Arceci! notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/ja.yml b/config/locales/ja.yml index e7985bed..fdbdcfb5 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -477,6 +477,8 @@ ja: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/ko.yml b/config/locales/ko.yml index f395665e..db7fc191 100644 --- a/config/locales/ko.yml +++ b/config/locales/ko.yml @@ -475,6 +475,8 @@ ko: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/nl.yml b/config/locales/nl.yml index fd419ef6..41b6647e 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -475,6 +475,8 @@ nl: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/pl.yml b/config/locales/pl.yml index dcd6c8c3..ca630664 100644 --- a/config/locales/pl.yml +++ b/config/locales/pl.yml @@ -474,6 +474,8 @@ pl: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index 842c76ee..fc3fcf9b 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -475,6 +475,8 @@ pt-BR: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/sl.yml b/config/locales/sl.yml index 4e277726..30dc44a6 100644 --- a/config/locales/sl.yml +++ b/config/locales/sl.yml @@ -476,6 +476,8 @@ sl: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/uk.yml b/config/locales/uk.yml index f9c02a3c..a6547ac1 100644 --- a/config/locales/uk.yml +++ b/config/locales/uk.yml @@ -477,6 +477,8 @@ uk: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/zh-TW.yml b/config/locales/zh-TW.yml index 73cf39c9..64fb497c 100644 --- a/config/locales/zh-TW.yml +++ b/config/locales/zh-TW.yml @@ -474,6 +474,8 @@ zh-TW: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/config/locales/zh.yml b/config/locales/zh.yml index df2a6cfe..c1d791d7 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -476,6 +476,8 @@ zh: notice_entries_copied: Copying has succeeded notice_entries_moved: Moving has succeeded label_dmsf_file_revision: DMS Document rev. + error_not_supported_image_format: Not supported image format + error_not_supported_video_format: Not supported video format easy_pages: modules: diff --git a/lib/redmine_dmsf/macros.rb b/lib/redmine_dmsf/macros.rb index 79568435..02718c07 100644 --- a/lib/redmine_dmsf/macros.rb +++ b/lib/redmine_dmsf/macros.rb @@ -29,16 +29,17 @@ module RedmineDmsf macro :dmsf do |_obj, args| raise ArgumentError if args.empty? # Requires file id - file = DmsfFile.visible.find args[0] + file = DmsfFile.visible.find_by(id: args[0]) + return "{{dmsf(#{args[0]})}}" unless file unless User.current&.allowed_to?(:view_dmsf_files, file.project, { id: file.id }) - raise l(:notice_not_authorized) + raise ::I18n.t(:notice_not_authorized) end if args[2].blank? revision = file.last_revision else revision = DmsfFileRevision.find_by(id: args[2], dmsf_file_id: args[0]) - raise ActiveRecord::RecordNotFound unless revision + return "{{dmsf(#{args[0]}, #{args[1]}, #{args[2]})}" unless revision end title = (args[1].presence || file.title) title.gsub!(/\A"|"\z/, '') # Remove apostrophes @@ -60,13 +61,14 @@ module RedmineDmsf macro :dmsff do |_obj, args| if args.empty? unless User.current.allowed_to?(:view_dmsf_folders, @project) && @project.module_enabled?(:dmsf) - raise l(:notice_not_authorized) + raise ::I18n.t(:notice_not_authorized) end - return link_to l(:link_documents), dmsf_folder_url(@project) + return link_to ::I18n.t(:link_documents), dmsf_folder_url(@project) else - folder = DmsfFolder.visible.find args[0] - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_folders, folder&.project) + folder = DmsfFolder.visible.find_by(id: args[0]) + return "{{dmsff(#{args[0]})}}" unless folder + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_folders, folder.project) title = (args[1].presence || folder.title) title.gsub!(/\A"|"\z/, '') # Remove leading and trailing apostrophe @@ -83,8 +85,9 @@ module RedmineDmsf macro :dmsfd do |_obj, args| raise ArgumentError if args.empty? # Requires file id - file = DmsfFile.visible.find args[0] - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) + file = DmsfFile.visible.find_by(id: args[0]) + return "{{dmsfd(#{args[0]})}}" unless file + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) title = (args[1].presence || file.title) title.gsub!(/\A"|"\z/, '') # Remove leading and trailing apostrophe @@ -99,8 +102,9 @@ module RedmineDmsf macro :dmsfdesc do |_obj, args| raise ArgumentError if args.empty? # Requires file id - file = DmsfFile.visible.find args[0] - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) + file = DmsfFile.visible.find_by(id: args[0]) + return "{{dmsfdesc(#{args[0]})}}" unless file + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) textilizable file.description end @@ -112,17 +116,17 @@ module RedmineDmsf macro :dmsfversion do |_obj, args| raise ArgumentError if args.empty? # Requires file id - file = DmsfFile.visible.find args[0] + file = DmsfFile.visible.find_by(id: args[0]) + return "{{dmsfversion(#{args[0]})}}" unless file unless User.current&.allowed_to?(:view_dmsf_files, file.project, { id: file.id }) - raise l(:notice_not_authorized) + raise ::I18n.t(:notice_not_authorized) end if args[1].blank? revision = file.last_revision else revision = DmsfFileRevision.find_by(id: args[1], dmsf_file_id: args[0]) - raise ActiveRecord::RecordNotFound unless revision - + return "{{dmsfversion(#{args[0]}, #{args[1]})}}" unless revision end textilizable revision.version end @@ -134,8 +138,9 @@ module RedmineDmsf macro :dmsflastupdate do |_obj, args| raise ArgumentError if args.empty? # Requires file id - file = DmsfFile.visible.find args[0] - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) + file = DmsfFile.visible.find_by(id: args[0]) + return "{{dmsflastupdate(#{args[0]})}}" unless file + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) textilizable format_time(file.last_revision.updated_at) end @@ -147,8 +152,9 @@ module RedmineDmsf macro :dmsft do |_obj, args| raise ArgumentError if args.length < 2 # Requires file id and lines number - file = DmsfFile.visible.find args[0] - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) + file = DmsfFile.visible.find_by(id: args[0]) + return "{{dmsft(#{args[0]}, #{args[1]})}}" unless file + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) content_tag :pre, file.text_preview(args[1]) end @@ -172,9 +178,13 @@ module RedmineDmsf ids = args[0].split html = [] ids.each do |id| - file = DmsfFile.visible.find(id) - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) - raise 'Not supported image format' unless file.image? + file = DmsfFile.visible.find_by(id: id) + unless file + html << "{{dmsf_image(#{args[0]})}}" + next + end + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) + raise ::I18n.t(:error_not_supported_image_format) unless file.image? member = Member.find_by(user_id: User.current.id, project_id: file.project.id) filename = file.last_revision.formatted_name(member) @@ -207,9 +217,10 @@ module RedmineDmsf size = options[:size] width = options[:width] height = options[:height] - file = DmsfFile.visible.find args[0] - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) - raise 'Not supported video format' unless file.video? + file = DmsfFile.visible.find_by(id: args[0]) + return "{{dmsf_video(#{args[0]})}}" unless file + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) + raise ::I18n.t(:error_not_supported_video_format) unless file.video? member = Member.find_by(user_id: User.current.id, project_id: file.project.id) filename = file.last_revision.formatted_name(member) @@ -228,6 +239,7 @@ module RedmineDmsf # dmsftn - link to an image thumbnail desc %{Wiki DMSF thumbnail: {{dmsftn(file_id)}} -- with default height 200 (auto width) + {{dmsftn(file_id1 file_id2 file_id3)}} -- multiple thumbnails {{dmsftn(file_id, size=300)}} -- with size 300x300 {{dmsftn(file_id, height=300)}} -- with height (auto width) {{dmsftn(file_id, width=300)}} -- with width (auto height) @@ -242,9 +254,13 @@ module RedmineDmsf ids = args[0].split html = [] ids.each do |id| - file = DmsfFile.visible.find(id) - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) - raise 'Not supported image format' unless file.image? + file = DmsfFile.visible.find_by(id: id) + unless file + html << "{{dmsftn(#{id})}}" + next + end + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) + raise ::I18n.t(:error_not_supported_image_format) unless file.image? member = Member.find_by(user_id: User.current.id, project_id: file.project.id) filename = file.last_revision.formatted_name(member) @@ -274,9 +290,9 @@ module RedmineDmsf macro :dmsfw do |_obj, args| raise ArgumentError if args.empty? # Requires file id - file = DmsfFile.visible.find args[0] - raise l(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) - raise ActiveRecord::RecordNotFound unless file.last_revision + file = DmsfFile.visible.find_by(id: args[0]) + return "{{dmsfw(#{args[0]})}}" unless file + raise ::I18n.t(:notice_not_authorized) unless User.current&.allowed_to?(:view_dmsf_files, file.project) file.last_revision.workflow_str(false) end diff --git a/test/unit/lib/redmine_dmsf/dmsf_macros_test.rb b/test/unit/lib/redmine_dmsf/dmsf_macros_test.rb index 48e0c04e..b4cdc299 100644 --- a/test/unit/lib/redmine_dmsf/dmsf_macros_test.rb +++ b/test/unit/lib/redmine_dmsf/dmsf_macros_test.rb @@ -50,7 +50,7 @@ class DmsfMacrosTest < RedmineDmsf::Test::HelperTest def test_macro_dmsf_file_not_found text = textilizable('{{dmsf(99)}}') - assert text.include?('Error') + assert text.include?('{{dmsf(99)}}'), text end def test_macro_dmsf_no_permissions @@ -283,7 +283,7 @@ class DmsfMacrosTest < RedmineDmsf::Test::HelperTest def test_macro_dmsf_image_not_image text = textilizable("{{dmsf_image(#{@file1.id})}}") - assert text.include?('Not supported image format') + assert text.include?(::I18n.t(:error_not_supported_image_format)) end # {{dmsf_video(file_id)}} @@ -336,7 +336,7 @@ class DmsfMacrosTest < RedmineDmsf::Test::HelperTest def test_macro_dmsf_video_not_video text = textilizable("{{dmsf_video(#{@file7.id})}}") - assert text.include?('Not supported video format'), text + assert text.include?(::I18n.t(:error_not_supported_video_format)), text end # {{dmsftn(file_id)}} @@ -429,7 +429,7 @@ class DmsfMacrosTest < RedmineDmsf::Test::HelperTest def test_macro_dmsftn_not_image text = textilizable("{{dmsftn(#{@file1.id})}}") - assert text.include?('Not supported image format') + assert text.include?(::I18n.t(:error_not_supported_image_format)) end # {{dmsfw(file_id)}} @@ -441,12 +441,12 @@ class DmsfMacrosTest < RedmineDmsf::Test::HelperTest def test_macro_dmsfw_no_permissions @manager_role.remove_permission! :view_dmsf_files text = textilizable("{{dmsfw(#{@file1.id})}}") - assert text.include?('Error'), text + assert text.include?(::I18n.t(:notice_not_authorized)) end def test_macro_dmsfw_dmsf_off @project1.disable_module! :dmsf text = textilizable("{{dmsfw(#{@file1.id})}}") - assert text.include?('Error'), text + assert text.include?(::I18n.t(:notice_not_authorized)) end end