Commit 822beebe authored by Fernando Ribeiro's avatar Fernando Ribeiro
Browse files

Merge branch 'develop' into 'master'

Develop into master

See merge request !8
parents df5ccf2f 3620fed5
# Generated by Django 2.2.12 on 2020-06-08 14:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auth', '0011_update_proxy_permissions'),
('courses', '0007_auto_20200512_2349'),
]
operations = [
migrations.AddField(
model_name='professormessage',
name='classes',
field=models.ManyToManyField(blank=True, related_name='message_courses', to='courses.Class', verbose_name='Default Class'),
),
migrations.AddField(
model_name='professormessage',
name='groups',
field=models.ManyToManyField(blank=True, help_text='The Groups that will receive this message.', related_name='message_groups', to='auth.Group', verbose_name='groups'),
),
]
# 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'),
......@@ -648,8 +649,31 @@ class ProfessorMessage(models.Model):
verbose_name=_('Course'),
null=True,
)
groups = models.ManyToManyField(
Group,
verbose_name=_('groups'),
blank=True,
help_text=_('The Groups that will receive this message.'),
related_name="message_groups",
)
classes = models.ManyToManyField(
'Class',
verbose_name=_('Default Class'),
related_name='message_courses',
blank=True,
)
def send(self):
# bcc = list with all destinataries
# batch = bcc.split( constant )
# for each batch ->
# send message
# save status
# if it goes wrong
# split batch by 2 and try it again (binary search)
# check if everything is sent
bcc = [u.email for u in self.users.all() if u.is_active and re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", u.email)]
try:
et = EmailTemplate.objects.get(name='professor-message')
......
......@@ -123,11 +123,11 @@ class ProfessorMessageSerializer(serializers.ModelSerializer):
course_slug = serializers.SerializerMethodField(read_only=True)
course_name = serializers.SerializerMethodField(read_only=True)
is_read = serializers.SerializerMethodField(read_only=True)
class Meta:
model = ProfessorMessage
fields = ('id', 'course', 'course_name', 'course_slug', 'professor',
'users', 'subject', 'message', 'date', 'is_read')
'users', 'subject', 'message', 'date', 'is_read', 'groups', 'classes')
def get_course_slug(self, obj):
try:
......@@ -147,33 +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)
groups = self.context['request'].data.get('groups', None)
recipients = self.context['request'].data.get('users', 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
from rest_framework.permissions import IsAuthenticated
from .models import (Course, CourseProfessor, ProfessorMessage, ProfessorMessageRead)
from .models import (Course, CourseProfessor, ProfessorMessage, ProfessorMessageRead, Class)
from .serializers import (CourseSerializer, BasicCourseProfessorSerializer,
ProfessorMessageSerializer, ProfessorMessageReadSerializer,
ProfessorGlobalMessageSerializer,
)
from .permissions import (IsProfessorCoordinatorOrAdminPermissionOrReadOnly, IsAssistantOrCoordinatorOrReadOnly, IsAdminOrReadOnly)
......@@ -95,7 +95,27 @@ class ProfessorMessageViewSet(viewsets.ModelViewSet):
permission_classes = (IsAssistantOrCoordinatorOrReadOnly,)
def perform_create(self, serializer):
obj = serializer.save(professor=self.request.user)
recipients = serializer.context['request'].data.get('users', None)
groups = serializer.context['request'].data.get('groups', None)
classes = serializer.context['request'].data.get('classes', 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))
elif classes:
for class_id in classes:
klass = Class.objects.get(id=class_id)
for user in klass.students.all():
users_to_be_added.append(user.id)
obj = serializer.save(professor=self.request.user, users=users_to_be_added)
if obj:
obj.send()
......@@ -121,9 +141,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