-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathviews.py
141 lines (119 loc) · 4.33 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""Views for courses"""
from django.db import transaction
from django.db.models import Prefetch
from rest_framework import (
viewsets,
mixins,
status,
)
from rest_framework.views import APIView
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.exceptions import (
APIException,
NotFound,
ValidationError,
)
from rest_framework.generics import CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from courses.catalog_serializers import CatalogProgramSerializer
from courses.models import Program, CourseRun
from courses.serializers import ProgramSerializer, CourseRunSerializer
from dashboard.models import ProgramEnrollment
from profiles.models import Profile
from profiles.serializers import ProfileImageSerializer
class ResourceConflict(APIException):
"""Custom exception for Conflict Status Code"""
status_code = status.HTTP_409_CONFLICT
default_detail = 'The resource already exists.'
class ProgramViewSet(viewsets.ReadOnlyModelViewSet):
"""API for the Program collection"""
authentication_classes = (
SessionAuthentication,
TokenAuthentication,
)
permission_classes = (
IsAuthenticated,
)
queryset = Program.objects.filter(live=True)
serializer_class = ProgramSerializer
class ProgramLearnersView(APIView):
"""API for Learners enrolled in the Program"""
authentication_classes = (
SessionAuthentication,
TokenAuthentication,
)
permission_classes = (
IsAuthenticated,
)
serializer_class = ProfileImageSerializer
def get(self, request, *args, **kargs): # pylint: disable=unused-argument
"""
Get eight random learners with images and
the total count of visible learners in the program
"""
program_id = self.kwargs["program_id"]
users = ProgramEnrollment.objects.filter(
program_id=program_id
).values_list('user', flat=True)
queryset = Profile.objects.exclude(
image_small__exact=''
).filter(user__in=users).exclude(
account_privacy='private'
).exclude(
user=request.user
).order_by('?')
learners_result = {
'learners_count': queryset.count(),
'learners': ProfileImageSerializer(queryset[:8], many=True).data
}
return Response(
status=status.HTTP_200_OK,
data=learners_result
)
class ProgramEnrollmentListView(CreateAPIView):
"""API for the User Program Enrollments"""
serializer_class = ProgramSerializer
authentication_classes = (
SessionAuthentication,
TokenAuthentication,
)
permission_classes = (
IsAuthenticated,
)
@transaction.atomic
def create(self, request, *args, **kwargs): # pylint: disable=unused-argument
"""
Create an enrollment for the current user
"""
program_id = request.data.get('program_id')
if not isinstance(program_id, int):
raise ValidationError('A `program_id` parameter must be specified')
serializer = self.get_serializer_class()
try:
program = Program.objects.get(live=True, pk=program_id)
except Program.DoesNotExist:
raise NotFound('The specified program has not been found or it is not live yet')
_, created = ProgramEnrollment.objects.get_or_create(
user=request.user,
program=program,
)
status_code = status.HTTP_201_CREATED if created else status.HTTP_200_OK
return Response(
status=status_code,
data=serializer(program, context={'request': request}).data
)
class CourseRunViewSet(viewsets.ReadOnlyModelViewSet):
"""API for the CourseRun model"""
serializer_class = CourseRunSerializer
queryset = CourseRun.objects.not_discontinued()
class CatalogViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""API for program/course catalog list"""
serializer_class = CatalogProgramSerializer
queryset = Program.objects.filter(live=True).prefetch_related(
Prefetch(
"course_set__courserun_set",
queryset=CourseRun.objects.not_discontinued(),
),
"programpage__thumbnail_image"
)