]> git.parisson.com Git - teleforma.git/commitdiff
start s3 for exam scripts
authorGuillaume Pellerin <guillaume.pellerin@parisson.com>
Thu, 7 Aug 2025 09:21:03 +0000 (11:21 +0200)
committerGuillaume Pellerin <guillaume.pellerin@parisson.com>
Thu, 8 Jan 2026 20:23:20 +0000 (21:23 +0100)
app/settings.py
teleforma/exam/models.py
teleforma/exam/views.py

index 6001ca209f3ef9efc62fe59cddc25a08a4eacb40..680cfae9bb2dd68a4f619d8feda7b8f786f35226 100644 (file)
@@ -598,6 +598,7 @@ TELEFORMA_PERIOD_DEFAULT_ID = 34
 TELEFORMA_EXAM_MAX_SESSIONS = 200
 TELEFORMA_EXAM_SCRIPT_MAX_SIZE = 31457280
 TELEFORMA_EXAM_SCRIPT_SERVICE_URL = '/webviewer/teleforma.html'
+TELEFORMA_EXAM_USE_S3 = True
 TELEFORMA_PRIVATE_DOCUMENTS_MODE = True
 TELEFORMA_PRIVATE_MEDIA_USE_S3 = True
 TELFORMA_MEDIA_DOWNLOAD = False
index d33c611967d9d740c2547361ac1eddf7df3ec6d8..ddc3865e50fda13b4237ad60e28eff779f31b4c1 100755 (executable)
@@ -46,6 +46,7 @@ import uuid
 from django.conf import settings
 from django.contrib.auth.models import User
 from django.contrib.sites.models import Site
+from django.core.files.storage import FileSystemStorage
 from django.db import models
 from django.db.models.signals import post_save
 from django.template.loader import render_to_string
@@ -58,6 +59,9 @@ from teleforma.models.core import Course, Period
 
 from ..models.core import session_choices
 
+from storages.backends.s3boto3 import S3Boto3Storage
+
+
 app = 'teleforma'
 
 
@@ -65,24 +69,6 @@ class MetaCore:
 
     app_label = 'exam'
 
-
-# import boxsdk
-# from boxsdk import OAuth2, Client
-# import StringIO
-#
-# box_client_id = settings.BOX_CLIENT_ID
-# box_client_secret = settings.BOX_CLIENT_SECRET
-# box_redirect_url = settings.BOX_REDIRECT_URL
-#
-# oauth = OAuth2(
-#     client_id=box_client_id,
-#     client_secret=box_client_secret,
-#     store_tokens='',
-# )
-#
-# box_auth_url, box_csrf_token = oauth.get_authorization_url('http://' + BOX_REDIRECT_URL)
-# box_client = Client(oauth)
-
 SCRIPT_STATUS = ((0, _('rejected')), (1, _('draft')), (2, _('submitted')),
                  (3, _('pending')), (4, _('marked')), (5, _('read')),
                  (6, _('backup')), (7, _('stat')))
@@ -104,6 +90,11 @@ REJECT_REASON = (('unreadable', _('unreadable')),
 cache_path = settings.MEDIA_ROOT + 'cache/'
 script_path = settings.MEDIA_ROOT + 'scripts/'
 
+if settings.TELEFORMA_EXAM_USE_S3:
+    storage = S3Boto3Storage
+else:
+    storage = FileSystemStorage
+
 
 def sha1sum_file(filename):
     '''
@@ -271,7 +262,12 @@ class Script(BaseResource):
     corrector = models.ForeignKey(User, related_name="corrector_scripts", verbose_name=_(
         'corrector'), blank=True, null=True, on_delete=models.SET_NULL)
     file = models.FileField(
-        _('PDF file'), upload_to='scripts/%Y/%m/%d', max_length=1024, blank=True)
+        _('PDF file'),
+        upload_to='scripts/%Y/%m/%d',
+        max_length=1024,
+        blank=True,
+        storage=storage
+        )
     box_uuid = models.CharField(_('Box UUID'), max_length=256, blank=True)
     score = models.FloatField(_('score'), blank=True,
                               null=True, help_text="/20")
index 73ffee919a7089ca91bd6c4e2eb39ce383c519ca..cebe4753d68277b86485f223a05f94dda364a402 100755 (executable)
@@ -30,7 +30,7 @@ from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer
 
 from teleforma.decorators import access_required, staff_required
 from teleforma.models.crfpa import Student
-from teleforma.views.core import CourseAccessMixin, get_courses
+from teleforma.views.core import CourseAccessMixin, get_courses, serve_media
 
 from ..exam.forms import MassScoreForm, ScoreForm, ScriptForm
 from ..exam.models import Quota, Script, ScriptType
@@ -173,6 +173,23 @@ class ScriptView(ScriptMixinView, CourseAccessMixin, UpdateView):
     def dispatch(self, *args, **kwargs):
         return super(ScriptView, self).dispatch(*args, **kwargs)
 
+    def serve(self, request, pk, streaming=False):
+        bucket = False
+        if settings.TELEFORMA_EXAM_USE_S3:
+            bucket = True
+
+        return serve_media(
+            file,
+            streaming=streaming,
+            bucket=bucket,
+            )
+
+    def download(self, request, pk):
+        return self.serve(request, pk, streaming=False)
+
+    def view(self, request, pk):
+        return self.serve(request, pk, streaming=True)
+
 
 class ScriptDownloadView(ScriptMixinView, CourseAccessMixin):
     """