26 lines
898 B
Python

from rest_framework.permissions import BasePermission
from django.core.exceptions import ValidationError
from users.models import User
MAX_IMAGE_SIZE_MB = 2
MAX_VIDEO_SIZE_MB = 50
def validate_score(score):
if score < 1 or score > 5:
raise ValidationError('Score must be between 1 and 5')
def validate_image_size(file):
max_size = MAX_IMAGE_SIZE_MB * 1024 * 1024
if file.size > max_size:
raise ValidationError(f'Max size file is {MAX_IMAGE_SIZE_MB} MB')
def validate_video_size(file):
max_size = MAX_VIDEO_SIZE_MB * 1024 * 1024
if file.size > max_size:
raise ValidationError(f'Max size file is {MAX_VIDEO_SIZE_MB} MB')
class IsProfessorOnly(BasePermission):
def has_permission(self, request, view):
user: User = request.user
return user.is_authenticated and (user.role == User.Roles.PROFESSOR.value or user.is_superuser)