def send_pickup_collector_updates(sender, instance, **kwargs): pickup = instance.pickupdate payload = PickupDateSerializer(pickup).data for subscription in ChannelSubscription.objects.recent().filter( user__in=pickup.store.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='pickups:pickupdate', payload=payload)
def send_feedback_possible_updates(sender, instance, **kwargs): pickup = instance payload = PickupDateSerializer(pickup).data for subscription in ChannelSubscription.objects.recent().filter( user__in=pickup.collectors.all()): send_in_channel(subscription.reply_channel, topic='pickups:feedback_possible', payload=payload)
def send_pickup_collector_updates(sender, instance, **kwargs): action = kwargs.get('action') if action and (action == 'post_add' or action == 'post_remove'): pickup = instance payload = PickupDateSerializer(pickup).data for subscription in ChannelSubscription.objects.recent().filter( user__in=pickup.store.group.members.all()): send_in_channel(subscription.reply_channel, topic='pickups:pickupdate', payload=payload)
def send_pickup_updates(sender, instance, **kwargs): pickup = instance if pickup.feedback_possible: # doesn't change serialized data return payload = PickupDateSerializer(pickup).data for subscription in ChannelSubscription.objects.recent().filter( user__in=pickup.store.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='pickups:pickupdate', payload=payload)
def send_pickup_updates(sender, instance, **kwargs): pickup = instance if pickup.done_and_processed: # doesn't change serialized data return payload = PickupDateSerializer(pickup).data for subscription in ChannelSubscription.objects.recent().filter( user__in=pickup.store.group.members.all()): if not pickup.deleted: send_in_channel(subscription.reply_channel, topic='pickups:pickupdate', payload=payload) else: send_in_channel(subscription.reply_channel, topic='pickups:pickupdate_deleted', payload=payload)
def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) \ .select_related('about') \ .prefetch_related('about__collectors') feedback = self.paginate_queryset(queryset) pickups = set(f.about for f in feedback) serializer = self.get_serializer(feedback, many=True) context = self.get_serializer_context() pickups_serializer = PickupDateSerializer(pickups, many=True, context=context) return self.get_paginated_response({ 'feedback': serializer.data, 'pickups': pickups_serializer.data, })
def list(self, request, *args, **kwargs): queryset = self.get_queryset() \ .exclude(latest_message_id=None) \ .annotate_unread_message_count_for(self.request.user) \ .select_related( 'latest_message', 'target_type', ) \ .prefetch_related( 'latest_message__reactions', 'participants', 'conversationparticipant_set', ) queryset = self.filter_queryset(queryset) conversations = self.paginate_queryset(queryset) messages = [ c.latest_message for c in conversations if c.latest_message is not None ] # Prefetch related objects per target type pickup_ct = ContentType.objects.get_for_model(PickupDate) pickup_conversations = [ item for item in conversations if item.target_type == pickup_ct ] pickups = PickupDate.objects. \ filter(id__in=[c.target_id for c in pickup_conversations]). \ prefetch_related('collectors') applications_ct = ContentType.objects.get_for_model(GroupApplication) application_conversations = [ item for item in conversations if item.target_type == applications_ct ] applications = GroupApplication.objects. \ filter(id__in=[c.target_id for c in application_conversations]). \ select_related('user') # Applicant does not have access to group member profiles, so we sideload reduced user profiles my_applications = [a for a in applications if a.user == request.user] def get_conversation(application): return next(c for c in application_conversations if c.target_id == application.id) users = get_user_model().objects. \ filter(conversationparticipant__conversation__in=[get_conversation(a) for a in my_applications]). \ exclude(id=request.user.id) context = self.get_serializer_context() serializer = self.get_serializer(conversations, many=True) message_serializer = ConversationMessageSerializer(messages, many=True, context=context) pickups_serializer = PickupDateSerializer(pickups, many=True, context=context) application_serializer = GroupApplicationSerializer(applications, many=True, context=context) user_serializer = UserInfoSerializer(users, many=True, context=context) return self.get_paginated_response({ 'conversations': serializer.data, 'messages': message_serializer.data, 'pickups': pickups_serializer.data, 'applications': application_serializer.data, 'users_info': user_serializer.data, })