def put(self, request, pk, format=None): user = self.get_object(pk) serializer = UserSerializer(user, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class HomeFeedSerializer(serializers.Serializer): """ Read Only Endpoint for HomeFeed Support nested Serializer """ id = serializers.IntegerField(read_only=True) feed_type = serializers.IntegerField() picker = UserSerializer(read_only=True, required=False) requester = UserSerializer(read_only=True, required=False) university = UniversitySerializer(read_only=True, required=False) flight_pick_request = FlightPickRequestListSerializer(read_only=True, required=False) pick_request = PickRequestListSerializer(read_only=True, required=False) price = serializers.IntegerField(required=False) start = serializers.CharField(max_length=200, required=False) flight = serializers.CharField(max_length=30, required=False) date_time = serializers.DateTimeField(required=False) destination = serializers.CharField(max_length=200, required=False) bags = serializers.IntegerField(required=False) description = serializers.CharField(required=False) confirmed = serializers.BooleanField(required=False) created = serializers.DateTimeField(required=False)
def post(self, request, format=None): print(request.data) serializer = UserSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class MainBlogSerializer(serializers.ModelSerializer): tags = TagSerializer(many=True) author = UserSerializer(many=True) section = SectionSerializer() class Meta: model = Blog fields = ['id', 'created', 'modified', 'title', 'slugtitle', 'body', 'image', 'description', 'authorlock', 'author', 'section', 'tags']
def get(self, request, message_type, format=None): user = request.user message_type = int(message_type) # retrieve all the messages the user send or receive result = [] unread_messages = MessageUnread.objects.filter(reader=user) unread_message_set = set() for unread_message in unread_messages: unread_message_set.add(unread_message.message_target.id) messages = []; if message_type == RECEIVED_MESSAGE: message_receives = MessageReceive.objects.filter(receiver=user).order_by('-created') for receive in message_receives: messages.append(receive.message_target) if message_type == SENT_MESSAGE: messages = Message.objects.filter(sender=user).order_by('-created') for message in messages: sender_serializer = UserSerializer(message.sender) unread = message.id in unread_message_set result.append({ 'id' : message.id, 'sender' : sender_serializer.data, 'message' : message.message, 'unread' : unread, 'created' : message.created, }) return Response(result, status=status.HTTP_200_OK)
def get(self, request, format=None): users = User.objects.all() serializer = UserSerializer(users, many=True) print(request.session) print(request.session.keys()) print(request.session.items()) #print (serializer.data) return Response(serializer.data)
def user_profile(request): response = {} response["status"] = message.SUCCESS print('You hit user profile') try: if request.method == "GET": user = request.user serializer = UserSerializer(user) print(serializer.data) response["data"] = serializer.data elif request.method == "PUT": data = request.data print(data) serializer = UserSerializer(request.user, data=data) if serializer.is_valid(): serializer.save() response["data"] = serializer.data response["message"] = message.UPDATE_USER_SUCCESS else: response["status"] = message.ERROR response["message"] = serializer.errors except Exception as exp: print(exp) traceback.print_exc() response["status"] = message.ERROR response["message"] = str(exp) if response["status"] == message.SUCCESS: return JSONResponse(response, status=status.HTTP_200_OK) else: return JSONResponse(response, status=status.HTTP_400_BAD_REQUEST)
def custom_jwt_response_handler(token, user=None, request=None): print("gets some :", request) # manages data return on login or signup return { 'token': token, # TODO: Return chat rooms avaliable to use in UserSerializer object 'user': UserSerializer(user, context={ 'request': request }).data }
class FlightPickRequestListSerializer(ModelSerializer): """ Read Only Endpoint for FlightPickRequest Support nested Serializer """ requester = UserSerializer(read_only=True) university = UniversitySerializer(read_only=True) class Meta: model = FlightPickRequest
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.validated_data return Response({ "user": UserSerializer(user, context=self.get_serializer_context()).data, "token": AuthToken.objects.create(user)[1] })
class PickUpListSerializer(ModelSerializer): """ Read Only Endpoint for PickUp Support nested Serializer """ pick_request = PickRequestListSerializer(read_only=True) picker = UserSerializer(read_only=True) class Meta: model = PickUp
def post(self, request, *args, **kwargs): try: response = super(CustomObtainAuthToken, self).post(request, *args, **kwargs) token = Token.objects.get(key=response.data['token']) user = User.objects.get(id=token.user_id) data = UserSerializer(user).data return Response({'token': token.key, 'user': data}) except: return Response({'error': "Could not login with credentials"}, status=status.HTTP_403_FORBIDDEN)
def get(self, request, message_id, format=None): result = [] replies = MessageReply.objects.filter(message_target_id=message_id).order_by('created') for reply in replies: sender_serializer = UserSerializer(reply.sender) result.append({ 'id' : reply.id, 'sender' : sender_serializer.data, 'message' : reply.message, 'created' : reply.created, }) return Response(result, status=status.HTTP_200_OK)
class ArticleSerializer(serializers.ModelSerializer): url = serializers.CharField(source='get_absolute_url') author = UserSerializer() category = CategorySerializer() comments_count = serializers.IntegerField() like_status = serializers.SerializerMethodField(method_name='get_like_status') def get_like_status(self, obj) -> str: user = self.context['request'].user if not user.is_authenticated: return LikeIconStatus.CANCELED if like_obj := obj.votes.filter(user=user).first(): return LikeIconStatus.LIKED if like_obj.vote == LikeStatus.LIKE else LikeIconStatus.DISLIKED return LikeIconStatus.CANCELED
def get(self, request): serializer = UserSerializer(request.user) return response.Response(serializer.data)
def get(self, request, pk, format=None): user = self.get_object(pk) serializer = UserSerializer(user) return Response(serializer.data)