from django.shortcuts import render

from rest_framework import viewsets

from chat.models import Member
from chat.models import ChatRoom
from chat.serializers import MemberSerializer
from chat.serializers import ChatRoomSerializer


class MemberViewSet(viewsets.ModelViewSet):
    model = Member
    serializer_class = MemberSerializer


class ChatRoomViewSet(viewsets.ModelViewSet):
    model = ChatRoom
    serializer_class = ChatRoomSerializer

from django.shortcuts import render
from django.shortcuts import get_object_or_404

from rest_framework import viewsets
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response

from chat.models import Member
from chat.models import ChatRoom
from chat.serializers import MemberSerializer
from chat.serializers import ChatRoomSerializer


class MemberViewSet(viewsets.ModelViewSet):
    model = Member
    serializer_class = MemberSerializer


class ChatRoomViewSet(viewsets.ModelViewSet):
    model = ChatRoom
    serializer_class = ChatRoomSerializer

    @action()
    def add_member(self, request, pk=None):
        chat_room = self.get_object()
        if 'lrz_id' not in request.DATA:
            # Invalid request
            return Response(status=status.HTTP_400_BAD_REQUEST)

        member = get_object_or_404(Member, lrz_id=request.DATA['lrz_id'])
        chat_room.members.add(member)

        return Response({
            'status': 'success',
        })

Add an action for adding members to a chat room
Add an action for adding members to a chat room

Even though django-rest-framework supports a Ruby-on-Rails style
of updating existing resources by issuing a PATCH or PUT request,
such updates are unsafe and can cause race-conditions to lose
some state. The implementation of this action isn't fully RESTful,
but neither is the PATCH-style used here, so it is still a good
choice for solving a possible race condition situation.
