def send_sms_messages_bulk(msg_data, replace_names=False): from orders_manager.models import Client, SmsDeliveryMessage from orders_manager.sms_delivery_service import SmsDeliveryService from orders_manager.transliterate_service import (transliterate_message, need_transliteration) clients = Client.objects.filter( pk__in=[i.get('client_id') for i in msg_data]).all() messages = [] for client in clients: for item in msg_data: if item.get('client_id') == client.id: client_name = (transliterate_message(client.name) if need_transliteration() else client.name) msg = (item.get('message').format(client_name=client_name) if replace_names else item.get('message')) messages.append({'recipient': client.phone, 'message': msg}) sms_service = SmsDeliveryService() sms_service.send_messages(messages) sms_ids = [i.get('id') for i in msg_data if i.get('id')] if sms_ids: for sms_model in SmsDeliveryMessage.objects.filter(pk__in=sms_ids): sms_model.is_checked = True sms_model.is_sent = True sms_model.save() return sms_ids
def send_sms_messages_bulk(msg_data, replace_names=False): from orders_manager.models import Client, SmsDeliveryMessage from orders_manager.sms_delivery_service import SmsDeliveryService from orders_manager.transliterate_service import (transliterate_message, need_transliteration) clients = Client.objects.filter( pk__in=[i.get('client_id') for i in msg_data]).all() messages = [] for client in clients: for item in msg_data: if item.get('client_id') == client.id: client_name = (transliterate_message(client.name) if need_transliteration() else client.name) msg = (item.get('message').format(client_name=client_name) if replace_names else item.get('message')) messages.append({ 'recipient': client.phone, 'message': msg }) sms_service = SmsDeliveryService() sms_service.send_messages(messages) sms_ids = [i.get('id') for i in msg_data if i.get('id')] if sms_ids: for sms_model in SmsDeliveryMessage.objects.filter(pk__in=sms_ids): sms_model.is_checked = True sms_model.is_sent = True sms_model.save() return sms_ids
def post(self, request, *args, **kwargs): try: if request.data: target = request.data.get('target') if target == 'event': _raise_denied_if_has_no_perm(self.request.user, 'delete_program') action = request.data.get('action') if action == 'save': event = SmsDeliveryEvent.objects.update_or_create( **request.data.get('data')) d = SmsDeliveryEventSerializer(event) return Response(status=status.HTTP_200_OK, data=d.data) elif action == 'delete': event_id = request.data.get('event_id') SmsDeliveryEvent.objects.get(id=event_id).delete() return Response(status=status.HTTP_200_OK) elif target == 'api_settings': sms_service = SmsDeliveryService() action = request.data.get('action') if action == 'get': return Response( status=status.HTTP_200_OK, data=sms_service.get_api_settings() ) if action == 'save': settings = request.data.get('settings') sms_service.save_api_settings( login=settings.get('login'), apikey=settings.get('apikey'), sender=settings.get('sender'), transliteration=settings.get('transliteration') ) return Response(status=status.HTTP_200_OK) action = request.data.get('action') if action == 'send': _raise_denied_if_has_no_perm(self.request.user, 'delete_program') mode = request.data.get('mode') messages = request.data.get('messages') if mode == 'manual' and messages: send_sms_messages_bulk(messages, replace_names=True) return Response(status=status.HTTP_200_OK) elif mode == 'scheduled' and messages: sent_ids = send_sms_messages_bulk(messages) return Response( status=status.HTTP_200_OK, data={ 'sent': sent_ids } ) if action == 'update_message_transliteration': from orders_manager.transliterate_service import \ transliterate_message, need_transliteration message = request.data.get('message', '') return Response( status=status.HTTP_200_OK, data={ 'message': ( transliterate_message(message) if need_transliteration() else transliterate_message( message, reverse=False) ) } ) elif action == 'moderated_message': _raise_denied_if_has_no_perm(self.request.user, 'delete_program') msg_id = request.data.get('message_id') value = request.data.get('value') if msg_id: sms_msg = SmsDeliveryMessage.objects.get(id=msg_id) sms_msg.is_checked = value sms_msg.save() return Response(status=status.HTTP_200_OK) return Response(status=status.HTTP_400_BAD_REQUEST) except Exception as ex: return Response( status=status.HTTP_400_BAD_REQUEST, data={ 'status': 'error', 'message': ex.args[0] } )