Small changes

This commit is contained in:
2022-09-25 11:06:41 +03:00
parent 2c51925827
commit 9c27897485
5 changed files with 28 additions and 10 deletions

View File

@@ -1,9 +1,11 @@
import traceback
from account.models import PhoneVerificationService
from arka.settings import PHONE_VERIFICATION_RESEND_TIME_SECS
# как создавать ошибку
# raise Exception(API_ERROR_XXX, <related_obj>)
API_OK_OBJ = {"status": "success"}
API_ERROR_INTERNAL_ERROR = (100, 'internal error')
@@ -27,7 +29,9 @@ API_ERROR_VALIDATION_CURRENTLY_VERIFIED = (522, 'currently phone is verified')
API_ERROR_VALIDATION_FAILED = (523, 'cannot be verified')
API_ERROR_VALIDATION_NOT_READY = (524, 'verification service not ready. call this method later')
API_ERROR_VALIDATION_NOT_FOUND = (525, 'verification service did not send code. call this method without \'code\'')
API_ERROR_VALIDATION_UNKNOWN = (526, 'unknown verification error')
API_ERROR_VALIDATION_RESEND_LIMIT = (526, f'resend verification limit '
f'(one verify for {PHONE_VERIFICATION_RESEND_TIME_SECS} secs)')
API_ERROR_VALIDATION_UNKNOWN = (527, 'unknown verification error')
API_ERROR_VALIDATION = {
PhoneVerificationService.CHECK_PHONE_INVALID_CODE: API_ERROR_VALIDATION_INVALID_CODE,
@@ -35,6 +39,7 @@ API_ERROR_VALIDATION = {
PhoneVerificationService.CHECK_PHONE_FAILED: API_ERROR_VALIDATION_FAILED,
PhoneVerificationService.CHECK_PHONE_NOT_READY: API_ERROR_VALIDATION_NOT_READY,
PhoneVerificationService.CHECK_PHONE_NOT_FOUND: API_ERROR_VALIDATION_NOT_FOUND,
PhoneVerificationService.CHECK_PHONE_RESEND_LIMIT: API_ERROR_VALIDATION_RESEND_LIMIT,
}

View File

@@ -71,7 +71,14 @@ def account_verify_phone(params):
code = api_get_param_int(params, "code", False, None)
if code is None:
PhoneVerificationService.send_verify(user.phone)
res, err_code = PhoneVerificationService.send_verify(user.phone)
if not res:
if err_code in API_ERROR_VALIDATION:
raise Exception(API_ERROR_VALIDATION[err_code])
else:
raise Exception(API_ERROR_VALIDATION_UNKNOWN)
return api_make_response({"action": "phone_call"})
else:
res, err_code = PhoneVerificationService.check_code(user.phone, code)
@@ -79,7 +86,7 @@ def account_verify_phone(params):
if res:
user.is_phone_verified = True
user.save()
return api_make_response({"status": "success"})
return api_make_response({})
else:
if err_code in API_ERROR_VALIDATION:
raise Exception(API_ERROR_VALIDATION[err_code])

View File

@@ -7,7 +7,7 @@ def __make_invalid_argument_type_error(name, value, except_type):
def api_make_response(response):
return {"response": response}
return {"response": API_OK_OBJ | response}
def api_get_param_int(params: dict, name: str, required=True, default=0):