mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2025-06-28 16:25:44 +08:00
Added endpoint for querying avatars of users in a study group and for individual study group details
This commit is contained in:
parent
0ecfb2223a
commit
c8fca412b8
7 changed files with 199 additions and 16 deletions
|
@ -4,6 +4,16 @@ from accounts.models import CustomUser
|
|||
from subjects.models import Subject
|
||||
from drf_extra_fields.geo_fields import PointField
|
||||
from landmarks.models import Landmark
|
||||
from drf_extra_fields.fields import Base64ImageField
|
||||
from djoser.serializers import UserSerializer as BaseUserSerializer
|
||||
|
||||
|
||||
class CustomUserAvatarSerializer(BaseUserSerializer):
|
||||
avatar = Base64ImageField()
|
||||
|
||||
class Meta(BaseUserSerializer.Meta):
|
||||
model = CustomUser
|
||||
fields = ['avatar', 'username']
|
||||
|
||||
|
||||
class CustomUserKeyRelatedField(serializers.PrimaryKeyRelatedField):
|
||||
|
@ -13,6 +23,21 @@ class CustomUserKeyRelatedField(serializers.PrimaryKeyRelatedField):
|
|||
return str(value)
|
||||
|
||||
|
||||
class StudyGroupDetailSerializer(serializers.ModelSerializer):
|
||||
name = serializers.CharField()
|
||||
students = serializers.StringRelatedField(many=True)
|
||||
subject = serializers.SlugRelatedField(
|
||||
many=False, slug_field='name', queryset=Subject.objects.all(), required=True, allow_null=False)
|
||||
location = PointField()
|
||||
landmark = serializers.SlugRelatedField(
|
||||
queryset=Landmark.objects.all(), many=False, slug_field='name', required=False, allow_null=True)
|
||||
|
||||
class Meta:
|
||||
model = StudyGroup
|
||||
fields = '__all__'
|
||||
read_only_fields = ['landmark', 'radius', 'students', 'distance']
|
||||
|
||||
|
||||
class StudyGroupSerializer(serializers.ModelSerializer):
|
||||
name = serializers.CharField()
|
||||
students = serializers.StringRelatedField(many=True)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from django.urls import include, path
|
||||
from .views import StudyGroupListView, StudyGroupListNearView, StudyGroupCreateView
|
||||
from .views import StudyGroupListView, StudyGroupListNearView, StudyGroupCreateView, StudyGroupDetailView, StudyGroupAvatarsView
|
||||
from rest_framework import routers
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
|
@ -7,7 +7,10 @@ router.register(r'create', StudyGroupCreateView,
|
|||
basename='Create Study Group')
|
||||
|
||||
urlpatterns = [
|
||||
|
||||
path('', StudyGroupListView.as_view()),
|
||||
path('near/', StudyGroupListNearView.as_view()),
|
||||
path('', include(router.urls)),
|
||||
path('near/', StudyGroupListNearView.as_view()),
|
||||
path('member_avatars/', StudyGroupAvatarsView.as_view()),
|
||||
path('<str:name>/', StudyGroupDetailView.as_view()),
|
||||
]
|
||||
|
|
|
@ -2,7 +2,7 @@ from django.shortcuts import render
|
|||
from rest_framework import generics, mixins
|
||||
from rest_framework.exceptions import PermissionDenied
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from .serializers import StudyGroupSerializer, StudyGroupCreateSerializer, StudyGroupDistanceSerializer
|
||||
from .serializers import StudyGroupSerializer, StudyGroupCreateSerializer, StudyGroupDistanceSerializer, StudyGroupDetailSerializer, CustomUserAvatarSerializer
|
||||
from .models import StudyGroup
|
||||
from subjects.models import Subject, SubjectInstance
|
||||
from student_status.models import StudentStatus
|
||||
|
@ -12,10 +12,48 @@ from django.contrib.gis.db.models.functions import Distance
|
|||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import permissions
|
||||
from accounts.models import CustomUser
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
class StudyGroupAvatarsView(generics.ListAPIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = CustomUserAvatarSerializer
|
||||
queryset = CustomUser.objects.all()
|
||||
|
||||
def get_queryset(self):
|
||||
# Get user
|
||||
user = self.request.user
|
||||
|
||||
# Get status of user
|
||||
user_status = StudentStatus.objects.filter(user=user).first()
|
||||
|
||||
# Return error if not in study group
|
||||
if (not user_status.study_group):
|
||||
raise exceptions.ValidationError("You are not in a study group")
|
||||
|
||||
# Get study group of user
|
||||
user_study_group = user_status.study_group
|
||||
|
||||
# Get students in that study group
|
||||
study_group_student_ids = user_study_group.students.values_list(
|
||||
'user', flat=True)
|
||||
|
||||
# Then get user instances of those students
|
||||
study_group_users = CustomUser.objects.filter(
|
||||
id__in=study_group_student_ids)
|
||||
|
||||
# Return to be parsed by serializer
|
||||
return study_group_users
|
||||
|
||||
|
||||
class StudyGroupMembersAvatarView(generics.ListAPIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = StudyGroupCreateSerializer
|
||||
queryset = CustomUser.objects.all()
|
||||
|
||||
|
||||
class StudyGroupCreateView(viewsets.ModelViewSet):
|
||||
http_method_names = ['patch', 'post', 'delete']
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
@ -23,6 +61,13 @@ class StudyGroupCreateView(viewsets.ModelViewSet):
|
|||
queryset = StudyGroup.objects.all()
|
||||
|
||||
|
||||
class StudyGroupDetailView(generics.RetrieveAPIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = StudyGroupDetailSerializer
|
||||
queryset = StudyGroup.objects.all()
|
||||
lookup_field = 'name'
|
||||
|
||||
|
||||
class StudyGroupListView(generics.ListAPIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = StudyGroupSerializer
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue