1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- # @Author : Rocky
- # @File : CustomizedPushController.py
- # @Time : 2023/10/18 16:31
- import logging
- import threading
- from django.http import HttpResponse
- from django.views import View
- from Model.models import CustomizedPush
- from Object.ResponseObject import ResponseObject
- from Service.CustomizedPushService import CustomizedPushObject
- CUSTOMIZED_PUSH_LOGGER = logging.getLogger('customized_push')
- class CustomizedPushView(View):
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.GET, operation)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.POST, operation)
- def validation(self, request_dict, operation):
- response = ResponseObject()
- if operation == 'start': # 开始定制化推送
- return self.customized_push_start(request_dict, response)
- @staticmethod
- def customized_push_start(request_dict, response):
- customized_push_id = request_dict.get('customized_push_id', None)
- if not customized_push_id:
- return response.json(444)
- try:
- customized_push_qs = CustomizedPush.objects.filter(id=customized_push_id, push_satus=False).values(
- 'title', 'msg', 'link', 'icon_link', 'country', 'device_type', 'register_period', 'push_app')
- if not customized_push_qs.exists():
- return response.json(173)
- kwargs = {
- 'id': customized_push_id,
- 'title': customized_push_qs[0]['title'],
- 'msg': customized_push_qs[0]['msg'],
- 'link': customized_push_qs[0]['link'],
- 'icon_link': customized_push_qs[0]['icon_link'],
- 'country': customized_push_qs[0]['country'],
- 'device_type': customized_push_qs[0]['device_type'],
- 'register_period': customized_push_qs[0]['register_period'],
- 'push_app': customized_push_qs[0]['push_app']
- }
- # customized_push(**kwargs)
- # 异步推送消息和保存数据
- push_thread = threading.Thread(
- target=customized_push,
- kwargs=kwargs)
- push_thread.start()
- # 更新推送状态
- customized_push_qs.update(push_satus=True)
- return response.json(0)
- except Exception as e:
- return HttpResponse(repr(e), status=500)
- def customized_push(**kwargs):
- CUSTOMIZED_PUSH_LOGGER.info('customized_push_id:{}开始推送,kwargs:{}'.format(kwargs['id'], kwargs))
- user_id_list = CustomizedPushObject.query_push_user(kwargs['device_type'], kwargs['country'],
- kwargs['register_period'])
- kwargs['user_id_list'] = user_id_list
- CustomizedPushObject.push_and_save_sys_msg(**kwargs)
|