Document uploads should be owned by user

This commit is contained in:
Michael Shamoon 2022-12-12 13:24:59 -08:00
parent 8159b7574c
commit 446842ecfc
5 changed files with 31 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import {
} from './consumer-status.service' } from './consumer-status.service'
import { DocumentService } from './rest/document.service' import { DocumentService } from './rest/document.service'
import { Subscription } from 'rxjs' import { Subscription } from 'rxjs'
import { SettingsService } from './settings.service'
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -16,7 +17,8 @@ export class UploadDocumentsService {
constructor( constructor(
private documentService: DocumentService, private documentService: DocumentService,
private consumerStatusService: ConsumerStatusService private consumerStatusService: ConsumerStatusService,
private settings: SettingsService
) {} ) {}
uploadFiles(files: NgxFileDropEntry[]) { uploadFiles(files: NgxFileDropEntry[]) {
@ -26,6 +28,7 @@ export class UploadDocumentsService {
fileEntry.file((file: File) => { fileEntry.file((file: File) => {
let formData = new FormData() let formData = new FormData()
formData.append('document', file, file.name) formData.append('document', file, file.name)
formData.set('owner', this.settings.currentUser.id.toString())
let status = this.consumerStatusService.newFileUpload(file.name) let status = this.consumerStatusService.newFileUpload(file.name)
status.message = $localize`Connecting...` status.message = $localize`Connecting...`

View File

@ -11,6 +11,7 @@ import magic
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User
from django.db import transaction from django.db import transaction
from django.db.models import Q from django.db.models import Q
from django.utils import timezone from django.utils import timezone
@ -99,6 +100,7 @@ class Consumer(LoggingMixin):
self.override_tag_ids = None self.override_tag_ids = None
self.override_document_type_id = None self.override_document_type_id = None
self.task_id = None self.task_id = None
self.owner_id = None
self.channel_layer = get_channel_layer() self.channel_layer = get_channel_layer()
@ -255,6 +257,7 @@ class Consumer(LoggingMixin):
override_tag_ids=None, override_tag_ids=None,
task_id=None, task_id=None,
override_created=None, override_created=None,
override_owner_id=None,
) -> Document: ) -> Document:
""" """
Return the document object if it was successfully created. Return the document object if it was successfully created.
@ -268,6 +271,7 @@ class Consumer(LoggingMixin):
self.override_tag_ids = override_tag_ids self.override_tag_ids = override_tag_ids
self.task_id = task_id or str(uuid.uuid4()) self.task_id = task_id or str(uuid.uuid4())
self.override_created = override_created self.override_created = override_created
self.override_owner_id = override_owner_id
self._send_progress(0, 100, "STARTING", MESSAGE_NEW_FILE) self._send_progress(0, 100, "STARTING", MESSAGE_NEW_FILE)
@ -526,6 +530,11 @@ class Consumer(LoggingMixin):
for tag_id in self.override_tag_ids: for tag_id in self.override_tag_ids:
document.tags.add(Tag.objects.get(pk=tag_id)) document.tags.add(Tag.objects.get(pk=tag_id))
if self.override_owner_id:
document.owner = User.objects.get(
pk=self.override_owner_id,
)
def _write(self, storage_type, source, target): def _write(self, storage_type, source, target):
with open(source, "rb") as read_file: with open(source, "rb") as read_file:
with open(target, "wb") as write_file: with open(target, "wb") as write_file:

View File

@ -681,6 +681,14 @@ class PostDocumentSerializer(serializers.Serializer):
required=False, required=False,
) )
owner = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(),
label="Owner",
allow_null=True,
write_only=True,
required=False,
)
def validate_document(self, document): def validate_document(self, document):
document_data = document.file.read() document_data = document.file.read()
mime_type = magic.from_buffer(document_data, mime=True) mime_type = magic.from_buffer(document_data, mime=True)
@ -710,6 +718,12 @@ class PostDocumentSerializer(serializers.Serializer):
else: else:
return None return None
def validate_owner(self, owner):
if owner:
return owner.id
else:
return None
class BulkDownloadSerializer(DocumentListSerializer): class BulkDownloadSerializer(DocumentListSerializer):

View File

@ -96,6 +96,7 @@ def consume_file(
override_tag_ids=None, override_tag_ids=None,
task_id=None, task_id=None,
override_created=None, override_created=None,
override_owner_id=None,
): ):
path = Path(path).resolve() path = Path(path).resolve()
@ -184,6 +185,7 @@ def consume_file(
override_tag_ids=override_tag_ids, override_tag_ids=override_tag_ids,
task_id=task_id, task_id=task_id,
override_created=override_created, override_created=override_created,
override_owner_id=override_owner_id,
) )
if document: if document:

View File

@ -637,6 +637,7 @@ class PostDocumentView(GenericAPIView):
tag_ids = serializer.validated_data.get("tags") tag_ids = serializer.validated_data.get("tags")
title = serializer.validated_data.get("title") title = serializer.validated_data.get("title")
created = serializer.validated_data.get("created") created = serializer.validated_data.get("created")
owner_id = serializer.validated_data.get("owner")
t = int(mktime(datetime.now().timetuple())) t = int(mktime(datetime.now().timetuple()))
@ -662,6 +663,7 @@ class PostDocumentView(GenericAPIView):
override_tag_ids=tag_ids, override_tag_ids=tag_ids,
task_id=task_id, task_id=task_id,
override_created=created, override_created=created,
override_owner_id=owner_id,
) )
return Response("OK") return Response("OK")