Handle opening a version should rediect to head

This commit is contained in:
shamoon
2026-02-10 13:20:45 -08:00
parent 667e4b81eb
commit a5c211cc0f
3 changed files with 48 additions and 1 deletions

View File

@@ -437,6 +437,7 @@ export class DocumentDetailComponent
} }
private loadDocument(documentId: number): void { private loadDocument(documentId: number): void {
let redirectedToHead = false
this.selectedVersionId = documentId this.selectedVersionId = documentId
this.previewUrl = this.documentsService.getPreviewUrl( this.previewUrl = this.documentsService.getPreviewUrl(
this.selectedVersionId this.selectedVersionId
@@ -459,7 +460,24 @@ export class DocumentDetailComponent
this.documentsService this.documentsService
.get(documentId) .get(documentId)
.pipe( .pipe(
catchError(() => { catchError((error) => {
if (error?.status === 404) {
return this.documentsService.getHeadId(documentId).pipe(
map((result) => {
const headId = result?.head_id
if (headId && headId !== documentId) {
const section =
this.route.snapshot.paramMap.get('section') || 'details'
redirectedToHead = true
this.router.navigate(['documents', headId, section], {
replaceUrl: true,
})
}
return null
}),
catchError(() => of(null))
)
}
// 404 is handled in the subscribe below // 404 is handled in the subscribe below
return of(null) return of(null)
}), }),
@@ -470,6 +488,9 @@ export class DocumentDetailComponent
.subscribe({ .subscribe({
next: (doc) => { next: (doc) => {
if (!doc) { if (!doc) {
if (redirectedToHead) {
return
}
this.router.navigate(['404'], { replaceUrl: true }) this.router.navigate(['404'], { replaceUrl: true })
return return
} }

View File

@@ -211,6 +211,12 @@ export class DocumentService extends AbstractPaperlessService<Document> {
}) })
} }
getHeadId(documentId: number) {
return this.http.get<{ head_id: number }>(
this.getResourceUrl(documentId, 'head')
)
}
deleteVersion(headDocumentId: number, versionId: number) { deleteVersion(headDocumentId: number, versionId: number) {
return this.http.delete<{ result: string; current_version_id: number }>( return this.http.delete<{ result: string; current_version_id: number }>(
this.getResourceUrl(headDocumentId, `versions/${versionId}`) this.getResourceUrl(headDocumentId, `versions/${versionId}`)

View File

@@ -805,6 +805,26 @@ class DocumentViewSet(
) )
return super().get_serializer(*args, **kwargs) return super().get_serializer(*args, **kwargs)
@action(methods=["get"], detail=True, url_path="head")
def head(self, request, pk=None):
try:
doc = Document.global_objects.select_related(
"owner",
"head_version",
).get(pk=pk)
except Document.DoesNotExist:
raise Http404
head_doc = doc if doc.head_version_id is None else doc.head_version
if request.user is not None and not has_perms_owner_aware(
request.user,
"view_document",
head_doc,
):
return HttpResponseForbidden("Insufficient permissions")
return Response({"head_id": head_doc.id})
def update(self, request, *args, **kwargs): def update(self, request, *args, **kwargs):
response = super().update(request, *args, **kwargs) response = super().update(request, *args, **kwargs)
from documents import index from documents import index