From dc21bb8c3279ff5bf942b5b1eeed0d0fc7e33dbd Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Wed, 28 Jun 2023 01:18:37 +0800 Subject: [PATCH] Polish study group messages --- .../0004_alter_studentstatus_study_group.py | 19 +++++++++++++++++++ .../0002_alter_message_timestamp.py | 18 ++++++++++++++++++ stude/studygroup_messages/models.py | 2 +- stude/studygroup_messages/serializers.py | 5 +++-- stude/studygroup_messages/views.py | 14 +++++++++++--- 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 stude/student_status/migrations/0004_alter_studentstatus_study_group.py create mode 100644 stude/studygroup_messages/migrations/0002_alter_message_timestamp.py diff --git a/stude/student_status/migrations/0004_alter_studentstatus_study_group.py b/stude/student_status/migrations/0004_alter_studentstatus_study_group.py new file mode 100644 index 0000000..bca9509 --- /dev/null +++ b/stude/student_status/migrations/0004_alter_studentstatus_study_group.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.2 on 2023-06-27 17:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('study_groups', '0001_initial'), + ('student_status', '0003_remove_studentstatus_study_group_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='studentstatus', + name='study_group', + field=models.ManyToManyField(blank=True, through='study_groups.StudyGroupMembership', to='study_groups.studygroup'), + ), + ] diff --git a/stude/studygroup_messages/migrations/0002_alter_message_timestamp.py b/stude/studygroup_messages/migrations/0002_alter_message_timestamp.py new file mode 100644 index 0000000..55d8981 --- /dev/null +++ b/stude/studygroup_messages/migrations/0002_alter_message_timestamp.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-06-27 17:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('studygroup_messages', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='message', + name='timestamp', + field=models.DateTimeField(auto_now_add=True), + ), + ] diff --git a/stude/studygroup_messages/models.py b/stude/studygroup_messages/models.py index dfbbecf..d864ea0 100644 --- a/stude/studygroup_messages/models.py +++ b/stude/studygroup_messages/models.py @@ -8,4 +8,4 @@ class Message(models.Model): user = models.ForeignKey('accounts.CustomUser', on_delete=models.CASCADE) study_group = models.ForeignKey(StudyGroup, on_delete=models.CASCADE) message_content = models.TextField(max_length=1024) - timestamp = models.DateField(auto_now_add=True) + timestamp = models.DateTimeField(auto_now_add=True) diff --git a/stude/studygroup_messages/serializers.py b/stude/studygroup_messages/serializers.py index 65f327a..5222e89 100644 --- a/stude/studygroup_messages/serializers.py +++ b/stude/studygroup_messages/serializers.py @@ -5,8 +5,9 @@ from accounts.models import CustomUser class MessageSerializer(serializers.ModelSerializer): user = serializers.SlugRelatedField( - queryset=CustomUser.objects.all(), slug_field='full_name') - group = serializers.CharField(source='subject.Subject', read_only=True) + queryset=CustomUser.objects.all(), slug_field='full_name', required=False) + study_group = serializers.CharField( + source='subject.Subject', read_only=True, required=False) class Meta: model = Message diff --git a/stude/studygroup_messages/views.py b/stude/studygroup_messages/views.py index 03fee44..535eb26 100644 --- a/stude/studygroup_messages/views.py +++ b/stude/studygroup_messages/views.py @@ -6,17 +6,26 @@ 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. 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) + 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) + def get_queryset(self): user = self.request.user @@ -41,7 +50,6 @@ class MessageViewSet(viewsets.ModelViewSet): print("Study Group List:", study_group_id_list) # Now fetch the Messages matching the study group id - messages = Message.objects.filter(study_group=study_group_id_list) + messages = Message.objects.filter( + study_group=study_group_id_list).order_by('-timestamp') return messages - - # To-do: only allow destroy of messages if message is by the same user