|
|
@ -1,11 +1,13 @@ |
|
|
from rest_framework import generics, permissions |
|
|
from rest_framework import generics, permissions |
|
|
|
|
|
from rest_framework.viewsets import ModelViewSet |
|
|
from rest_framework.authentication import TokenAuthentication |
|
|
from rest_framework.authentication import TokenAuthentication |
|
|
from drf_yasg.utils import swagger_auto_schema |
|
|
from drf_yasg.utils import swagger_auto_schema |
|
|
from drf_yasg import openapi |
|
|
from drf_yasg import openapi |
|
|
from django.db.models import Q |
|
|
from django.db.models import Q |
|
|
|
|
|
|
|
|
from apps.certificate.models import Certificate |
|
|
from apps.certificate.models import Certificate |
|
|
from apps.certificate.serializers import CertificateRequestSerializer, CertificateSerializer |
|
|
|
|
|
|
|
|
from apps.certificate.serializers import AdminCertificateSerializer, CertificateRequestSerializer, CertificateSerializer |
|
|
|
|
|
from apps.account.permissions import IsSuperAdmin |
|
|
from apps.course.models import Course |
|
|
from apps.course.models import Course |
|
|
from apps.course.models.course import extract_text_from_json |
|
|
from apps.course.models.course import extract_text_from_json |
|
|
from utils.pagination import StandardResultsSetPagination |
|
|
from utils.pagination import StandardResultsSetPagination |
|
|
@ -57,7 +59,6 @@ class CertificateRequestView(generics.CreateAPIView): |
|
|
serializer.save(student=self.request.user) |
|
|
serializer.save(student=self.request.user) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserCertificatesListView(generics.ListAPIView): |
|
|
class UserCertificatesListView(generics.ListAPIView): |
|
|
serializer_class = CertificateSerializer |
|
|
serializer_class = CertificateSerializer |
|
|
permission_classes = [permissions.IsAuthenticated] |
|
|
permission_classes = [permissions.IsAuthenticated] |
|
|
@ -81,12 +82,6 @@ class UserCertificatesListView(generics.ListAPIView): |
|
|
return Certificate.objects.filter(student=self.request.user).order_by('-created_at') |
|
|
return Certificate.objects.filter(student=self.request.user).order_by('-created_at') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from rest_framework.viewsets import ModelViewSet |
|
|
|
|
|
from apps.certificate.serializers import AdminCertificateSerializer |
|
|
|
|
|
from apps.account.permissions import IsPanelUser, IsSuperAdmin |
|
|
|
|
|
from django.db.models import Q |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_professor(request): |
|
|
def is_professor(request): |
|
|
return getattr(request.user, 'user_type', None) == 'professor' |
|
|
return getattr(request.user, 'user_type', None) == 'professor' |
|
|
|
|
|
|
|
|
@ -107,12 +102,23 @@ class AdminCertificateViewSet(ModelViewSet): |
|
|
|
|
|
|
|
|
# Search query |
|
|
# Search query |
|
|
search_query = self.request.query_params.get('search', None) |
|
|
search_query = self.request.query_params.get('search', None) |
|
|
|
|
|
has_search = False |
|
|
if search_query: |
|
|
if search_query: |
|
|
|
|
|
has_search = True |
|
|
matching_course_ids = find_matching_course_ids(search_query) |
|
|
matching_course_ids = find_matching_course_ids(search_query) |
|
|
|
|
|
from django.db.models import Case, When, Value, IntegerField |
|
|
queryset = queryset.filter( |
|
|
queryset = queryset.filter( |
|
|
Q(student__fullname__icontains=search_query) | |
|
|
Q(student__fullname__icontains=search_query) | |
|
|
Q(student__email__icontains=search_query) | |
|
|
Q(student__email__icontains=search_query) | |
|
|
Q(course_id__in=matching_course_ids) |
|
|
Q(course_id__in=matching_course_ids) |
|
|
|
|
|
).annotate( |
|
|
|
|
|
search_relevance=Case( |
|
|
|
|
|
When(student__fullname__icontains=search_query, then=Value(3)), |
|
|
|
|
|
When(student__email__icontains=search_query, then=Value(2)), |
|
|
|
|
|
When(course_id__in=matching_course_ids, then=Value(1)), |
|
|
|
|
|
default=Value(0), |
|
|
|
|
|
output_field=IntegerField() |
|
|
|
|
|
) |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
# Filters |
|
|
# Filters |
|
|
@ -140,6 +146,6 @@ class AdminCertificateViewSet(ModelViewSet): |
|
|
if issued_before: |
|
|
if issued_before: |
|
|
queryset = queryset.filter(created_at__date__lte=issued_before) |
|
|
queryset = queryset.filter(created_at__date__lte=issued_before) |
|
|
|
|
|
|
|
|
|
|
|
if has_search: |
|
|
|
|
|
return queryset.order_by('-search_relevance', '-created_at') |
|
|
return queryset.order_by('-created_at') |
|
|
return queryset.order_by('-created_at') |
|
|
|
|
|
|
|
|
|
|
|
|