Browse Source

attachment filter based on type of file added

master
Mohsen Taba 2 months ago
parent
commit
5219e03628
  1. 28
      apps/course/views/admin.py

28
apps/course/views/admin.py

@ -340,6 +340,34 @@ class AdminAttachmentViewSet(viewsets.ModelViewSet):
search = self.request.query_params.get('search', None) search = self.request.query_params.get('search', None)
if search: if search:
queryset = queryset.filter(title__icontains=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 return queryset

Loading…
Cancel
Save