コード例 #1
0
def get_info(request, id):
    user = UserProfile.objects.filter(id=id).first()
    if user is not None:  # user of given id exists
        user_stats = Game.get_user_stats(user)
        return Response(user_stats, status=200)
    else:  # user of given id doesn't exist
        return Response({'error': "User of given id doesn't exist."},
                        status=404)
コード例 #2
0
def login_user(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:  # authentication success
        login(request, user)
        user_profile = UserProfile.objects.filter(username=username).first()
        user_stats = Game.get_user_stats(user=user_profile)
        return Response(user_stats, status=200)
    else:  # authentication failed
        return Response({'error': 'Authentication failed.'}, status=400)
コード例 #3
0
def register(request):
    username = request.POST['username']
    password = request.POST['password']
    # check if user of given username exists
    if UserProfile.is_username_taken(username=username):
        return Response({'error': 'This username is already taken'},
                        status=400)
    else:
        user = UserProfile.objects.create_user(username=username,
                                               password=password)
        # get stats of created user
        user_stats = Game.get_user_stats(user)
    return Response(user_stats, status=201)
コード例 #4
0
def get_about_me(request):
    if request.user.is_authenticated():
        # PATCH = update username
        if request.method == "PATCH":
            serializer = UserProfileSerializer(
                request.user,
                request.data,
                partial=True,
            )
            if serializer.is_valid():
                serializer.save()
        # generate user's data (username + stats)
        user_stats = Game.get_user_stats(request.user)
        return Response(user_stats, status=200)
    else:
        return Response(
            {'detail': 'Authentication credentials were not provided.'},
            status=403)