Skip to content

Commit 6c55e61

Browse files
validate img size
1 parent 782726e commit 6c55e61

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

account/serializers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from django.conf import settings
12
from django.contrib.auth import get_user_model
3+
from django.utils.translation import gettext as _
24
from rest_framework import serializers
35

46
User = get_user_model()
@@ -15,6 +17,7 @@ class Meta:
1517
'avatar': {'allow_null': True},
1618
'avatar_thumbnail': {'read_only': True},
1719
'user_type': {'read_only': True},
20+
'email': {'read_only': True}
1821
}
1922

2023
def validate(self, attrs):
@@ -24,7 +27,13 @@ def validate(self, attrs):
2427
return attrs
2528

2629
def validate_avatar(self, img):
27-
return img # TODO
30+
size = img.size / 1e6 # bytes to megabytes
31+
if size > settings.MAX_UPLOAD_SIZE_MEGABYTES:
32+
raise serializers.ValidationError(
33+
_('File size must not exceed %dMB.') % settings.MAX_UPLOAD_SIZE_MEGABYTES,
34+
code='exceed_max_upload_size'
35+
)
36+
return img
2837

2938

3039
class RegisterTeacherSerializer(UserSerializer):

account/views.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.contrib.auth import get_user_model
2+
from django.db.models import Q
23
from django.utils.decorators import classonlymethod, method_decorator
34
from django.views.decorators.debug import sensitive_post_parameters
45
from rest_framework import mixins
@@ -9,6 +10,7 @@
910
from rest_framework.viewsets import GenericViewSet
1011

1112
from account import business
13+
from account.managers import UserTypes
1214
from account.serializers import (MeSerializer, RegisterTeacherSerializer,
1315
UserSerializer)
1416

@@ -34,7 +36,23 @@ class UserViewSet(mixins.ListModelMixin,
3436
serializer_class = UserSerializer
3537

3638
def get_queryset(self):
37-
return User.objects.all() # TODO: if teacher: return students, if student: return teacher and classmates
39+
user = self.request.user
40+
41+
if user.is_teacher():
42+
classrooms = user.classrooms_teaching.all()
43+
else:
44+
classrooms = user.classrooms_studying.all()
45+
46+
classrooms = classrooms.select_related('teacher').prefetch_related('students')
47+
48+
users_pk = []
49+
for classroom in classrooms:
50+
teacher = classroom.teacher
51+
students = classroom.students.all()
52+
users_pk.append(teacher.pk)
53+
users_pk.extend(student.pk for student in students)
54+
55+
return User.objects.filter(pk__in=users_pk)
3856

3957
@action(
4058
methods=['POST'], detail=False, url_path='register-teacher',

classroom/serializers/exercise.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.conf import settings
12
from django.utils.translation import gettext as _
23
from rest_framework import serializers
34

@@ -65,4 +66,10 @@ class ReadingExerciseUploadImgSerializer(serializers.Serializer):
6566
image_url = serializers.URLField(read_only=True)
6667

6768
def validate_image(self, image):
68-
return image # TODO
69+
size = image.size / 1e6 # bytes to megabytes
70+
if size > settings.MAX_UPLOAD_SIZE_MEGABYTES:
71+
raise serializers.ValidationError(
72+
_('File size must not exceed %dMB.') % settings.MAX_UPLOAD_SIZE_MEGABYTES,
73+
code='exceed_max_upload_size'
74+
)
75+
return image

keep_learning/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,5 @@
281281
del REST_FRAMEWORK['DEFAULT_THROTTLE_RATES']
282282

283283
WEB_LOGIN_URL = env('WEB_LOGIN_URL')
284+
285+
MAX_UPLOAD_SIZE_MEGABYTES = 10

0 commit comments

Comments
 (0)