Skip to content

Commit a52bf22

Browse files
committed
make enabling optional
1 parent 8312eb3 commit a52bf22

40 files changed

+99
-60
lines changed

cms/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
REPORTED_TIMES_THRESHOLD = 10
4444
ALLOW_ANONYMOUS_ACTIONS = ["report", "like", "dislike", "watch"] # need be a list
4545

46+
# whether media attachments functionality is enabled
47+
ENABLE_MEDIA_ATTACHMENTS = False # Set to True to enable attachments feature
48+
4649
# experimental functionality for user ratings - does not work
4750
ALLOW_RATINGS = False
4851
ALLOW_RATINGS_CONFIRMED_EMAIL_ONLY = True

files/admin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ class AttachmentAdmin(admin.ModelAdmin):
283283
admin.site.register(Language, LanguageAdmin)
284284
admin.site.register(VideoTrimRequest, VideoTrimRequestAdmin)
285285
admin.site.register(TranscriptionRequest, TranscriptionRequestAdmin)
286-
admin.site.register(Attachment, AttachmentAdmin)
286+
287+
# Only register Attachment admin if the feature is enabled
288+
if settings.ENABLE_MEDIA_ATTACHMENTS:
289+
admin.site.register(Attachment, AttachmentAdmin)
287290

288291
Media._meta.app_config.verbose_name = "Media"

files/context_processors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def stuff(request):
2929
ret["CAN_DISLIKE_MEDIA"] = settings.CAN_DISLIKE_MEDIA
3030
ret["CAN_REPORT_MEDIA"] = settings.CAN_REPORT_MEDIA
3131
ret["CAN_SHARE_MEDIA"] = settings.CAN_SHARE_MEDIA
32+
ret["ENABLE_MEDIA_ATTACHMENTS"] = settings.ENABLE_MEDIA_ATTACHMENTS
3233
ret["UPLOAD_MAX_SIZE"] = settings.UPLOAD_MAX_SIZE
3334
ret["UPLOAD_MAX_FILES_NUMBER"] = settings.UPLOAD_MAX_FILES_NUMBER
3435
ret["PRE_UPLOAD_MEDIA_MESSAGE"] = settings.PRE_UPLOAD_MEDIA_MESSAGE

files/models/attachment.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
from django.db import models
22
from django.db.models.signals import post_delete
33
from django.dispatch import receiver
4+
import os
45
from .media import Media
56

67
class Attachment(models.Model):
78
"""Attachment model for files linked to a video (Media)"""
89
media = models.ForeignKey(Media, on_delete=models.CASCADE, related_name="attachments")
9-
name = models.CharField(max_length=255, help_text="Naam van het bestand")
10+
name = models.CharField(max_length=255, blank=True, help_text="Name of the file (optional, uses filename if empty)")
1011
file = models.FileField(upload_to="attachments/%Y/%m/%d/")
1112
uploaded_at = models.DateTimeField(auto_now_add=True)
1213

14+
def save(self, *args, **kwargs):
15+
# If name is empty, use the filename
16+
if not self.name and self.file:
17+
# Get the filename without the path
18+
self.name = os.path.basename(self.file.name)
19+
super().save(*args, **kwargs)
20+
1321
def __str__(self):
1422
return f"{self.name} ({self.file.name})"
1523

files/serializers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def get_url(self, obj):
107107
return self.context["request"].build_absolute_uri(obj.get_absolute_url())
108108

109109
def get_attachments(self, obj):
110+
# Only return attachments if the feature is enabled
111+
if not settings.ENABLE_MEDIA_ATTACHMENTS:
112+
return []
110113
attachments = obj.attachments.all()
111114
return AttachmentSerializer(attachments, many=True, context=self.context).data
112115

@@ -274,6 +277,7 @@ class Meta:
274277
class AttachmentSerializer(serializers.ModelSerializer):
275278
file_url = serializers.SerializerMethodField()
276279
file_size = serializers.SerializerMethodField()
280+
name = serializers.CharField(required=False, allow_blank=True)
277281

278282
def get_file_url(self, obj):
279283
request = self.context.get('request')

files/views/attachment.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from ..models import Attachment, Media
44
from ..serializers import AttachmentSerializer
55
from django import forms
6+
from django.conf import settings
67
from django.core.paginator import Paginator
8+
from django.http import Http404
79
from django.views.decorators.http import require_http_methods
810
from django.shortcuts import render, get_object_or_404, redirect
911

@@ -18,6 +20,12 @@ class AttachmentViewSet(viewsets.ModelViewSet):
1820
parser_classes = [MultiPartParser, FormParser]
1921
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
2022

23+
def initial(self, request, *args, **kwargs):
24+
# Check if attachments feature is enabled
25+
if not settings.ENABLE_MEDIA_ATTACHMENTS:
26+
raise Http404("Attachments feature is not enabled")
27+
super().initial(request, *args, **kwargs)
28+
2129
def get_queryset(self):
2230
media_id = self.request.query_params.get('media')
2331
if media_id:
@@ -43,6 +51,10 @@ def perform_destroy(self, instance):
4351

4452
@require_http_methods(["GET", "POST"])
4553
def edit_attachments(request):
54+
# Check if attachments feature is enabled
55+
if not settings.ENABLE_MEDIA_ATTACHMENTS:
56+
raise Http404("Attachments feature is not enabled")
57+
4658
media_token = request.GET.get('m')
4759
media_object = get_object_or_404(Media, friendly_token=media_token)
4860
attachments_qs = Attachment.objects.filter(media=media_object).order_by('-uploaded_at')

frontend/src/static/js/components/media-actions/AttachmentDownloadLink.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState, useEffect } from 'react';
22
import { usePopup } from '../../utils/hooks/';
33
import { SiteContext } from '../../utils/contexts/';
4-
import { MediaPageStore } from '../../utils/stores/';
4+
import { MediaPageStore, PageStore } from '../../utils/stores/';
55
import { formatInnerLink } from '../../utils/helpers/';
66
import { CircleIconButton, MaterialIcon, NavigationContentApp, NavigationMenuList, PopupMain } from '../_shared/';
77
import { translateString } from '../../utils/helpers/';
@@ -60,7 +60,12 @@ export function AttachmentDownloadLink(props) {
6060
};
6161
}, []);
6262

63-
if (!hasAttachments) {
63+
// Check if attachments feature is enabled via MediaCMS config
64+
const features = PageStore.get('config-options')?.features;
65+
const attachmentsEnabled = features?.media?.actions?.attachments !== false;
66+
67+
// Don't render if feature is disabled or no attachments
68+
if (!attachmentsEnabled || !hasAttachments) {
6469
return null;
6570
}
6671

static/css/_commons.css

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/css/add-media.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/css/media.css

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)