Backend (Django 4.2 + DRF): - accounts: phone OTP login, JWT, profile, addresses - catalog: categories, products, images, filters, search, hits/new/related - orders: creation with Idempotency-Key, stock reservation, status history, cancel - loyalty: bonus accounts, promo codes, gift certificates - payments: payment records and YooKassa webhook handling - content: static pages, banners, site settings - integration: 1C exchange (catalog sync, order push via outbox, statuses, bonuses) - common: error envelope, pagination, request-id, integration auth, admin dashboard Infrastructure: Dockerfile, docker-compose (Postgres, Redis, Celery worker/beat, Nginx), requirements split by environment, OpenAPI contracts for site and 1C, architecture doc. Tests: 22 pytest cases covering catalog, orders, idempotency, OTP, integration.
126 lines
4.1 KiB
Python
126 lines
4.1 KiB
Python
from django.conf import settings
|
|
from rest_framework import status
|
|
from rest_framework.exceptions import ValidationError
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
from rest_framework.viewsets import ModelViewSet
|
|
from rest_framework_simplejwt.tokens import RefreshToken
|
|
|
|
from .models import Address
|
|
from .serializers import (
|
|
AddressSerializer,
|
|
OtpRequestSerializer,
|
|
OtpVerifySerializer,
|
|
PasswordSetSerializer,
|
|
ProfileUpdateSerializer,
|
|
UserSerializer,
|
|
)
|
|
from .services import request_otp, verify_otp
|
|
|
|
|
|
class OtpRequestView(APIView):
|
|
permission_classes = [AllowAny]
|
|
throttle_scope = "otp"
|
|
serializer_class = OtpRequestSerializer
|
|
|
|
def post(self, request):
|
|
serializer = OtpRequestSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
otp, code = request_otp(serializer.validated_data["phone"])
|
|
|
|
payload = {
|
|
"detail": "Код отправлен",
|
|
"expires_in": settings.OTP_TTL_SECONDS,
|
|
}
|
|
if settings.OTP_EXPOSE_DEBUG_CODE and settings.OTP_DEBUG_CODE:
|
|
payload["debug_code"] = code
|
|
return Response(payload, status=status.HTTP_200_OK)
|
|
|
|
|
|
class OtpVerifyView(APIView):
|
|
permission_classes = [AllowAny]
|
|
throttle_scope = "otp"
|
|
serializer_class = OtpVerifySerializer
|
|
|
|
def post(self, request):
|
|
serializer = OtpVerifySerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
user = verify_otp(
|
|
serializer.validated_data["phone"], serializer.validated_data["code"]
|
|
)
|
|
|
|
refresh = RefreshToken.for_user(user)
|
|
return Response(
|
|
{
|
|
"access": str(refresh.access_token),
|
|
"refresh": str(refresh),
|
|
"user": UserSerializer(user).data,
|
|
}
|
|
)
|
|
|
|
|
|
class MeView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = UserSerializer
|
|
|
|
def get(self, request):
|
|
return Response(UserSerializer(request.user).data)
|
|
|
|
def patch(self, request):
|
|
serializer = ProfileUpdateSerializer(
|
|
request.user, data=request.data, partial=True
|
|
)
|
|
serializer.is_valid(raise_exception=True)
|
|
serializer.save()
|
|
return Response(UserSerializer(request.user).data)
|
|
|
|
|
|
class PasswordSetView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = PasswordSetSerializer
|
|
|
|
def post(self, request):
|
|
serializer = PasswordSetSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
user = request.user
|
|
|
|
if (
|
|
user.password
|
|
and user.has_usable_password()
|
|
and not user.check_password(
|
|
serializer.validated_data.get("current_password", "")
|
|
)
|
|
):
|
|
raise ValidationError({"current_password": "Неверный текущий пароль"})
|
|
|
|
user.set_password(serializer.validated_data["new_password"])
|
|
user.save(update_fields=["password"])
|
|
return Response({"detail": "Пароль обновлён"})
|
|
|
|
|
|
class AddressViewSet(ModelViewSet):
|
|
serializer_class = AddressSerializer
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
if getattr(self, "swagger_fake_view", False):
|
|
return Address.objects.none()
|
|
if not self.request.user.is_authenticated:
|
|
return Address.objects.none()
|
|
return Address.objects.filter(user=self.request.user)
|
|
|
|
def perform_create(self, serializer):
|
|
if serializer.validated_data.get("is_default"):
|
|
Address.objects.filter(user=self.request.user, is_default=True).update(
|
|
is_default=False
|
|
)
|
|
serializer.save(user=self.request.user)
|
|
|
|
def perform_update(self, serializer):
|
|
if serializer.validated_data.get("is_default"):
|
|
Address.objects.filter(user=self.request.user, is_default=True).exclude(
|
|
pk=serializer.instance.pk
|
|
).update(is_default=False)
|
|
serializer.save()
|