diff --git a/apps/course/views/admin.py b/apps/course/views/admin.py index 7601c63..30a798c 100644 --- a/apps/course/views/admin.py +++ b/apps/course/views/admin.py @@ -340,6 +340,34 @@ class AdminAttachmentViewSet(viewsets.ModelViewSet): search = self.request.query_params.get('search', None) if search: queryset = queryset.filter(title__icontains=search) + + file_type = self.request.query_params.get('type', None) + if file_type: + image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.bmp'] + video_extensions = ['.mp4', '.webm', '.ogg', '.mkv', '.mov', '.avi'] + audio_extensions = ['.mp3', '.wav', '.ogg', '.m4a', '.aac', '.flac'] + + if file_type == 'image': + q_objs = Q() + for ext in image_extensions: + q_objs |= Q(file__iendswith=ext) + queryset = queryset.filter(q_objs) + elif file_type == 'video': + q_objs = Q() + for ext in video_extensions: + q_objs |= Q(file__iendswith=ext) + queryset = queryset.filter(q_objs) + elif file_type == 'audio': + q_objs = Q() + for ext in audio_extensions: + q_objs |= Q(file__iendswith=ext) + queryset = queryset.filter(q_objs) + elif file_type == 'other': + q_objs = Q() + for ext in image_extensions + video_extensions + audio_extensions: + q_objs |= Q(file__iendswith=ext) + queryset = queryset.exclude(q_objs) + return queryset