mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Polish study group messages
This commit is contained in:
parent
fbe047e8a2
commit
dc21bb8c32
5 changed files with 52 additions and 6 deletions
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue