backend - edit main tab
This commit is contained in:
@@ -4,10 +4,12 @@ from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.core.validators import validate_email
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from api.auth.serializers import UserResponseSerializer
|
||||
|
||||
from api.models import UserProfile
|
||||
|
||||
from api.utils.decorators import handle_exceptions
|
||||
|
||||
class UserDataView(ViewSet):
|
||||
@@ -32,4 +34,52 @@ class UserDataView(ViewSet):
|
||||
return Response(
|
||||
{"error": "User profile not found"},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
)
|
||||
|
||||
class AccountActions(ViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@action(detail=False, methods=['patch'])
|
||||
@handle_exceptions
|
||||
|
||||
def change_data_main_tab(self, request):
|
||||
"""Обновление данных на главной странице аккаунта"""
|
||||
|
||||
user = request.user
|
||||
user_profile = get_object_or_404(UserProfile, user=user)
|
||||
|
||||
# обновляем данные пользователя
|
||||
if 'firstName' in request.data:
|
||||
user.first_name = request.data['firstName']
|
||||
if 'lastName' in request.data:
|
||||
user.last_name = request.data['lastName']
|
||||
if 'email' in request.data:
|
||||
email = request.data['email']
|
||||
validate_email(email) # handle_exceptions обработает ValidationError
|
||||
user.email = email
|
||||
user.username = email
|
||||
|
||||
# обновляем номер телефона
|
||||
if 'phone_number' in request.data:
|
||||
phone = request.data['phone_number']
|
||||
if phone:
|
||||
if len(phone) < 13: # +375XXXXXXXXX
|
||||
raise ValidationError("Номер телефона слишком короткий")
|
||||
|
||||
if len(phone) > 18:
|
||||
raise ValidationError("Номер телефона слишком длинный")
|
||||
|
||||
# проверка на уникальность
|
||||
if UserProfile.objects.filter(phone_number=phone).exclude(user=user).exists():
|
||||
raise ValidationError("Этот номер телефона уже используется")
|
||||
|
||||
user_profile.phone_number = phone
|
||||
|
||||
# сохраняем изменения
|
||||
user.save()
|
||||
user_profile.save()
|
||||
|
||||
return Response({
|
||||
"message": "Данные успешно обновлены",
|
||||
"user": UserResponseSerializer(user).data
|
||||
}, status=status.HTTP_200_OK)
|
||||
@@ -14,6 +14,8 @@ class UserResponseSerializer(serializers.Serializer):
|
||||
phone_number = serializers.CharField(source='userprofile.phone_number')
|
||||
image = serializers.CharField(source='userprofile.image')
|
||||
uuid = serializers.SerializerMethodField()
|
||||
country = serializers.CharField(source='userprofile.country')
|
||||
city = serializers.CharField(source='userprofile.city')
|
||||
def get_uuid(self, obj):
|
||||
try:
|
||||
return str(obj.userprofile.uuid)[:6]
|
||||
|
||||
Reference in New Issue
Block a user