Last active
July 4, 2018 22:26
-
-
Save fhdez/4c09ff2090e75f8e2b8871eb3bffc390 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotificationReadSerializer(ModelSerializer): | |
receiver = serializers.SerializerMethodField() | |
was_read = serializers.SerializerMethodField() | |
class Meta: | |
model = ReceiverMessage | |
fields = [ | |
'id', | |
'receiver', | |
'was_read' | |
] | |
def get_receiver(self, instance): | |
users = [] | |
groups = [] | |
for receiver in instance.receivers.all(): | |
if receiver.user: | |
users.append(UserParentUpdateSerializer(receiver.user).data) | |
if receiver.group: | |
groups.append(GroupSerializer(receiver.group).data) | |
return { | |
'users': users, | |
'groups': groups | |
} | |
def get_was_read(self, instance): | |
children = self.context['request'].user.children.all() | |
children_ids = children.values_list('id', flat=True) | |
children_group_ids = children.values_list( | |
'user_groups__group__id', flat=True | |
) | |
receivers = instance.receivers.filter( | |
Q( | |
user_id__in=children_ids | |
) | | |
Q( | |
group_id__in=children_group_ids | |
) | |
) | |
was_read = False | |
for receiver in receivers: | |
was_read = receiver.was_read | |
return was_read | |
router.register_nested( | |
r"director-messages", | |
r"read", | |
NotificationReadViewSet, | |
parent_lookup_name='director-messages', | |
base_name='read' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment