Commit 54864a5c authored by Fernando Ribeiro's avatar Fernando Ribeiro
Browse files

WIP: current progress in part 2 of #158

parent 22181617
......@@ -123,12 +123,18 @@ 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', '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
......@@ -151,25 +157,28 @@ class ProfessorMessageSerializer(serializers.ModelSerializer):
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))
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
......@@ -95,8 +95,15 @@ 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)
if obj:
obj.groups.set(groups)
obj.classes.set(classes)
obj.send()
def get_queryset(self):
......
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