mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2025-06-28 16:25:44 +08:00
Removed associative entity and fixed relationship between student status and study group
This commit is contained in:
parent
771300f933
commit
7dc80caee7
23 changed files with 266 additions and 253 deletions
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-09-03 09:32
|
||||
# Generated by Django 4.2.3 on 2023-09-25 13:07
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
|
|
@ -3,7 +3,7 @@ from rest_framework import routers
|
|||
from . import views
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
router.register(r'group_messages', views.MessageViewSet,
|
||||
router.register(r'', views.MessageViewSet,
|
||||
basename='group_messages')
|
||||
|
||||
# Wire up our API using automatic URL routing.
|
||||
|
|
|
@ -5,7 +5,6 @@ from rest_framework.permissions import IsAuthenticated
|
|||
from rest_framework.exceptions import PermissionDenied
|
||||
from rest_framework import viewsets
|
||||
from student_status.models import StudentStatus
|
||||
from study_groups.models import StudyGroupMembership
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
# Create your views here.
|
||||
|
@ -15,25 +14,29 @@ class MessageViewSet(viewsets.ModelViewSet):
|
|||
serializer_class = MessageSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
http_method_names = ['get', 'post']
|
||||
|
||||
def get_object(self):
|
||||
user = self.request.user
|
||||
return Message.objects.get(user=user)
|
||||
queryset = Message.objects.all()
|
||||
|
||||
def perform_create(self, serializer):
|
||||
user = self.request.user
|
||||
study_group_id_list = StudyGroupMembership.objects.filter(
|
||||
user=user.id).values_list('study_group', flat=True).first()
|
||||
serializer.save(user=user, study_group_id=study_group_id_list)
|
||||
user_status = StudentStatus.objects.filter(user=user).first()
|
||||
user_study_group = user_status.study_group
|
||||
serializer.save(user=user, study_group=user_study_group)
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
user_status = StudentStatus.objects.filter(user=user).first()
|
||||
user_study_group = user_status.study_group
|
||||
|
||||
if not user.is_student:
|
||||
raise PermissionDenied(
|
||||
"You must be a student to view messages of your current study group"
|
||||
)
|
||||
|
||||
if not user_study_group:
|
||||
raise PermissionDenied(
|
||||
"You are currently do not have a study group"
|
||||
)
|
||||
|
||||
# Get student_status id of the current user
|
||||
student_status = StudentStatus.objects.filter(
|
||||
user=user.id
|
||||
|
@ -41,15 +44,9 @@ class MessageViewSet(viewsets.ModelViewSet):
|
|||
|
||||
print("User ID:", user.id)
|
||||
print("Student_Status ID:", student_status)
|
||||
|
||||
# Get the study group id
|
||||
print(StudyGroupMembership.objects.all())
|
||||
study_group_id_list = StudyGroupMembership.objects.filter(
|
||||
user=user.id).values_list('study_group').first()
|
||||
|
||||
print("Study Group List:", study_group_id_list)
|
||||
print("User Study Group:", user_study_group)
|
||||
|
||||
# Now fetch the Messages matching the study group id
|
||||
messages = Message.objects.filter(
|
||||
study_group=study_group_id_list).order_by('-timestamp')
|
||||
study_group=user_study_group).order_by('-timestamp')
|
||||
return messages
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue