示例#1
0
 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]
             }
         )