Painstakingly added an endpoint to show messages from the current study group of the user

This commit is contained in:
Keannu Bernasol 2023-06-28 00:49:11 +08:00
parent 11d6887af8
commit fbe047e8a2
26 changed files with 223 additions and 47 deletions

View file

@ -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',

View file

@ -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'),
),
]

View file

@ -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'))
]

View file

@ -68,6 +68,7 @@ INSTALLED_APPS = [
'semesters',
'subjects',
'study_groups',
'studygroup_messages',
]
MIDDLEWARE = [

View file

@ -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

View file

@ -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 = [

View file

@ -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

View file

@ -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)),
],

View file

@ -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'),
),
]

View file

@ -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'),
),
]

View file

@ -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'),
),
]

View file

@ -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)

View file

@ -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)),

View file

@ -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}'

View file

View file

@ -0,0 +1,5 @@
from django.contrib import admin
from .models import Message
admin.site.register(Message)

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class StudygroupMessagesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'studygroup_messages'

View file

@ -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)),
],
),
]

View file

@ -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)

View file

@ -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__'

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -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)),
]

View file

@ -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

View file

@ -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 = [

View file

@ -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