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

Feature/wp export status (#8289)

ulfertsfa0669c36784
diff --git a/app/controllers/work_packages/exports_controller.rb b/app/controllers/work_packages/exports_controller.rb
index cf0ca3c2622..3102b35e0d3 100644
--- a/app/controllers/work_packages/exports_controller.rb
+++ b/app/controllers/work_packages/exports_controller.rb
@@ -35,13 +35,24 @@ class WorkPackages::ExportsController < ApplicationController
:authorize_current_user
def show
- if @export.attachments.first
+ if @export.ready?
redirect_to attachment_content_path
else
+ headers['Link'] = "<#{status_work_packages_export_path(@export.id)}> rel=\"status\""
head 202
end
end
+ def status
+ if @export.ready?
+ headers['Link'] = "<#{attachment_content_path}> rel=\"download\""
+
+ render plain: 'Completed'
+ else
+ render plain: 'Processing'
+ end
+ end
+
private
def authorize_current_user
diff --git a/app/models/work_packages/export.rb b/app/models/work_packages/export.rb
index 52be14492c7..14472974ff0 100644
--- a/app/models/work_packages/export.rb
+++ b/app/models/work_packages/export.rb
@@ -11,4 +11,8 @@ class WorkPackages::Export < ApplicationRecord
def visible?(user)
user_id == user.id
end
+
+ def ready?
+ attachments.any?
+ end
end
diff --git a/config/routes.rb b/config/routes.rb
index 734b9967283..cda42eac4ce 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -409,7 +409,9 @@ OpenProject::Application.routes.draw do
namespace :work_packages do
match 'auto_complete' => 'auto_completes#index', via: %i[get post]
- resources :exports, only: [:show]
+ resources :exports, only: [:show] do
+ get 'status', action: :status, on: :member
+ end
resources :calendar, controller: 'calendars', only: [:index]
resource :bulk, controller: 'bulk', only: %i[edit update destroy]
# FIXME: this is kind of evil!! We need to remove this soonest and
diff --git a/frontend/src/app/components/modals/export-modal/wp-table-export.modal.ts b/frontend/src/app/components/modals/export-modal/wp-table-export.modal.ts
index c196f94ea99..b746200f45e 100644
--- a/frontend/src/app/components/modals/export-modal/wp-table-export.modal.ts
+++ b/frontend/src/app/components/modals/export-modal/wp-table-export.modal.ts
@@ -57,6 +57,7 @@ export class WpTableExportModal extends OpModalComponent implements OnInit, OnDe
public downloadHref:string;
public isLoading = false;
+ private subscription?:Subscription;
private finished?:Function;
@ViewChild('downloadLink') downloadLink:ElementRef;
@@ -87,6 +88,7 @@ export class WpTableExportModal extends OpModalComponent implements OnInit, OnDe
ngOnDestroy() {
super.ngOnDestroy();
+ this.safeUnsubscribe();
}
private buildExportOptions(results:WorkPackageCollectionResource) {
@@ -116,8 +118,8 @@ export class WpTableExportModal extends OpModalComponent implements OnInit, OnDe
this.download(data.url!);
}
- if (data.status === 202) {
- this.pollUntilDownload(data.url!);
+ if (data.status === 202 && this.linkHeaderUrl(data)) {
+ this.pollUntilDownload(this.linkHeaderUrl(data)!);
}
},
(error:HttpErrorResponse) => this.handleError(error));
@@ -126,20 +128,19 @@ export class WpTableExportModal extends OpModalComponent implements OnInit, OnDe
private pollUntilDownload(url:string) {
this.isLoading = true;
- interval(1000)
- .pipe(
- switchMap(() => this.performRequest(url)),
- takeWhile(response => response.status === 202, true),
- withDelayedLoadingIndicator(this.loadingIndicator.getter('modal')),
- ).subscribe(
- response => {
- if (response.status === 200) {
- this.download(response.url!);
- }
- },
- error => this.handleError(error),
- () => this.isLoading = false
- );
+ this.subscription = interval(1000)
+ .pipe(
+ switchMap(() => this.performRequest(url)),
+ takeWhile(response => response.status === 200 && !this.linkHeaderUrl(response), true),
+ withDelayedLoadingIndicator(this.loadingIndicator.getter('modal')),
+ ).subscribe(response => {
+ if (response.status === 200 && this.linkHeaderUrl(response)) {
+ this.download(this.linkHeaderUrl(response)!);
+ }
+ },
+ error => this.handleError(error),
+ () => this.isLoading = false
+ );
}
private performRequest(url:string):Observable<HttpResponse<any>> {
@@ -182,4 +183,26 @@ export class WpTableExportModal extends OpModalComponent implements OnInit, OnDe
this.service.close();
});
}
+
+ private linkHeaderUrl(data:HttpResponse<any>) {
+ let link = data.headers.get('link');
+
+ if (!link) {
+ return null;
+ }
+
+ let match = link.match(/<([^>]+)>/);
+
+ if (!match) {
+ return null;
+ } else {
+ return match[1];
+ }
+ }
+
+ private safeUnsubscribe() {
+ if (this.subscription) {
+ this.subscription.unsubscribe();
+ }
+ }
}
diff --git a/spec/controllers/work_packages/exports_controller_spec.rb b/spec/controllers/work_packages/exports_controller_spec.rb
index e93c439de4b..718b681ef68 100644
--- a/spec/controllers/work_packages/exports_controller_spec.rb
+++ b/spec/controllers/work_packages/exports_controller_spec.rb
@@ -64,8 +64,6 @@ describe WorkPackages::ExportsController, type: :controller do
let(:attachment_done) { true }
describe 'show' do
- let(:query) { FactoryBot.build_stubbed(:query) }
-
context 'with an existing id' do
before do
get 'show', params: { id: export.id }
@@ -85,6 +83,75 @@ describe WorkPackages::ExportsController, type: :controller do
expect(response.status)
.to eql 202
end
+
+ it 'returns the status location via the link header' do
+ expect(response.headers['Link'])
+ .to eql "</work_packages/exports/#{export.id}/status> rel=\"status\""
+ end
+ end
+
+ context 'with the export belonging to a different user' do
+ let(:current_user) { FactoryBot.build_stubbed(:user) }
+
+ it 'returns 404 NOT FOUND' do
+ expect(response.status)
+ .to eql 404
+ end
+ end
+ end
+
+ context 'with an inexisting id' do
+ before do
+ get 'show', params: { id: export.id + 5 }
+ end
+
+ it 'returns 404 NOT FOUND' do
+ expect(response.status)
+ .to eql 404
+ end
+ end
+ end
+
+ describe 'status' do
+ context 'with an existing id' do
+ before do
+ get 'status', params: { id: export.id }
+ end
+
+ context 'with the attachment being ready' do
+ it 'returns 200 OK' do
+ expect(response.status)
+ .to eql 200
+ end
+
+ it 'links to the download location' do
+ expect(response.headers['Link'])
+ .to eql "<#{api_v3_paths.attachment_content(attachment.id)}> rel=\"download\""
+ end
+
+ it 'reads "Completed"' do
+ expect(response.body)
+ .to eql 'Completed'
+ end
+ end
+
+ context 'with the attachment not being ready' do
+ let(:attachment_done) { false }
+
+ it 'returns 200 OK' do
+ expect(response.status)
+ .to eql 200
+ end
+
+ it 'has no link to the download' do
+ expect(response.headers['Link'])
+ .to be nil
+ end
+
+ it 'reads "Processing"' do
+ expect(response.body)
+ .to eql 'Processing'
+ end
end
context 'with the export belonging to a different user' do
diff --git a/spec/routing/work_packages/exports_spec.rb b/spec/routing/work_packages/exports_spec.rb
index 4ca82ab7a80..5db02d73d0c 100644
--- a/spec/routing/work_packages/exports_spec.rb
+++ b/spec/routing/work_packages/exports_spec.rb
@@ -32,4 +32,8 @@ describe 'work_package exports routes', type: :routing do
it '/work_packages/exports/:id GET routes to work_packages/exports#show' do
expect(get('/work_packages/exports/5')).to route_to('work_packages/exports#show', id: "5")
end
+
+ it '/work_packages/exports/:id/status GET routes to work_packages/exports#status' do
+ expect(get('/work_packages/exports/5/status')).to route_to('work_packages/exports#status', id: "5")
+ end
end