mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 22:12:22 +01:00
Add queue/upload restriction
This commit is contained in:
@ -4,7 +4,9 @@ from functools import wraps
|
|||||||
import django.middleware.csrf as csrf
|
import django.middleware.csrf as csrf
|
||||||
from django.contrib.auth import authenticate, login
|
from django.contrib.auth import authenticate, login
|
||||||
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||||
from django.db.models import Q
|
from django.db import transaction
|
||||||
|
from django.db.models import Q, Sum, Value
|
||||||
|
from django.db.models.functions import Coalesce
|
||||||
from django.http import JsonResponse, HttpResponseForbidden
|
from django.http import JsonResponse, HttpResponseForbidden
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.views.decorators.http import require_http_methods
|
from django.views.decorators.http import require_http_methods
|
||||||
@ -238,6 +240,17 @@ def upload(request):
|
|||||||
if not title:
|
if not title:
|
||||||
return JsonResponse({'success': False, 'errorMessage': 'Please enter titles which are not empty.'})
|
return JsonResponse({'success': False, 'errorMessage': 'Please enter titles which are not empty.'})
|
||||||
|
|
||||||
|
# Allow upload if the user has a good reputation
|
||||||
|
# Score function:
|
||||||
|
# - U = duration * songs uploaded
|
||||||
|
# - Q = duration * songs queued
|
||||||
|
# - If 3*U < Q: allow upload (otherwise don't)
|
||||||
|
stats = upload_stats(request.user)
|
||||||
|
ratio = stats['minutes_queued'] / (3.0 * stats['minutes_upload'])
|
||||||
|
if not request.user.is_superuser and ratio < 1.0:
|
||||||
|
msg = 'Queue-to-upload ratio too high. Please queue some more before uploading. ({:.2f})'
|
||||||
|
return JsonResponse({'success': False, 'errorMessage': msg.format(ratio)})
|
||||||
|
|
||||||
for i, file in enumerate(files):
|
for i, file in enumerate(files):
|
||||||
duration = File(file).info.length
|
duration = File(file).info.length
|
||||||
hash = send_to_bertha(file)
|
hash = send_to_bertha(file)
|
||||||
@ -277,3 +290,11 @@ def mute(request):
|
|||||||
command = QueueCommand(queue=request.user.queue, command='mute')
|
command = QueueCommand(queue=request.user.queue, command='mute')
|
||||||
command.save()
|
command.save()
|
||||||
return JsonResponse({})
|
return JsonResponse({})
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def upload_stats(user):
|
||||||
|
q = PlaylistSong.objects.filter(user=user, song__deleted=False).aggregate(
|
||||||
|
minutes_queued=Coalesce(Sum('song__duration'), Value(0)))
|
||||||
|
q.update(Song.objects.filter(user=user, deleted=False).aggregate(
|
||||||
|
minutes_upload=Coalesce(Sum('duration'), Value(0))))
|
||||||
|
return q
|
||||||
|
|||||||
Reference in New Issue
Block a user