Add API routes for courses and sessions
This commit is contained in:
parent
b1ff647c6e
commit
c80bf1e56a
|
@ -51,7 +51,7 @@ class Course(models.Model):
|
||||||
|
|
||||||
class Session(models.Model):
|
class Session(models.Model):
|
||||||
instructor = models.ForeignKey(User, related_name='teaching', blank=True, null=True, on_delete=models.SET_NULL)
|
instructor = models.ForeignKey(User, related_name='teaching', blank=True, null=True, on_delete=models.SET_NULL)
|
||||||
course = models.ForeignKey(Course, blank=True, null=True, on_delete=models.SET_NULL)
|
course = models.ForeignKey(Course, related_name='sessions', blank=True, null=True, on_delete=models.SET_NULL)
|
||||||
|
|
||||||
old_instructor = models.TextField(blank=True, null=True)
|
old_instructor = models.TextField(blank=True, null=True)
|
||||||
datetime = models.DateTimeField(blank=True, null=True)
|
datetime = models.DateTimeField(blank=True, null=True)
|
||||||
|
@ -59,7 +59,7 @@ class Session(models.Model):
|
||||||
|
|
||||||
class Training(models.Model):
|
class Training(models.Model):
|
||||||
user = models.ForeignKey(User, related_name='training', blank=True, null=True, on_delete=models.SET_NULL)
|
user = models.ForeignKey(User, related_name='training', blank=True, null=True, on_delete=models.SET_NULL)
|
||||||
session = models.ForeignKey(Session, blank=True, null=True, on_delete=models.SET_NULL)
|
session = models.ForeignKey(Session, related_name='students', blank=True, null=True, on_delete=models.SET_NULL)
|
||||||
|
|
||||||
member_id = models.IntegerField(blank=True, null=True)
|
member_id = models.IntegerField(blank=True, null=True)
|
||||||
attendance_status = models.TextField(blank=True, null=True)
|
attendance_status = models.TextField(blank=True, null=True)
|
||||||
|
|
|
@ -41,6 +41,40 @@ class TransactionSerializer(serializers.ModelSerializer):
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
class SessionDetailSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = models.Training
|
||||||
|
exclude = ['user']
|
||||||
|
|
||||||
|
class SessionSerializer(serializers.ModelSerializer):
|
||||||
|
student_count = serializers.SerializerMethodField()
|
||||||
|
class Meta:
|
||||||
|
model = models.Session
|
||||||
|
fields = '__all__'
|
||||||
|
depth = 1
|
||||||
|
def get_student_count(self, obj):
|
||||||
|
return len(obj.students.all())
|
||||||
|
|
||||||
|
|
||||||
|
class CourseSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = models.Course
|
||||||
|
fields = '__all__'
|
||||||
|
|
||||||
|
class CourseDetailSerializer(serializers.ModelSerializer):
|
||||||
|
sessions = SessionSerializer(many=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = models.Course
|
||||||
|
fields = '__all__'
|
||||||
|
depth = 1
|
||||||
|
|
||||||
|
class AdminCourseSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = models.Course
|
||||||
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
class RegistrationSerializer(RegisterSerializer):
|
class RegistrationSerializer(RegisterSerializer):
|
||||||
first_name = serializers.CharField(max_length=32)
|
first_name = serializers.CharField(max_length=32)
|
||||||
last_name = serializers.CharField(max_length=32)
|
last_name = serializers.CharField(max_length=32)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
|
from django.db.models import Max
|
||||||
from rest_framework import viewsets, views, permissions
|
from rest_framework import viewsets, views, permissions
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_auth.registration.views import RegisterView
|
from rest_auth.registration.views import RegisterView
|
||||||
|
@ -33,6 +34,33 @@ class MemberViewSet(viewsets.ModelViewSet):
|
||||||
return serializers.MemberSerializer
|
return serializers.MemberSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class CourseViewSet(viewsets.ModelViewSet):
|
||||||
|
permission_classes = [AllowMetadata | permissions.IsAuthenticated]
|
||||||
|
queryset = models.Course.objects.annotate(date=Max('sessions__datetime')).order_by('-date')
|
||||||
|
|
||||||
|
def get_serializer_class(self):
|
||||||
|
if self.action == 'retrieve':
|
||||||
|
return serializers.CourseDetailSerializer
|
||||||
|
else:
|
||||||
|
return serializers.CourseSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class SessionViewSet(viewsets.ModelViewSet):
|
||||||
|
permission_classes = [AllowMetadata | permissions.IsAuthenticated]
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
if self.action == 'list':
|
||||||
|
return models.Session.objects.order_by('-datetime')[:20]
|
||||||
|
else:
|
||||||
|
return models.Session.objects.all()
|
||||||
|
|
||||||
|
def get_serializer_class(self):
|
||||||
|
#if self.action == 'retrieve':
|
||||||
|
# return serializers.CourseDetailSerializer
|
||||||
|
#else:
|
||||||
|
return serializers.SessionSerializer
|
||||||
|
|
||||||
|
|
||||||
class MyUserView(views.APIView):
|
class MyUserView(views.APIView):
|
||||||
permission_classes = [AllowMetadata | permissions.IsAuthenticated]
|
permission_classes = [AllowMetadata | permissions.IsAuthenticated]
|
||||||
|
|
||||||
|
|
|
@ -169,7 +169,7 @@ if DEBUG:
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
||||||
'PAGE_SIZE': 20,
|
'PAGE_SIZE': 100,
|
||||||
'DEFAULT_RENDERER_CLASSES': DEFAULT_RENDERER_CLASSES,
|
'DEFAULT_RENDERER_CLASSES': DEFAULT_RENDERER_CLASSES,
|
||||||
'DEFAULT_AUTHENTICATION_CLASSES': DEFAULT_AUTHENTICATION_CLASSES,
|
'DEFAULT_AUTHENTICATION_CLASSES': DEFAULT_AUTHENTICATION_CLASSES,
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ from .api import views
|
||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
router.register(r'users', views.UserViewSet)
|
router.register(r'users', views.UserViewSet)
|
||||||
router.register(r'members', views.MemberViewSet, basename='member')
|
router.register(r'members', views.MemberViewSet, basename='member')
|
||||||
|
router.register(r'courses', views.CourseViewSet, basename='course')
|
||||||
|
router.register(r'sessions', views.SessionViewSet, basename='session')
|
||||||
#router.register(r'me', views.FullMemberView, basename='fullmember')
|
#router.register(r'me', views.FullMemberView, basename='fullmember')
|
||||||
#router.register(r'registration', views.RegistrationViewSet, basename='register')
|
#router.register(r'registration', views.RegistrationViewSet, basename='register')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user