def __init__(self, data=None, *args, **kwargs): super().__init__(data=data, *args, **kwargs) if ( not self.is_bound and self.instance.contact_phone and not ( is_french_number(self.instance.contact_phone) and is_mobile_number(self.instance.contact_phone) ) ): self.initial["contact_phone"] = "" self.fields["contact_phone"].help_text = _( "Seul un numéro de téléphone mobile français (outremer inclus) peut être utilisé pour la validation." ) self.fields["contact_phone"].required = True self.fields["contact_phone"].error_messages["required"] = _( "Vous devez indiquer le numéro de mobile qui vous servira à valider votre compte." ) fields = [ Row( HalfCol( FieldWithButtons( "contact_phone", Submit("submit", "Recevoir mon code") ) ) ) ] self.helper = FormHelper() self.helper.form_method = "POST" self.helper.layout = Layout(*fields)
def send_expiration_sms_reminder(sp_subscription_pk): try: sp_subscription = SystemPaySubscription.objects.select_related( "subscription__person", "alias" ).get(pk=sp_subscription_pk) except SystemPaySubscription.DoesNotExist: return recipient = sp_subscription.subscription.person if ( not recipient.contact_phone or not is_french_number(recipient.contact_phone) or not is_mobile_number(recipient.contact_phone) ): return connection_params = generate_token_params(recipient) url = shorten_url( add_params_to_urls(front_url("view_payments"), connection_params), secret=True ) send_sms( f"Votre carte bleue arrive à expiration. Pour continuer votre don régulier à la France insoumise, " f"mettez là à jour : {url}\n" f"Merci encore de votre soutien !", recipient.contact_phone, )
def clean_contact_phone(self): phone_number = normalize_overseas_numbers(self.cleaned_data["contact_phone"]) if not is_french_number(phone_number): raise ValidationError(self.error_messages["french_only"], "french_only") if not is_mobile_number(phone_number): raise ValidationError(self.error_messages["mobile_only"], "mobile_only") return phone_number
def handle(self, phone_number, **options): if not is_mobile_number(phone_number): raise ValueError( "Le numéro doit être un numéro de téléphone mobile.") (session, created) = TelegramSession.objects.get_or_create( phone_number=phone_number) client = Client(":memory:", phone_number=str(phone_number), **api_params) client.start() session_string = client.export_session_string() session.session_string = session_string session.save() client.stop()
def test_is_mobile(self): self.assertTrue(is_mobile_number(p("06 38 68 98 45"))) self.assertTrue(is_mobile_number(p("07 34 98 56 78"))) self.assertFalse(is_mobile_number(p("06 90 48 25 64"))) # Guadeloupe, en +33 self.assertTrue( is_mobile_number(p("+590 6 90 48 25 64")) # Guadeloupe, en +590 ) self.assertFalse(is_mobile_number(p("01 42 87 65 89"))) self.assertFalse(is_mobile_number(p("09 45 87 65 23"))) self.assertFalse(is_mobile_number(p("05 90 45 78 56"))) # Guadeloupe, en +33 self.assertFalse( is_mobile_number(p("+590 5 90 45 78 56")) # Guadeloupe, en +590 )
def update_telegram_groups(self, pk): with transaction.atomic(): try: instance = TelegramGroup.objects.select_for_update().get(pk=pk) except TelegramGroup.DoesNotExist: return with instance.admin_session.create_client() as client: in_chat_numbers = set( member.user.phone_number for chat_id in instance.telegram_ids for member in client.iter_chat_members(chat_id)) chat_empty_slots = { chat_id: 50 - client.get_chat_members_count(chat_id) for chat_id in instance.telegram_ids } in_segment_and_chat_people = [ person for person in instance.segment.get_subscribers_queryset() if str(person.contact_phone)[1:] in in_chat_numbers ] in_segment_not_chat_people = [ person for person in instance.segment.get_subscribers_queryset() if person.contact_phone and is_mobile_number(person.contact_phone) and str(person.contact_phone)[1:] not in in_chat_numbers ] client.add_contacts([ InputPhoneContact( phone=str(person.contact_phone), first_name=person.first_name, last_name=person.last_name, ) for person in in_segment_not_chat_people ]) new_chat_members = [ person for person in in_segment_not_chat_people if is_telegram_user(client, person) ] def chat_generator(): for chat_id in instance.telegram_ids: yield chat_id while True: title = f"{instance.name} {len(instance.telegram_ids) + 1}" if instance.type == TelegramGroup.CHAT_TYPE_SUPERGROUP: chat_id = client.create_supergroup(title=title).id client.set_chat_permissions(chat_id, DEFAULT_GROUP_PERMISSIONS) elif instance.type == TelegramGroup.CHAT_TYPE_CHANNEL: chat_id = client.create_channel(title=title).id instance.telegram_ids = instance.telegram_ids + [chat_id] chat_empty_slots[chat_id] = 200 sleep(5) yield chat_id def new_chat_members_generator(): remaining = new_chat_members chat_iterator = chat_generator() while len(remaining) > 0: chat_id = next(chat_iterator) while len(remaining) > 0 and chat_empty_slots[chat_id] > 0: to_yield = remaining[:chat_empty_slots[chat_id]] remaining = remaining[chat_empty_slots[chat_id]:] yield chat_id, [str(p.contact_phone) for p in to_yield] for chat_id, phones_to_add in new_chat_members_generator(): client.add_chat_members(chat_id, phones_to_add) sleep(5) chat_empty_slots[ chat_id] = 200 - client.get_chat_members_count(chat_id) TelegramGroup.objects.filter(pk=pk).update( telegram_ids=instance.telegram_ids, telegram_users=len(new_chat_members) + len(in_segment_and_chat_people), )