def post(self, request): data = JSONParser().parse(request) print(data) # Serialize data into user serializer = UserSerializer(data=data) # try: # serializer.is_valid() # print("here") # except ValidationError as e: # print("caught") # print(e) # # return HttpResponse(status=400) if serializer.is_valid(): instance = serializer.save() # Try obtain auth token here and return it after user and token generated token = Token.objects.get(user=instance) # token is a Token instance token_value = getattr(token, "key") return JsonResponse({'token': token_value}, status=201) else: # Not sure how to handle errors here yet # print(serializer.errors['password'][0]) print(serializer.errors['password']) # Add custom message or error code in here? # 409 Conflict for existing user return HttpResponse(serializer.errors, status=400)
def post(self, request, format=None): serializer = UserSerializer(data=request.data) if serializer.is_valid(): serializer.save() user = User.objects.get(username=serializer.data['username']) jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) response_dict = {'token': token} return Response(response_dict, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class WishlistSerializer(serializers.ModelSerializer): user = UserSerializer() book = BookSerializer() class Meta: model = Wishlist fields = ('id', 'user', 'book', 'has_read')
class QuoteSerializer(serializers.ModelSerializer): user = UserSerializer() book = BookSerializer() class Meta: model = Quote fields = ('id', 'quote', 'user', 'book')
def to_representation(self, instance): representation = super().to_representation(instance) user_query_set = User.objects.filter(id=instance.user.id) representation['user'] = UserSerializer(user_query_set, many=True).data[0] return representation
class ReviewSerializer(serializers.ModelSerializer): user = UserSerializer() class Meta: model = Review fields = ( 'user', 'book', 'review', )
def getFollowing(request): if (request.method == 'GET'): user = request.user following = Follows.get_following(user.profile) serialized = UserSerializer(following, many=True) return JsonResponse(serialized.data, status=status.HTTP_200_OK, safe=False) return HttpResponse(status=400)
def getFollowers(request): if (request.method == 'GET'): user = request.user followers = [i.target_user for i in user.followed_by.all()] serialized = UserSerializer(followers, many=True) return JsonResponse(serialized.data, status=status.HTTP_200_OK, safe=False) return HttpResponse(status=400)
class CommentSerializer(serializers.ModelSerializer): author = UserSerializer() class Meta: model = Comment fields = ( 'id', 'content', 'publish_date', 'author', 'post', )
class BazaarBookSerializer(serializers.ModelSerializer): user = UserSerializer() book = BookSerializer() class Meta: model = BazaarBook fields = ( 'id', 'book', 'user', 'price', 'publish_date', )
class PostSerializer(serializers.ModelSerializer): author = UserSerializer() comments = CommentSerializer(many=True) upvotes = UpvoteSerializer(many=True) class Meta: model = Post fields = ( 'id', 'title', 'content', 'publish_date', 'author', 'comments', 'upvotes', )
def to_representation(self, instance): representation = super().to_representation(instance) user_query_set = User.objects.filter(id=instance.user.id) representation['user'] = UserSerializer(user_query_set, many=True).data[0] start_location_query_set = Location.objects.filter( id=instance.startLocation.id) representation['startLocation'] = LocationSerializer( start_location_query_set, many=True).data[0] end_location_query_set = Location.objects.filter( id=instance.endLocation.id) representation['endLocation'] = LocationSerializer( end_location_query_set, many=True).data[0] return representation
def post(self, request): data = request.data serializer = UserLoginSerializer(data=data) if serializer.is_valid(): user = serializer.validated_data['user'] login(request, user) new_data = UserSerializer(user, fields=[ 'title', 'first_name', 'last_name', 'email', 'phone_number', 'profile_pic', 'bool_subscriptions_job', 'bool_relevant_info', 'isPaidUser' ]).data response = { 'status': True, 'message': 'Logged in successfully', 'data': [new_data] } return Response(response, status=status.HTTP_200_OK) response = {'status': False, 'message': 'Email or Password incorrect.'} return Response(response, status=status.HTTP_200_OK)
class AnnotationListSerializer(serializers.ModelSerializer): user = UserSerializer(read_only=True) class Meta: model = Annotation fields = '__all__'
class CommentSerializer(serializers.ModelSerializer): author = UserSerializer() class Meta: model = Comment fields = ('comment', 'author', 'photo')
class LikeSerializer(serializers.ModelSerializer): user = UserSerializer() class Meta: model = Like fields = ('photo', 'user')