Fix(accounts): send sms

send sms
This commit is contained in:
Haydar Ghasemi 2026-05-23 13:55:47 +03:30
parent 706de57427
commit 43b5ba9028

View file

@ -0,0 +1,62 @@
from apps.users.models import *
from django.contrib.gis.geos import Point, Polygon
import random
import os
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
help = "send sms by uuid list"
def add_arguments(self, parser):
parser.add_argument('message_file', type=str, help='Message as a file')
parser.add_argument('uuids_file', type=str, help='File with one UUID per line')
def handle(self, *args, **options):
print(options['message_file'])
print(options['uuids_file'])
message_file_path = options['message_file']
uuids_file_path = options['uuids_file']
if not os.path.exists(message_file_path):
raise CommandError(f'File "{message_file_path}" does not exist')
if not os.path.exists(uuids_file_path):
raise CommandError(f'File "{uuids_file_path}" does not exist')
uuids = []
invalid_lines = []
message: str = ''''''
with open(message_file_path, 'r', encoding='utf-8') as f:
message = f.read()
with open(uuids_file_path, 'r', encoding='utf-8') as f:
for line_num, line in enumerate(f, 1):
line = line.strip()
if not line: # Skip empty lines
continue
try:
# Validate and normalize UUID
valid_uuid = str(uuid.UUID(line))
uuids.append(valid_uuid)
self.stdout.write(f'Line {line_num}: ✓ {valid_uuid}')
except (ValueError, AttributeError, TypeError):
invalid_lines.append((line_num, line))
self.stdout.write(
self.style.ERROR(f'Line {line_num}: ✗ Invalid UUID "{line}"')
)
sms_client = SMSPolicy.get_client()
users = User.objects.filter(uuid__in=uuids)
empty_phone_numbers = list(filter(lambda u: not u.phone_number, users))
if empty_phone_numbers:
logger.error(f'not exist phone numbers for following uuids')
print(empty_phone_numbers)
logger.error(f'end of not exist phone numbers')
return
for user in users:
logger.info(f'try to send message for: {user.uuid} and {user.phone_number}')
# sms_client.send_sms(user.phone_number.strip('+'), message)
logger.info(f'sent message for: {user.uuid} and {user.phone_number}')