All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m17s
93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
from rest_framework import status
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from rest_framework_simplejwt.tokens import RefreshToken
|
|
from django.contrib.auth import authenticate, get_user_model
|
|
from django.utils import timezone
|
|
from django.db.models import Sum
|
|
|
|
from .serializers import RegisterSerializer, UserSerializer
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
@api_view(['POST'])
|
|
@permission_classes([AllowAny])
|
|
def register_view(request):
|
|
"""POST /api/v1/auth/register"""
|
|
serializer = RegisterSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
user = serializer.save()
|
|
|
|
refresh = RefreshToken.for_user(user)
|
|
return Response({
|
|
'user': UserSerializer(user).data,
|
|
'tokens': {
|
|
'access': str(refresh.access_token),
|
|
'refresh': str(refresh),
|
|
}
|
|
}, status=status.HTTP_201_CREATED)
|
|
|
|
|
|
@api_view(['GET', 'POST'])
|
|
@permission_classes([AllowAny])
|
|
def login_view(request):
|
|
"""GET/POST /api/v1/auth/login"""
|
|
if request.method == 'GET':
|
|
return Response({'message': 'Use POST to login', 'required_fields': ['username', 'password']})
|
|
|
|
username = request.data.get('username', '')
|
|
password = request.data.get('password', '')
|
|
|
|
# Try authenticate with username first, then email
|
|
user = authenticate(username=username, password=password)
|
|
if user is None:
|
|
# Try email login
|
|
try:
|
|
user_by_email = User.objects.get(email=username)
|
|
user = authenticate(username=user_by_email.username, password=password)
|
|
except User.DoesNotExist:
|
|
pass
|
|
|
|
if user is None:
|
|
return Response(
|
|
{'error': 'invalid_credentials', 'message': '用户名或密码错误'},
|
|
status=status.HTTP_401_UNAUTHORIZED
|
|
)
|
|
|
|
refresh = RefreshToken.for_user(user)
|
|
return Response({
|
|
'user': UserSerializer(user).data,
|
|
'tokens': {
|
|
'access': str(refresh.access_token),
|
|
'refresh': str(refresh),
|
|
}
|
|
})
|
|
|
|
|
|
@api_view(['GET'])
|
|
@permission_classes([IsAuthenticated])
|
|
def me_view(request):
|
|
"""GET /api/v1/auth/me — Phase 3: returns seconds-based quota"""
|
|
user = request.user
|
|
today = timezone.now().date()
|
|
first_of_month = today.replace(day=1)
|
|
|
|
daily_seconds_used = user.generation_records.filter(
|
|
created_at__date=today
|
|
).aggregate(total=Sum('seconds_consumed'))['total'] or 0
|
|
|
|
monthly_seconds_used = user.generation_records.filter(
|
|
created_at__date__gte=first_of_month
|
|
).aggregate(total=Sum('seconds_consumed'))['total'] or 0
|
|
|
|
data = UserSerializer(user).data
|
|
data['quota'] = {
|
|
'daily_seconds_limit': user.daily_seconds_limit,
|
|
'daily_seconds_used': daily_seconds_used,
|
|
'monthly_seconds_limit': user.monthly_seconds_limit,
|
|
'monthly_seconds_used': monthly_seconds_used,
|
|
}
|
|
return Response(data)
|