From 3c992930d4b8d184add30cec415f01049f8b7a5a Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Mon, 15 Apr 2024 03:56:29 +0200 Subject: [PATCH] add DocumentPrivate to allow user metadata embeddding into PDFs --- app/settings.py | 1 + teleforma/models/core.py | 40 ++++++++++++++++++++++++++++++++++++---- teleforma/views/core.py | 19 ++++++++++++++----- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/app/settings.py b/app/settings.py index eb8a3a0e..92d3eef6 100644 --- a/app/settings.py +++ b/app/settings.py @@ -265,6 +265,7 @@ TELEFORMA_PERIOD_DEFAULT_ID = 34 TELEFORMA_EXAM_MAX_SESSIONS = 99 TELEFORMA_EXAM_SCRIPT_MAX_SIZE = 31457280 TELEFORMA_EXAM_SCRIPT_SERVICE_URL = '/webviewer/teleforma.html' +TELEFORMA_PRIVATE_DOCUMENTS_MODE = True EMAIL_HOST = 'angus.parisson.com' DEFAULT_FROM_EMAIL = 'crfpa@pre-barreau.com' diff --git a/teleforma/models/core.py b/teleforma/models/core.py index 48923803..6fab21fe 100644 --- a/teleforma/models/core.py +++ b/teleforma/models/core.py @@ -801,6 +801,13 @@ class Document(MediaBase): readers = models.ManyToManyField(User, related_name="document", verbose_name=_('readers'), blank=True) + class Meta(MetaCore): + db_table = app_label + '_' + 'document' + ordering = ['-date_added'] + indexes = [ + models.Index(fields=['course', 'is_published', '-date_added' ]), + ] + def is_image(self): is_url_image = False if self.url: @@ -823,12 +830,37 @@ class Document(MediaBase): self.set_mime_type() super(Document, self).save(**kwargs) + def private_file(self, user): + private_document = DocumentPrivate.objects.get_or_create(document=self, user=user) + return private_document.file + + +class DocumentPrivate(MediaBase): + + document = models.ForeignKey('Document', related_name='private_documents', verbose_name=_('document'), + null=True, blank=True, on_delete=models.CASCADE) + user = models.ForeignKey(User, related_name="private_documents", verbose_name=_('user'), + null=True, blank=True, on_delete=models.CASCADE) + file = models.FileField(_('file'), upload_to='private_documents/%Y/%m/%d', db_column="filename", + max_length=1024, blank=True) class Meta(MetaCore): - db_table = app_label + '_' + 'document' + db_table = app_label + '_' + 'document_private' ordering = ['-date_added'] - indexes = [ - models.Index(fields=['course', 'is_published', '-date_added' ]), - ] + + def save(self, **kwargs): + if "pdf" in self.document.mimetype: + from pypdf import PdfReader, PdfWriter + writer = PdfWriter(clone_from=self.document.file.path) + writer.add_metadata({"/Downloader": self.user.username}) + name = self.document.file.name + today = datetime.date.today() + root = settings.MEDIA_ROOT + "private_documents/%s/%s/%s/" % (today.year, today.month, today.day) + os.path.makedirs(root) + path = root + user.username + " - " + name + with open(path, "wb") as f: + writer.write(f) + self.file = path + super(DocumentPrivate, self).save(**kwargs) class DocumentSimple(MediaBase): diff --git a/teleforma/views/core.py b/teleforma/views/core.py index 54d22a1b..43daa838 100644 --- a/teleforma/views/core.py +++ b/teleforma/views/core.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2011-2018 Parisson SARL -# Copyright (c) 2011-2018 Guillaume Pellerin +# Copyright (c) 2011-2024 Parisson SARL +# Copyright (c) 2011-2024 Guillaume Pellerin # This software is a computer program whose purpose is to backup, analyse, # transcode and stream any audio content with its metadata over a web frontend. @@ -31,6 +31,7 @@ # knowledge of the CeCILL license and that you accept its terms. # # Authors: Guillaume Pellerin + import datetime import os import requests @@ -796,7 +797,11 @@ class DocumentView(CourseAccessMixin, DetailView): courses = get_courses(request.user) document = Document.objects.get(pk=pk) if get_access(document, courses): - return serve_media(document.file.path, streaming=False) + if settings.TELEFORMA_PRIVATE_DOCUMENTS_MODE: + path = document.private_file.path + else: + path = document.file.path + return serve_media(path, streaming=False) else: return redirect('teleforma-home') @@ -804,7 +809,11 @@ class DocumentView(CourseAccessMixin, DetailView): courses = get_courses(request.user) document = Document.objects.get(pk=pk) if get_access(document, courses): - return serve_media(document.file.path, streaming=True) + if settings.TELEFORMA_PRIVATE_DOCUMENTS_MODE: + path = document.private_file.path + else: + path = document.file.path + return serve_media(path, streaming=True) else: return redirect('teleforma-home') @@ -870,6 +879,7 @@ class ConferenceView(CourseAccessMixin, DetailView): settings, "TELECASTER_LIVE_STREAMING_PROTOCOL", 'http') server_type = stream['server_type'] stream_type = stream['stream_type'] + if server_type == 'icecast': port = getattr( settings, "TELECASTER_LIVE_ICECAST_STREAMING_PORT", '8000') @@ -880,7 +890,6 @@ class ConferenceView(CourseAccessMixin, DetailView): settings, "TELECASTER_LIVE_STREAM_M_STREAMING_PORT", '8080') path = getattr( settings, "TELECASTER_LIVE_STREAM_M_STREAMING_PATH", '/') - #site = Site.objects.all()[0] server, c = StreamingServer.objects.get_or_create( protocol=protocol, host=host, -- 2.39.5