mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-07-07 06:06:31 +00:00
refactor(signals, tasks): update user verification task to use async and improve logging 🔨
This commit is contained in:
@ -7,7 +7,6 @@ from django.db.models.signals import post_save
|
|||||||
from api.models import User
|
from api.models import User
|
||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
from api.tasks import verify_user_with_person_api_task
|
from api.tasks import verify_user_with_person_api_task
|
||||||
from asgiref.sync import sync_to_async
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=User)
|
@receiver(post_save, sender=User)
|
||||||
@ -30,10 +29,9 @@ def assign_device_permissions(sender, instance, created, **kwargs):
|
|||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=User)
|
@receiver(post_save, sender=User)
|
||||||
@sync_to_async
|
async def verify_user_with_person_api(sender, instance, created, **kwargs):
|
||||||
def verify_user_with_person_api(sender, instance, created, **kwargs):
|
|
||||||
if created:
|
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)
|
@receiver(reset_password_token_created)
|
||||||
|
31
api/tasks.py
31
api/tasks.py
@ -18,11 +18,13 @@ env.read_env(os.path.join(BASE_DIR, ".env"))
|
|||||||
|
|
||||||
@app.task
|
@app.task
|
||||||
def add(x, y):
|
def add(x, y):
|
||||||
print(f"Adding {x} and {y}")
|
logger.info(f"Executing test background task with {x} and {y}")
|
||||||
return x + 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
|
@app.task
|
||||||
def deactivate_expired_devices():
|
def deactivate_expired_devices():
|
||||||
expired_devices = Device.objects.filter(
|
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)
|
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.
|
Verify the user with the Person API.
|
||||||
:param user_id: The ID of the user to verify.
|
: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
|
return None
|
||||||
# Call the Person API to verify the user
|
# Call the Person API to verify the user
|
||||||
|
|
||||||
verification_failed_message = f"""
|
# verification_failed_message = f"""
|
||||||
_The following user verification failed_:
|
# _The following user verification failed_:
|
||||||
*ID Card:* {user.id_card}
|
# *ID Card:* {user.id_card}
|
||||||
*Name:* {user.first_name} {user.last_name}
|
# *Name:* {user.first_name} {user.last_name}
|
||||||
*House Name:* {user.address}
|
# *House Name:* {user.address}
|
||||||
*Date of Birth:* {user.dob}
|
# *Date of Birth:* {user.dob}
|
||||||
*Island:* {(user.atoll.name if user.atoll else "N/A")} {(user.island.name if user.island else "N/A")}
|
# *Island:* {(user.atoll.name if user.atoll else "N/A")} {(user.island.name if user.island else "N/A")}
|
||||||
*Mobile:* {user.mobile}
|
# *Mobile:* {user.mobile}
|
||||||
Visit [SAR Link Portal](https://portal.sarlink.net) to manually verify this user.
|
# Visit [SAR Link Portal](https://portal.sarlink.net) to manually verify this user.
|
||||||
"""
|
# """
|
||||||
|
|
||||||
# logger.info(verification_failed_message)
|
# logger.info(verification_failed_message)
|
||||||
PERSON_VERIFY_BASE_URL = env.str("PERSON_VERIFY_BASE_URL", default="") # type: ignore
|
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,
|
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",
|
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
|
return False
|
||||||
else:
|
else:
|
||||||
# Handle the error case
|
# Handle the error case
|
||||||
|
Reference in New Issue
Block a user