From fbe047e8a2e4bfd97ef60f01100b661246a2f042 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Wed, 28 Jun 2023 00:49:11 +0800 Subject: [PATCH] Painstakingly added an endpoint to show messages from the current study group of the user --- stude/accounts/migrations/0001_initial.py | 12 +---- stude/accounts/migrations/0002_initial.py | 45 ++++++++++++++++++ stude/api/urls.py | 3 +- stude/config/settings.py | 1 + stude/courses/migrations/0001_initial.py | 2 +- stude/courses/migrations/0002_initial.py | 4 +- stude/semesters/migrations/0001_initial.py | 2 +- .../student_status/migrations/0001_initial.py | 5 +- .../student_status/migrations/0002_initial.py | 10 +++- .../0003_alter_studentstatus_subject.py | 20 -------- ...move_studentstatus_study_group_and_more.py | 23 +++++++++ stude/student_status/models.py | 4 +- stude/study_groups/migrations/0001_initial.py | 3 +- stude/study_groups/models.py | 4 ++ stude/studygroup_messages/__init__.py | 0 stude/studygroup_messages/admin.py | 5 ++ stude/studygroup_messages/apps.py | 6 +++ .../migrations/0001_initial.py | 28 +++++++++++ .../migrations/__init__.py | 0 stude/studygroup_messages/models.py | 11 +++++ stude/studygroup_messages/serializers.py | 13 +++++ stude/studygroup_messages/tests.py | 3 ++ stude/studygroup_messages/urls.py | 13 +++++ stude/studygroup_messages/views.py | 47 +++++++++++++++++++ stude/subjects/migrations/0001_initial.py | 4 +- stude/year_levels/migrations/0001_initial.py | 2 +- 26 files changed, 223 insertions(+), 47 deletions(-) create mode 100644 stude/accounts/migrations/0002_initial.py delete mode 100644 stude/student_status/migrations/0003_alter_studentstatus_subject.py create mode 100644 stude/student_status/migrations/0003_remove_studentstatus_study_group_and_more.py create mode 100644 stude/studygroup_messages/__init__.py create mode 100644 stude/studygroup_messages/admin.py create mode 100644 stude/studygroup_messages/apps.py create mode 100644 stude/studygroup_messages/migrations/0001_initial.py create mode 100644 stude/studygroup_messages/migrations/__init__.py create mode 100644 stude/studygroup_messages/models.py create mode 100644 stude/studygroup_messages/serializers.py create mode 100644 stude/studygroup_messages/tests.py create mode 100644 stude/studygroup_messages/urls.py create mode 100644 stude/studygroup_messages/views.py diff --git a/stude/accounts/migrations/0001_initial.py b/stude/accounts/migrations/0001_initial.py index c3df0a2..1c5b101 100644 --- a/stude/accounts/migrations/0001_initial.py +++ b/stude/accounts/migrations/0001_initial.py @@ -1,10 +1,9 @@ -# Generated by Django 4.2.2 on 2023-06-27 13:08 +# Generated by Django 4.2.2 on 2023-06-27 15:21 import accounts.models import django.contrib.auth.models import django.contrib.auth.validators from django.db import migrations, models -import django.db.models.deletion import django.utils.timezone @@ -13,10 +12,6 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('auth', '0012_alter_user_first_name_max_length'), - ('courses', '0002_initial'), - ('semesters', '0001_initial'), - ('year_levels', '0001_initial'), ] operations = [ @@ -39,11 +34,6 @@ class Migration(migrations.Migration): ('is_banned', models.BooleanField(default=False)), ('student_id_number', models.CharField(max_length=16, validators=[accounts.models.validate_student_id])), ('avatar', models.ImageField(null=True, upload_to=accounts.models.CustomUser._get_upload_to)), - ('course', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.course')), - ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), - ('semester', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='semesters.semester')), - ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), - ('year_level', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='year_levels.year_level')), ], options={ 'verbose_name': 'user', diff --git a/stude/accounts/migrations/0002_initial.py b/stude/accounts/migrations/0002_initial.py new file mode 100644 index 0000000..8e150eb --- /dev/null +++ b/stude/accounts/migrations/0002_initial.py @@ -0,0 +1,45 @@ +# Generated by Django 4.2.2 on 2023-06-27 15:21 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ('year_levels', '0001_initial'), + ('accounts', '0001_initial'), + ('courses', '0002_initial'), + ('semesters', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='customuser', + name='course', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.course'), + ), + migrations.AddField( + model_name='customuser', + name='groups', + field=models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups'), + ), + migrations.AddField( + model_name='customuser', + name='semester', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='semesters.semester'), + ), + migrations.AddField( + model_name='customuser', + name='user_permissions', + field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions'), + ), + migrations.AddField( + model_name='customuser', + name='year_level', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='year_levels.year_level'), + ), + ] diff --git a/stude/api/urls.py b/stude/api/urls.py index f0d7631..6942309 100644 --- a/stude/api/urls.py +++ b/stude/api/urls.py @@ -8,5 +8,6 @@ urlpatterns = [ path('year_levels/', include('year_levels.urls')), path('semesters/', include('semesters.urls')), path('subjects/', include('subjects.urls')), - path('study_groups/', include('study_groups.urls')) + path('study_groups/', include('study_groups.urls')), + path('messages/', include('studygroup_messages.urls')) ] diff --git a/stude/config/settings.py b/stude/config/settings.py index 2892cbc..4a89979 100644 --- a/stude/config/settings.py +++ b/stude/config/settings.py @@ -68,6 +68,7 @@ INSTALLED_APPS = [ 'semesters', 'subjects', 'study_groups', + 'studygroup_messages', ] MIDDLEWARE = [ diff --git a/stude/courses/migrations/0001_initial.py b/stude/courses/migrations/0001_initial.py index f9bf4c5..2530e9b 100644 --- a/stude/courses/migrations/0001_initial.py +++ b/stude/courses/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 09:44 +# Generated by Django 4.2.2 on 2023-06-27 15:21 from django.db import migrations, models import django.db.models.deletion diff --git a/stude/courses/migrations/0002_initial.py b/stude/courses/migrations/0002_initial.py index f69407a..5f3e96f 100644 --- a/stude/courses/migrations/0002_initial.py +++ b/stude/courses/migrations/0002_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 09:44 +# Generated by Django 4.2.2 on 2023-06-27 15:21 from django.db import migrations, models import django.db.models.deletion @@ -9,8 +9,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('courses', '0001_initial'), ('subjects', '0001_initial'), + ('courses', '0001_initial'), ] operations = [ diff --git a/stude/semesters/migrations/0001_initial.py b/stude/semesters/migrations/0001_initial.py index dfc4049..e890336 100644 --- a/stude/semesters/migrations/0001_initial.py +++ b/stude/semesters/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 09:44 +# Generated by Django 4.2.2 on 2023-06-27 15:21 from django.db import migrations, models diff --git a/stude/student_status/migrations/0001_initial.py b/stude/student_status/migrations/0001_initial.py index 24adbf9..cf457c7 100644 --- a/stude/student_status/migrations/0001_initial.py +++ b/stude/student_status/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 13:08 +# Generated by Django 4.2.2 on 2023-06-27 15:21 from django.conf import settings from django.db import migrations, models @@ -10,7 +10,7 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('accounts', '0001_initial'), + ('accounts', '0002_initial'), ] operations = [ @@ -20,7 +20,6 @@ class Migration(migrations.Migration): ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)), ('x', models.FloatField(null=True)), ('y', models.FloatField(null=True)), - ('subject', models.CharField(max_length=100, null=True)), ('active', models.BooleanField(default=False)), ('timestamp', models.DateField(auto_now_add=True)), ], diff --git a/stude/student_status/migrations/0002_initial.py b/stude/student_status/migrations/0002_initial.py index 624ece8..801da19 100644 --- a/stude/student_status/migrations/0002_initial.py +++ b/stude/student_status/migrations/0002_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 13:08 +# Generated by Django 4.2.2 on 2023-06-27 15:21 from django.db import migrations, models import django.db.models.deletion @@ -9,8 +9,9 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('study_groups', '0001_initial'), ('student_status', '0001_initial'), + ('subjects', '0001_initial'), + ('study_groups', '0001_initial'), ] operations = [ @@ -19,4 +20,9 @@ class Migration(migrations.Migration): name='study_group', field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='study_groups.studygroup'), ), + migrations.AddField( + model_name='studentstatus', + name='subject', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='subjects.subject'), + ), ] diff --git a/stude/student_status/migrations/0003_alter_studentstatus_subject.py b/stude/student_status/migrations/0003_alter_studentstatus_subject.py deleted file mode 100644 index da0fd9e..0000000 --- a/stude/student_status/migrations/0003_alter_studentstatus_subject.py +++ /dev/null @@ -1,20 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-27 13:10 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('subjects', '0001_initial'), - ('student_status', '0002_initial'), - ] - - operations = [ - migrations.AlterField( - model_name='studentstatus', - name='subject', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='subjects.subject'), - ), - ] diff --git a/stude/student_status/migrations/0003_remove_studentstatus_study_group_and_more.py b/stude/student_status/migrations/0003_remove_studentstatus_study_group_and_more.py new file mode 100644 index 0000000..43495cc --- /dev/null +++ b/stude/student_status/migrations/0003_remove_studentstatus_study_group_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.2 on 2023-06-27 15:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('study_groups', '0001_initial'), + ('student_status', '0002_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='studentstatus', + name='study_group', + ), + migrations.AddField( + model_name='studentstatus', + name='study_group', + field=models.ManyToManyField(blank=True, null=True, through='study_groups.StudyGroupMembership', to='study_groups.studygroup'), + ), + ] diff --git a/stude/student_status/models.py b/stude/student_status/models.py index 2447f83..b2ca891 100644 --- a/stude/student_status/models.py +++ b/stude/student_status/models.py @@ -14,5 +14,5 @@ class StudentStatus(models.Model): 'subjects.Subject', on_delete=models.CASCADE, null=True) active = models.BooleanField(default=False) timestamp = models.DateField(auto_now_add=True) - study_group = models.ForeignKey( - StudyGroup, on_delete=models.SET_NULL, null=True) + study_group = models.ManyToManyField( + 'study_groups.StudyGroup', through='study_groups.StudyGroupMembership', blank=True) diff --git a/stude/study_groups/migrations/0001_initial.py b/stude/study_groups/migrations/0001_initial.py index 37b0202..6c2f8df 100644 --- a/stude/study_groups/migrations/0001_initial.py +++ b/stude/study_groups/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 13:08 +# Generated by Django 4.2.2 on 2023-06-27 15:21 from django.db import migrations, models import django.db.models.deletion @@ -18,6 +18,7 @@ class Migration(migrations.Migration): name='StudyGroup', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=48)), ('x', models.FloatField(null=True)), ('y', models.FloatField(null=True)), ('active', models.BooleanField(default=False)), diff --git a/stude/study_groups/models.py b/stude/study_groups/models.py index 0e4ef14..e6647c9 100644 --- a/stude/study_groups/models.py +++ b/stude/study_groups/models.py @@ -4,6 +4,7 @@ from subjects.models import Subject class StudyGroup(models.Model): + name = models.CharField(max_length=48) users = models.ManyToManyField( 'student_status.StudentStatus', through='StudyGroupMembership') x = models.FloatField(null=True) @@ -18,3 +19,6 @@ class StudyGroupMembership(models.Model): 'student_status.StudentStatus', on_delete=models.CASCADE) study_group = models.ForeignKey( 'study_groups.StudyGroup', on_delete=models.CASCADE) + + def __str__(self): + return f'StudyGroupMembership: User={self.user_id}, StudyGroup={self.study_group_id}' diff --git a/stude/studygroup_messages/__init__.py b/stude/studygroup_messages/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/studygroup_messages/admin.py b/stude/studygroup_messages/admin.py new file mode 100644 index 0000000..2e19a60 --- /dev/null +++ b/stude/studygroup_messages/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Message + + +admin.site.register(Message) diff --git a/stude/studygroup_messages/apps.py b/stude/studygroup_messages/apps.py new file mode 100644 index 0000000..16d78c7 --- /dev/null +++ b/stude/studygroup_messages/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class StudygroupMessagesConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'studygroup_messages' diff --git a/stude/studygroup_messages/migrations/0001_initial.py b/stude/studygroup_messages/migrations/0001_initial.py new file mode 100644 index 0000000..f6cdecc --- /dev/null +++ b/stude/studygroup_messages/migrations/0001_initial.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.2 on 2023-06-27 15:24 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('study_groups', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Message', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('message_content', models.TextField(max_length=1024)), + ('timestamp', models.DateField(auto_now_add=True)), + ('study_group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='study_groups.studygroup')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/stude/studygroup_messages/migrations/__init__.py b/stude/studygroup_messages/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/studygroup_messages/models.py b/stude/studygroup_messages/models.py new file mode 100644 index 0000000..dfbbecf --- /dev/null +++ b/stude/studygroup_messages/models.py @@ -0,0 +1,11 @@ +from django.db import models +from study_groups.models import StudyGroup + +# Create your models here. + + +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) diff --git a/stude/studygroup_messages/serializers.py b/stude/studygroup_messages/serializers.py new file mode 100644 index 0000000..65f327a --- /dev/null +++ b/stude/studygroup_messages/serializers.py @@ -0,0 +1,13 @@ +from rest_framework import serializers +from .models import Message +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) + + class Meta: + model = Message + fields = '__all__' diff --git a/stude/studygroup_messages/tests.py b/stude/studygroup_messages/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/stude/studygroup_messages/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/stude/studygroup_messages/urls.py b/stude/studygroup_messages/urls.py new file mode 100644 index 0000000..8fd0e5d --- /dev/null +++ b/stude/studygroup_messages/urls.py @@ -0,0 +1,13 @@ +from django.urls import include, path +from rest_framework import routers +from . import views + +router = routers.DefaultRouter() +router.register(r'group_messages', views.MessageViewSet, + basename='group_messages') + +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. +urlpatterns = [ + path('', include(router.urls)), +] diff --git a/stude/studygroup_messages/views.py b/stude/studygroup_messages/views.py new file mode 100644 index 0000000..03fee44 --- /dev/null +++ b/stude/studygroup_messages/views.py @@ -0,0 +1,47 @@ +from django.shortcuts import render +from .models import Message +from .serializers import MessageSerializer +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 +# Create your views here. + + +class MessageViewSet(viewsets.ModelViewSet): + serializer_class = MessageSerializer + permission_classes = [IsAuthenticated] + + def get_object(self): + user = self.request.user + return Message.objects.get(user=user) + + def get_queryset(self): + user = self.request.user + + if not user.is_student: + raise PermissionDenied( + "You must be a student to view messages of your current study group" + ) + + # Get student_status id of the current user + student_status = StudentStatus.objects.filter( + user=user.id + ).values_list('study_group', flat=True).first() + + 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) + + # Now fetch the Messages matching the study group id + messages = Message.objects.filter(study_group=study_group_id_list) + return messages + + # To-do: only allow destroy of messages if message is by the same user diff --git a/stude/subjects/migrations/0001_initial.py b/stude/subjects/migrations/0001_initial.py index 6fef778..ec2350e 100644 --- a/stude/subjects/migrations/0001_initial.py +++ b/stude/subjects/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 09:44 +# Generated by Django 4.2.2 on 2023-06-27 15:21 from django.db import migrations, models import django.db.models.deletion @@ -9,9 +9,9 @@ class Migration(migrations.Migration): initial = True dependencies = [ + ('semesters', '0001_initial'), ('year_levels', '0001_initial'), ('courses', '0001_initial'), - ('semesters', '0001_initial'), ] operations = [ diff --git a/stude/year_levels/migrations/0001_initial.py b/stude/year_levels/migrations/0001_initial.py index 5ff8b57..8859f76 100644 --- a/stude/year_levels/migrations/0001_initial.py +++ b/stude/year_levels/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 09:44 +# Generated by Django 4.2.2 on 2023-06-27 15:21 from django.db import migrations, models