예제 #1
0
def change_username(request):
    available_namechanges = get_available_namechanges_data(request.user)
    if not available_namechanges['changes_left']:
        return Response(
            {
                'username': [_("You can't change your username at this time.")],
                'next_change_on': available_namechanges['next_change_on'],
            },
            status=400,
        )

    serializer = ChangeUsernameSerializer(
        data=request.data,
        context={'user': request.user},
    )

    serializer.is_valid(raise_exception=True)

    try:
        serializer.change_username(changed_by=request.user)

        response_data = get_available_namechanges_data(request.user)
        response_data.update({
            'username': request.user.username,
            'slug': request.user.slug,
        })

        return Response(response_data)
    except IntegrityError:
        return Response(
            {
                'username': [_("Please try again.")],
            },
            status=400,
        )
예제 #2
0
def username_endpoint(request):
    if request.method == 'POST':
        return change_username(request)
    else:
        form_options = get_available_namechanges_data(request.user)
        form_options.update({
            'length_min': settings.username_length_min,
            'length_max': settings.username_length_max,
        })
        
        return Response(form_options)
예제 #3
0
    def test_username_changes_helper(self):
        """username changes are tracked correctly"""
        test_user = UserModel.objects.create_user('Bob', '*****@*****.**',
                                                  'pass123')

        namechanges = get_available_namechanges_data(test_user)
        self.assertEqual(namechanges, {
            'changes_left': 2,
            'next_change_on': None,
        })

        self.assertEqual(test_user.namechanges.count(), 0)

        test_user.set_username('Boberson')
        test_user.save(update_fields=['username', 'slug'])

        namechanges = get_available_namechanges_data(test_user)
        self.assertEqual(namechanges, {
            'changes_left': 1,
            'next_change_on': None,
        })

        self.assertEqual(test_user.namechanges.count(), 1)