Commit 15f7c1a4 authored by Fernando Ribeiro's avatar Fernando Ribeiro
Browse files

Feat: update models, views, serializers. Part 2 of #158

parent 54864a5c
# Generated by Django 2.2.12 on 2020-06-09 17:53
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('courses', '0008_auto_20200608_1102'),
]
operations = [
migrations.AlterField(
model_name='professormessage',
name='users',
field=models.ManyToManyField(blank=True, related_name='messages', to=settings.AUTH_USER_MODEL),
),
]
......@@ -630,6 +630,7 @@ class ProfessorMessage(models.Model):
users = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name='messages',
blank=True,
)
subject = models.CharField(
_('Subject'),
......
......@@ -129,12 +129,6 @@ class ProfessorMessageSerializer(serializers.ModelSerializer):
fields = ('id', 'course', 'course_name', 'course_slug', 'professor',
'users', 'subject', 'message', 'date', 'is_read', 'groups', 'classes')
def create(self):
groups = self.context['request'].data.get('groups', None)
classes = self.context['request'].data.get('classes', None)
ProfessorMessage.classes.add(*classes)
ProfessorMessage.groups.add(*groups)
def get_course_slug(self, obj):
try:
return obj.course.slug
......@@ -153,32 +147,3 @@ class ProfessorMessageSerializer(serializers.ModelSerializer):
return read_state.is_read
except ProfessorMessageRead.DoesNotExist as e:
return False
class ProfessorGlobalMessageSerializer(ProfessorMessageSerializer):
users = SimpleUserSerializer(read_only=True, required=False, many=True)
def create(self, validated_data):
all_students = self.context['request'].data.get('all_students', None)
recipients = self.context['request'].data.get('users', None)
groups = self.context['request'].data.get('groups', None)
classes = self.context['request'].data.get('classes', None)
validated_data['professor'] = self.context['request'].user
global_message = ProfessorMessage(**validated_data)
global_message.save()
User = get_user_model()
if all_students:
# If all_students was set to True by the client, this is a global message
global_message.users.add(*[user for user in User.objects.all()])
elif groups:
# If groups were specified, their users are the recipients
global_message.users.add(*[user for user in User.objects.filter(groups__in=groups)])
elif recipients:
# Otherwise, user the recipients list
for user_id in self.context['request'].data['users']:
global_message.users.add(User.objects.get(id=user_id))
global_message.send()
return global_message
from django.db.models import Q
from django.db import IntegrityError
from django.contrib.auth import get_user_model
from rest_framework import viewsets, mixins
from rest_framework.response import Response
......@@ -8,7 +9,6 @@ from rest_framework.permissions import IsAuthenticated
from .models import (Course, CourseProfessor, ProfessorMessage, ProfessorMessageRead)
from .serializers import (CourseSerializer, BasicCourseProfessorSerializer,
ProfessorMessageSerializer, ProfessorMessageReadSerializer,
ProfessorGlobalMessageSerializer,
)
from .permissions import (IsProfessorCoordinatorOrAdminPermissionOrReadOnly, IsAssistantOrCoordinatorOrReadOnly, IsAdminOrReadOnly)
......@@ -95,15 +95,21 @@ class ProfessorMessageViewSet(viewsets.ModelViewSet):
permission_classes = (IsAssistantOrCoordinatorOrReadOnly,)
def perform_create(self, serializer):
groups = self.request.groups
classes = self.request.classes
print("groups", groups)
print("classes", classes)
obj = serializer.save(professor=self.request.user)
recipients = serializer.context['request'].data.get('users', None)
groups = serializer.context['request'].data.get('groups', None)
users_to_be_added = []
User = get_user_model()
# If groups were specified, their users are the recipients
if groups:
users_to_be_added = User.objects.filter(groups__in=groups)
# Otherwise, user the recipients list
elif recipients:
for user_id in serializer.context['request'].data['users']:
users_to_be_added.append(User.objects.get(id=user_id))
obj = serializer.save(professor=self.request.user, users=users_to_be_added)
if obj:
obj.groups.set(groups)
obj.classes.set(classes)
obj.send()
def get_queryset(self):
......@@ -128,9 +134,22 @@ class ProfessorGlobalMessageViewSet(mixins.CreateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
model = ProfessorMessage
serializer_class = ProfessorGlobalMessageSerializer
serializer_class = ProfessorMessageSerializer
permission_classes = (IsAdminOrReadOnly,)
def perform_create(self, serializer):
User = get_user_model()
all_students = serializer.context['request'].data.get('all_students', None)
users_to_be_added = []
if all_students:
# If all_students was set to True by the client, this is a global message
users_to_be_added = User.objects.all()
obj = serializer.save(professor=self.request.user, users=users_to_be_added)
if obj:
obj.send()
def get_queryset(self):
# Get all admin messages sent using this view
queryset = ProfessorMessage.objects.filter(course=None).order_by('-id')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment