refactor(signals, tasks): update user verification task to use async and improve logging 🔨

This commit is contained in:
2025-07-05 14:33:25 +05:00
parent 6ec31023c7
commit c31acead70
2 changed files with 19 additions and 18 deletions

View File

@ -7,7 +7,6 @@ from django.db.models.signals import post_save
from api.models import User
from django.contrib.auth.models import Permission
from api.tasks import verify_user_with_person_api_task
from asgiref.sync import sync_to_async
@receiver(post_save, sender=User)
@ -30,10 +29,9 @@ def assign_device_permissions(sender, instance, created, **kwargs):
@receiver(post_save, sender=User)
@sync_to_async
def verify_user_with_person_api(sender, instance, created, **kwargs):
async def verify_user_with_person_api(sender, instance, created, **kwargs):
if created:
verify_user_with_person_api_task(instance.id)
await verify_user_with_person_api_task.defer_async(instance.id)
@receiver(reset_password_token_created)

View File

@ -18,11 +18,13 @@ env.read_env(os.path.join(BASE_DIR, ".env"))
@app.task
def add(x, y):
print(f"Adding {x} and {y}")
logger.info(f"Executing test background task with {x} and {y}")
return x + y
@app.periodic(cron="0 0 */28 * *") # type: ignore
@app.periodic(
cron="0 0 */28 * *", queue="heavy_tasks", periodic_id="deactivate_expired_devices"
) # type: ignore
@app.task
def deactivate_expired_devices():
expired_devices = Device.objects.filter(
@ -66,7 +68,8 @@ def add_new_devices_to_omada(new_devices: list[dict]):
omada_client.add_new_devices_to_omada(new_devices)
def verify_user_with_person_api_task(user_id: int):
@app.task
async def verify_user_with_person_api_task(user_id: int):
"""
Verify the user with the Person API.
:param user_id: The ID of the user to verify.
@ -80,16 +83,16 @@ def verify_user_with_person_api_task(user_id: int):
return None
# Call the Person API to verify the user
verification_failed_message = f"""
_The following user verification failed_:
*ID Card:* {user.id_card}
*Name:* {user.first_name} {user.last_name}
*House Name:* {user.address}
*Date of Birth:* {user.dob}
*Island:* {(user.atoll.name if user.atoll else "N/A")} {(user.island.name if user.island else "N/A")}
*Mobile:* {user.mobile}
Visit [SAR Link Portal](https://portal.sarlink.net) to manually verify this user.
"""
# verification_failed_message = f"""
# _The following user verification failed_:
# *ID Card:* {user.id_card}
# *Name:* {user.first_name} {user.last_name}
# *House Name:* {user.address}
# *Date of Birth:* {user.dob}
# *Island:* {(user.atoll.name if user.atoll else "N/A")} {(user.island.name if user.island else "N/A")}
# *Mobile:* {user.mobile}
# Visit [SAR Link Portal](https://portal.sarlink.net) to manually verify this user.
# """
# logger.info(verification_failed_message)
PERSON_VERIFY_BASE_URL = env.str("PERSON_VERIFY_BASE_URL", default="") # type: ignore
@ -170,7 +173,7 @@ def verify_user_with_person_api_task(user_id: int):
user.mobile,
f"Dear {user.first_name} {user.last_name}, \n\nYour account registration is being processed. \n\nWe will notify you once verification is complete. \n\n - SAR Link",
)
send_clean_telegram_markdown(message=verification_failed_message)
# send_clean_telegram_markdown(message=verification_failed_message)
return False
else:
# Handle the error case