]> git.parisson.com Git - teleforma.git/commitdiff
add DocumentPrivate to allow user metadata embeddding into PDFs
authorGuillaume Pellerin <guillaume.pellerin@free.fr>
Mon, 15 Apr 2024 01:56:29 +0000 (03:56 +0200)
committerGuillaume Pellerin <guillaume.pellerin@free.fr>
Mon, 15 Apr 2024 01:56:29 +0000 (03:56 +0200)
app/settings.py
teleforma/models/core.py
teleforma/views/core.py

index eb8a3a0eb23a90e00c2fca01d6b83c1ecd000315..92d3eef6065a8155546cc893e5c7d55d312a0fdf 100644 (file)
@@ -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'
index 489238038c15eb5d4a56693a9346baff42f3009a..6fab21fecccbac313e10a62285724286596532f3 100644 (file)
@@ -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):
index 54d22a1b743e7c23b76c6514ba0f2b73bcaa8323..43daa83819b63284002ce5f3c22d704d2652e47b 100644 (file)
@@ -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 <yomguy@parisson.com>
+
 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,