def update(self, request, *args, **kwargs): participants_ids = compat_get_request_data(self.request).getlist('participants', []) if len(participants_ids) > 0: # we warn the user he cannot update the participants here return Response("Participant updates not allowed by this method.", status=status.HTTP_400_BAD_REQUEST) partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=compat_get_request_data(request), partial=partial) compat_serializer_check_is_valid(serializer) try: self.perform_update(serializer) except: compat_perform_update(self, serializer) return Response(serializer.data)
def create(self, request, *args, **kwargs): """ We ensure the Thread only involves eligible participants. """ serializer = self.get_serializer(data=compat_get_request_data(request)) compat_serializer_check_is_valid(serializer) self.perform_create(request, serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def add_participants(self, request, pk=None): # we get the thread and check for permission thread = Thread.objects.get(id=pk) self.check_object_permissions(request, thread) # we get the participants and add them participants_ids = json.loads(compat_get_request_data(self.request).get('participants')) thread.add_participants(request, *participants_ids) # we return the thread serializer = self.get_serializer(thread) return Response(serializer.data)
def remove_participant(self, request, pk=None): # we get the thread and check for permission thread = Thread.objects.get(id=pk) self.check_object_permissions(request, thread) # we get the participant participant_id = compat_get_request_data(self.request).get('participant') participant = Participant.objects.get(id=participant_id) # we remove him if thread.remove_participant allows us to try: thread.remove_participant(request, participant) # we return the thread serializer = self.get_serializer(thread) return Response(serializer.data) except Exception: return Response(status=status.HTTP_400_BAD_REQUEST)
def post_message(self, request, pk=None): """ Pk is the pk of the Thread to which the message belongs. """ # we get the thread and check for permission thread = Thread.objects.get(id=pk) self.check_object_permissions(request, thread) # we get the body body = compat_get_request_data(self.request).get('body') # we create the message # Message.objects.save() could return an Exception try: message = Message(sender=request.rest_messaging_participant, thread=thread, body=body) message.save() serializer = SimpleMessageSerializer(message) return Response(serializer.data, status=status.HTTP_201_CREATED) except Exception: return Response(status=status.HTTP_412_PRECONDITION_FAILED)
def perform_create(self, request, serializer): participants_ids = json.loads(compat_get_request_data(self.request).get('participants')) thread = Thread.managers.get_or_create_thread(self.request, compat_get_request_data(self.request).get('name'), *participants_ids) setattr(serializer, compat_thread_serializer_set(), thread)