Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add representation formats to API

Oliver Günthere904a7e59d29
diff --git a/lib/api/decorators/offset_paginated_collection.rb b/lib/api/decorators/offset_paginated_collection.rb
index d4e50692244..b5f58fc7b9c 100644
--- a/lib/api/decorators/offset_paginated_collection.rb
+++ b/lib/api/decorators/offset_paginated_collection.rb
@@ -93,8 +93,12 @@ module API
"#{@self_link_base}?#{href_query(page, page_size)}"
end
- def href_query(page, page_size)
- @query.merge(offset: page, pageSize: page_size).to_query
+ def href_query(page = @page, page_size = @per_page)
+ query_params(page, page_size).to_query
+ end
+
+ def query_params(page = @page, page_size = @per_page)
+ @query.merge(offset: page, pageSize: page_size)
end
def paged_models(models)
diff --git a/lib/api/v3/projects/project_collection_representer.rb b/lib/api/v3/projects/project_collection_representer.rb
index b6aa01a1c04..3c90aaac6c2 100644
--- a/lib/api/v3/projects/project_collection_representer.rb
+++ b/lib/api/v3/projects/project_collection_representer.rb
@@ -36,15 +36,41 @@ require 'roar/json/hal'
module API
module V3
module Projects
- class ProjectCollectionRepresenter < ::API::Decorators::UnpaginatedCollection
+ class ProjectCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection
self.to_eager_load = ::API::V3::Projects::ProjectRepresenter.to_eager_load
self.checked_permissions = ::API::V3::Projects::ProjectRepresenter.checked_permissions
- def initialize(models, self_link:, current_user:)
+ def initialize(models, **options)
super
@represented = ::API::V3::Projects::ProjectEagerLoadingWrapper.wrap(represented)
end
+
+ links :representations do
+ [
+ representation_format_csv,
+ representation_format_xls
+ ]
+ end
+
+ def representation_format(format, mime_type:)
+ {
+ href: url_for(controller: :projects, action: :index, format: format, **query_params),
+ identifier: format,
+ type: mime_type,
+ title: I18n.t("export.format.#{format}")
+ }
+ end
+
+ def representation_format_xls
+ representation_format 'xls',
+ mime_type: 'application/vnd.ms-excel'
+ end
+
+ def representation_format_csv
+ representation_format 'csv',
+ mime_type: 'text/csv'
+ end
end
end
end
diff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rb
index 5782f347a04..4aa8b0331a0 100644
--- a/lib/api/v3/work_packages/work_package_collection_representer.rb
+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb
@@ -211,6 +211,9 @@ module API
representation_format_pdf_attachments,
representation_format_pdf_description,
representation_format_pdf_description_attachments,
+ representation_format_xls,
+ representation_format_xls_descriptions,
+ representation_format_xls_relations,
representation_format_csv
]
@@ -266,6 +269,27 @@ module API
url_query_extras: 'show_descriptions=true&show_attachments=true'
end
+ def representation_format_xls
+ representation_format 'xls',
+ mime_type: 'application/vnd.ms-excel'
+ end
+
+ def representation_format_xls_descriptions
+ representation_format 'xls-with-descriptions',
+ i18n_key: 'xls_with_descriptions',
+ mime_type: 'application/vnd.ms-excel',
+ format: 'xls',
+ url_query_extras: 'show_descriptions=true'
+ end
+
+ def representation_format_xls_relations
+ representation_format 'xls-with-relations',
+ i18n_key: 'xls_with_relations',
+ mime_type: 'application/vnd.ms-excel',
+ format: 'xls',
+ url_query_extras: 'show_relations=true'
+ end
+
def representation_format_csv
representation_format 'csv',
mime_type: 'text/csv'
diff --git a/modules/xls_export/lib/open_project/xls_export/engine.rb b/modules/xls_export/lib/open_project/xls_export/engine.rb
index d28f0414d43..105795123a7 100644
--- a/modules/xls_export/lib/open_project/xls_export/engine.rb
+++ b/modules/xls_export/lib/open_project/xls_export/engine.rb
@@ -10,12 +10,6 @@ module OpenProject::XlsExport
patches %i[CostReportsController]
- extend_api_response(:v3, :work_packages, :work_package_collection) do
- require_relative 'patches/api/v3/export_formats'
-
- prepend Patches::API::V3::ExportFormats
- end
-
initializer 'xls_export.register_hooks' do
# don't use require_dependency to not reload hooks in development mode
diff --git a/modules/xls_export/lib/open_project/xls_export/patches/api/v3/export_formats.rb b/modules/xls_export/lib/open_project/xls_export/patches/api/v3/export_formats.rb
deleted file mode 100644
index 6bb403aa6b1..00000000000
--- a/modules/xls_export/lib/open_project/xls_export/patches/api/v3/export_formats.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-module OpenProject::XlsExport::Patches
- module API::V3::ExportFormats
- def representation_formats
- super + [
- representation_format_xls,
- representation_format_xls_descriptions,
- representation_format_xls_relations
- ]
- end
-
- def representation_format_xls
- representation_format 'xls',
- mime_type: 'application/vnd.ms-excel'
- end
-
- def representation_format_xls_descriptions
- representation_format 'xls-with-descriptions',
- i18n_key: 'xls_with_descriptions',
- mime_type: 'application/vnd.ms-excel',
- format: 'xls',
- url_query_extras: 'show_descriptions=true'
- end
-
- def representation_format_xls_relations
- representation_format 'xls-with-relations',
- i18n_key: 'xls_with_relations',
- mime_type: 'application/vnd.ms-excel',
- format: 'xls',
- url_query_extras: 'show_relations=true'
- end
- end
-end