diff --git a/config/conf/celery.py b/config/conf/celery.py index 990d399..4e4054e 100644 --- a/config/conf/celery.py +++ b/config/conf/celery.py @@ -6,4 +6,5 @@ CELERY_ACCEPT_CONTENT = ['json'] CELERY_TASK_SERIALIZER = 'json' CELERY_TIMEZONE = settings.TIME_ZONE -SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') +if not env('DEBUG'): + SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') diff --git a/core/apps/accounts/cache/user.py b/core/apps/accounts/cache/user.py index 015199e..e283b66 100644 --- a/core/apps/accounts/cache/user.py +++ b/core/apps/accounts/cache/user.py @@ -7,12 +7,15 @@ from config.env import env r = redis.StrictRedis.from_url(env.str('REDIS_URL')) -def cache_user_credentials(phone_number, password, time): +def cache_user_credentials(phone_number, password, first_name, last_name, email, time): key = f"user_credentials:{phone_number}" r.hmset(key, { "phone": phone_number, - "password": password + "password": password, + "first_name": first_name, + "last_name": last_name, + "email": email }) r.expire(key, time) @@ -27,5 +30,8 @@ def get_user_creadentials(phone_number): return { "phone": data.get(b"phone").decode() if data.get(b"phone") else None, - "password": data.get(b"password").decode() if data.get(b"password") else None + "password": data.get(b"password").decode() if data.get(b"password") else None, + "first_name": data.get(b"first_name").decode() if data.get(b'first_name') else None, + "last_name": data.get(b"last_name").decode() if data.get(b'last_name') else None, + "email": data.get(b"email").decode() if data.get(b'email') else None, } \ No newline at end of file diff --git a/core/apps/accounts/models/verification_code.py b/core/apps/accounts/models/verification_code.py index 2793883..b595733 100644 --- a/core/apps/accounts/models/verification_code.py +++ b/core/apps/accounts/models/verification_code.py @@ -12,7 +12,7 @@ class VerificationCode(BaseModel): expiration_time = models.TimeField(null=True, blank=True) def __str__(self): - return f'{self.user.phone} - {self.code}' + return f'{self.phone} - {self.code}' class Meta: verbose_name = 'Verification Code' diff --git a/core/apps/accounts/serializers/auth.py b/core/apps/accounts/serializers/auth.py index 4b20947..470af4b 100644 --- a/core/apps/accounts/serializers/auth.py +++ b/core/apps/accounts/serializers/auth.py @@ -28,12 +28,20 @@ class LoginSerializer(serializers.Serializer): class RegisterSerializer(serializers.Serializer): phone = serializers.CharField() password = serializers.CharField() + first_name = serializers.CharField() + last_name = serializers.CharField() + email = serializers.EmailField() def validate_phone(self, value): if User.objects.filter(phone=value).exists(): - raise serializers.ValidationError("User exists") + raise serializers.ValidationError("User exists with this phone") return value - + + def validate_email(self, value): + if User.objects.filter(email=value).exists(): + raise serializers.ValidationError("User exists with this email") + return value + class ConfirmUserSerializer(serializers.Serializer): phone = serializers.CharField() @@ -52,24 +60,4 @@ class ConfirmUserSerializer(serializers.Serializer): class ChoiseRoleSerializer(serializers.Serializer): - role = serializers.ChoiceField(choices=ROLE_CHOICES) - - -class CompliteUserProfileSerializer(serializers.Serializer): - first_name = serializers.CharField() - last_name = serializers.CharField() - email = serializers.EmailField() - - def validate(self, data): - user = User.objects.filter(email=data.get('email')).first() - if user: - raise serializers.ValidationError({'detail': "User with this email already exists"}) - return data - - def update(self, instance, validated_data): - with transaction.atomic(): - instance.first_name = validated_data.get('first_name') - instance.last_name = validated_data.get('last_name') - instance.email = validated_data.get('email') - instance.save() - return instance \ No newline at end of file + role = serializers.ChoiceField(choices=ROLE_CHOICES) \ No newline at end of file diff --git a/core/apps/accounts/urls.py b/core/apps/accounts/urls.py index ed50c11..d33d4c0 100644 --- a/core/apps/accounts/urls.py +++ b/core/apps/accounts/urls.py @@ -1,6 +1,6 @@ from django.urls import path, include -from core.apps.accounts.views.auth import LoginApiView, RegisterApiView, ConfirUserApiView, ChoiceUserRoleApiView, CompliteUserProfileApiView +from core.apps.accounts.views.auth import LoginApiView, RegisterApiView, ConfirUserApiView, ChoiceUserRoleApiView urlpatterns = [ path('auth/', include( @@ -9,7 +9,6 @@ urlpatterns = [ path('register/', RegisterApiView.as_view(), name='register'), path('confirm_user/', ConfirUserApiView.as_view(), name='confirm-user'), path('choise_user_role/', ChoiceUserRoleApiView.as_view(), name='choise-user-role'), - path('complite_user_profile/', CompliteUserProfileApiView.as_view(), name='complite-user-profile'), ] )) ] \ No newline at end of file diff --git a/core/apps/accounts/views/auth.py b/core/apps/accounts/views/auth.py index d54b47f..9d5a575 100644 --- a/core/apps/accounts/views/auth.py +++ b/core/apps/accounts/views/auth.py @@ -37,7 +37,10 @@ class RegisterApiView(generics.GenericAPIView): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): data = serializer.validated_data - cache_user_credentials(data['phone'], data['password'], 300) + cache_user_credentials( + data['phone'], data['password'], data['first_name'], + data['last_name'], data['email'], 300 + ) user_tasks.create_and_send_sms_code.delay(data['phone']) return success_message("code is send", 200) return error_message(serializer.errors, 400) @@ -56,7 +59,10 @@ class ConfirUserApiView(generics.GenericAPIView): data = get_user_creadentials(phone) if not data: return error_message("Not found", 404) - user = User.objects.create_user(phone=data['phone']) + user = User.objects.create_user( + phone=data['phone'], first_name=data['first_name'], + last_name=data['last_name'], email=data['email'] + ) user.set_password(data['password']) user.save() confirmation.is_verify = True @@ -82,18 +88,3 @@ class ChoiceUserRoleApiView(generics.GenericAPIView): user.save() return success_message('role choices', 200) return error_message(serializer.errors, 400) - - -class CompliteUserProfileApiView(generics.GenericAPIView): - serializer_class = auth_serializer.CompliteUserProfileSerializer - queryset = User.objects.all() - - def put(self, request): - user = request.user - if user: - serializer = self.serializer_class(data=request.data, instance=user) - if serializer.is_valid(): - serializer.save() - return success_message("profile complited", 200) - return error_message(serializer.errors, 400) - return error_message("User not found", 404) \ No newline at end of file