def post(self, request, version, format=None): serializer = RoomSerializer(data=request.data, context={'request': request}) if serializer.is_valid(): serializer.save() return Response(serializer.validated_data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, version, pk, format=None): room = get_object_or_404(Room, pk=pk) serializer = RoomSerializer(room, data=request.data, context={'request': request}) if serializer.is_valid(): serializer.save() return Response(serializer.validated_data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, version, format=None): rooms = Room.objects.all() serializer = RoomSerializer(rooms, many=True, context={'request': request}) return Response(serializer.data, status=status.HTTP_200_OK)
def unlock(request, version, pk, format=None): room = get_object_or_404(Room, pk=pk) sender = request.user try: check_rooming(sender, sender) room.unlock(sender) except exceptions.ValidationError as e: return Response('; '.join(e.messages), status=status.HTTP_403_FORBIDDEN) return Response(RoomSerializer(room).data, status=status.HTTP_200_OK)
def leave(request, version, pk, format=None): room = get_object_or_404(Room, pk=pk) sender = request.user serializer = LeaveMethodSerializer(data=request.data) if serializer.is_valid(): user_id = serializer.validated_data.get("user") user = get_object_or_404(User, pk=user_id) try: check_rooming(user, sender) room.leave(user) except exceptions.ValidationError as e: return Response("; ".join(e.messages), status=status.HTTP_403_FORBIDDEN) return Response(RoomSerializer(room).data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def lock(request, version, pk, format=None): # only locks the room room = get_object_or_404(Room, pk=pk) sender = request.user serializer = LockMethodAdminSerializer(data=request.data) \ if sender.is_staff else LockMethodSerializer(data=request.data) if serializer.is_valid(): user_id = serializer.validated_data.get("user") expiration_date = serializer.validated_data.get("expiration_date") user = get_object_or_404(User, pk=user_id) try: check_rooming(user, sender) room.set_lock(user, sender, expiration_date) except exceptions.ValidationError as e: return Response('; '.join(e.messages), status=status.HTTP_403_FORBIDDEN) return Response(RoomSerializer(room).data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def leave(request, version, pk): room = get_object_or_404(Room, pk=pk) sender = request.user serializer = LeaveMethodSerializer(data=request.data) if serializer.is_valid(): user_id = serializer.validated_data.get("user") user = User.objects.filter(pk=user_id).first() if not user: return Response(_("Specified user does not exist."), status=status.HTTP_400_BAD_REQUEST) try: _check_rooming(user, sender) room.leave(user, sender) except exceptions.ValidationError as e: return Response("; ".join(e.messages), status=status.HTTP_403_FORBIDDEN) return Response(RoomSerializer(room).data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def join(request, version, pk): # only room joining room = get_object_or_404(Room, pk=pk) sender = request.user serializer = JoinMethodSerializer(data=request.data) if serializer.is_valid(): user_id = serializer.validated_data.get("user") password = serializer.validated_data.get("password") user = User.objects.filter(pk=user_id).first() if not user: return Response("No such user", status=status.HTTP_400_BAD_REQUEST) try: _check_rooming(user, sender) room.join(user, sender, password) except exceptions.ValidationError as e: return Response("; ".join(e.messages), status=status.HTTP_403_FORBIDDEN) return Response(RoomSerializer(room).data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, version, pk, format=None): room = get_object_or_404(Room, pk=pk) serializer = RoomSerializer(room, context={'request': request}) return Response(serializer.data, status=status.HTTP_200_OK)
def unhide(request, version, pk, format=None): room = get_object_or_404(Room, pk=pk) room.hidden = False room.save() return Response(RoomSerializer(room).data, status=status.HTTP_200_OK)