|
@@ -1,19 +1,7 @@
|
|
|
-#!/usr/bin/env python3
|
|
|
-# -*- coding: utf-8 -*-
|
|
|
-"""
|
|
|
-@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
|
|
|
-@AUTHOR: ASJRD018
|
|
|
-@NAME: AnsjerFormal
|
|
|
-@software: PyCharm
|
|
|
-@DATE: 2018/9/11 15:08
|
|
|
-@Version: python3.6
|
|
|
-@MODIFY DECORD:ansjer dev
|
|
|
-@file: UserController.py
|
|
|
-@Contact: chanjunkai@163.com
|
|
|
-"""
|
|
|
import datetime
|
|
|
import time
|
|
|
import oss2
|
|
|
+import requests
|
|
|
from django.contrib.auth.hashers import make_password, check_password # 对密码加密模块
|
|
|
from django.db import transaction
|
|
|
from django.db.models import Q
|
|
@@ -22,10 +10,13 @@ from django.utils.timezone import utc
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
from django.views.generic import TemplateView
|
|
|
|
|
|
-from Ansjer.config import SERVER_DOMAIN, OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
|
|
|
+from Ansjer.config import SERVER_DOMAIN, OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, AWS_ACCESS_KEY_ID, \
|
|
|
+ AWS_SECRET_ACCESS_KEY, AWS_SES_ACCESS_REGION, DETECT_PUSH_DOMAINS
|
|
|
from Controller.CheckUserData import DataValid, RandomStr
|
|
|
from Model.models import Device_User, Role, UserExModel, CountryModel, MenuModel, FeedBackModel, StatResModel, \
|
|
|
- SysMassModel, App_Info, SysMsgModel, DeviceSuperPassword
|
|
|
+ SysMassModel, App_Info, SysMsgModel, DeviceSuperPassword, CustomizedPush, DeviceTypeModel
|
|
|
+from Object.AWS.AmazonS3Util import AmazonS3Util
|
|
|
+from Object.ApschedulerObject import ApschedulerObject
|
|
|
from Object.RedisObject import RedisObject
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
@@ -883,3 +874,173 @@ class UserManagement(View):
|
|
|
return response.json(173)
|
|
|
device_super_password_qs.delete()
|
|
|
return response.json(0)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def getCustomizedPushList(request_dict, response):
|
|
|
+ title = request_dict.get('title', None)
|
|
|
+ country = request_dict.get('country', None)
|
|
|
+ device_type = request_dict.get('device_type', None)
|
|
|
+ push_satus = request_dict.get('push_satus', None)
|
|
|
+ page = request_dict.get('pageNo', None)
|
|
|
+ line = request_dict.get('pageSize', None)
|
|
|
+
|
|
|
+ if not all([page, line]):
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ page = int(page)
|
|
|
+ line = int(line)
|
|
|
+
|
|
|
+ try:
|
|
|
+ customized_push_qs = CustomizedPush.objects.all()
|
|
|
+ if title:
|
|
|
+ customized_push_qs = customized_push_qs.filter(title=title)
|
|
|
+ if country:
|
|
|
+ customized_push_qs = customized_push_qs.filter(country=country)
|
|
|
+ if device_type:
|
|
|
+ customized_push_qs = customized_push_qs.filter(device_type__contains=device_type)
|
|
|
+ if push_satus:
|
|
|
+ customized_push_qs = customized_push_qs.filter(push_satus=push_satus)
|
|
|
+ if not customized_push_qs.exists():
|
|
|
+ return response.json(0, [])
|
|
|
+ total = customized_push_qs.count()
|
|
|
+ customized_push_qs = customized_push_qs.\
|
|
|
+ values('id', 'title', 'msg', 'link', 'icon_link', 'country', 'device_type', 'register_period',
|
|
|
+ 'time_zone', 'push_time', 'push_app', 'push_satus')
|
|
|
+ customized_push_qs = customized_push_qs[(page - 1) * line:page * line]
|
|
|
+ customized_push_list = []
|
|
|
+ for customized_push in customized_push_qs:
|
|
|
+ # 格式化数据
|
|
|
+ register_period = customized_push['register_period'] + '年'
|
|
|
+ time_zone = 'UTC ' + customized_push['time_zone']
|
|
|
+
|
|
|
+ customized_push_list.append({
|
|
|
+ 'customized_push_id': customized_push['id'],
|
|
|
+ 'title': customized_push['title'],
|
|
|
+ 'msg': customized_push['msg'],
|
|
|
+ 'link': customized_push['link'],
|
|
|
+ 'icon_link': customized_push['icon_link'],
|
|
|
+ 'country': customized_push['country'],
|
|
|
+ 'device_type': customized_push['device_type'],
|
|
|
+ 'register_period': register_period,
|
|
|
+ 'time_zone': time_zone,
|
|
|
+ 'push_time': customized_push['push_time'],
|
|
|
+ 'push_app': customized_push['push_app'],
|
|
|
+ 'push_satus': customized_push['push_satus']
|
|
|
+ })
|
|
|
+ return response.json(0, {'list': customized_push_list, 'total': total})
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def addOrEditCustomizedPush(cls, request, request_dict, response):
|
|
|
+ title = request_dict.get('title', None)
|
|
|
+ msg = request_dict.get('msg', None)
|
|
|
+ link = request_dict.get('link', None)
|
|
|
+ icon = request.FILES.get('icon', None)
|
|
|
+ country = request_dict.get('country', None)
|
|
|
+ device_type = request_dict.get('deviceType', None)
|
|
|
+ register_period = request_dict.get('registerPeriod', None)
|
|
|
+ time_zone = request_dict.get('timeZone', None)
|
|
|
+ push_time = request_dict.get('pushTime', None)
|
|
|
+ push_app = request_dict.get('pushApp', None)
|
|
|
+ is_edit = request_dict.get('isEdit', None)
|
|
|
+
|
|
|
+ if not all([title, msg, link, country, device_type, register_period, time_zone, push_time, push_app]):
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ # 截掉UTC和空格
|
|
|
+ time_zone = time_zone[4:]
|
|
|
+ try:
|
|
|
+ push_timestamp = CommonService.convert_to_timestamp(float(time_zone), push_time)
|
|
|
+ if push_timestamp <= int(time.time()):
|
|
|
+ return response.json(806)
|
|
|
+ customized_push_data = {
|
|
|
+ 'title': title,
|
|
|
+ 'msg': msg,
|
|
|
+ 'link': link,
|
|
|
+ 'country': country,
|
|
|
+ 'device_type': device_type,
|
|
|
+ 'register_period': register_period,
|
|
|
+ 'time_zone': time_zone,
|
|
|
+ 'push_time': push_time,
|
|
|
+ 'push_timestamp': push_timestamp,
|
|
|
+ 'push_app': push_app
|
|
|
+ }
|
|
|
+ icon_link = ''
|
|
|
+ if icon is not None:
|
|
|
+ icon_name = icon.name
|
|
|
+ icon_link = 'https://ansjerfilemanager.s3.amazonaws.com/customized-push/' + icon_name
|
|
|
+ customized_push_data['icon_link'] = icon_link
|
|
|
+
|
|
|
+ if icon_link:
|
|
|
+ # 上传没有上传过的图片到S3
|
|
|
+ customized_push_qs = CustomizedPush.objects.filter(icon_link=icon_link)
|
|
|
+ if not customized_push_qs.exists():
|
|
|
+ bucket = 'ansjerfilemanager'
|
|
|
+ file_key = 'customized-push/' + icon_name
|
|
|
+ s3 = AmazonS3Util(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], AWS_SES_ACCESS_REGION)
|
|
|
+ s3.upload_file_obj(
|
|
|
+ bucket,
|
|
|
+ file_key,
|
|
|
+ icon,
|
|
|
+ {'ContentType': icon.content_type, 'ACL': 'public-read'})
|
|
|
+
|
|
|
+ apscheduler_obj = ApschedulerObject()
|
|
|
+ if is_edit: # 编辑
|
|
|
+ customized_push_id = request_dict.get('customizedPushId', None)
|
|
|
+ if not customized_push_id:
|
|
|
+ return response.json(444)
|
|
|
+ customized_push_data['push_satus'] = False
|
|
|
+ CustomizedPush.objects.filter(id=customized_push_id).update(**customized_push_data)
|
|
|
+ apscheduler_obj.del_job('customizedPushId_{}'.format(customized_push_id)) # 删除旧定时任务
|
|
|
+ else: # 新增
|
|
|
+ customized_push = CustomizedPush.objects.create(**customized_push_data)
|
|
|
+ customized_push_id = customized_push.id
|
|
|
+
|
|
|
+ # 创建定时任务
|
|
|
+ task_id = 'customized_push_id_{}'.format(customized_push_id)
|
|
|
+ apscheduler_obj.create_date_job(func=cls.req_customized_push, task_id=task_id, time_stamp=push_timestamp,
|
|
|
+ args=(customized_push_id,))
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def req_customized_push(customized_push_id):
|
|
|
+ """
|
|
|
+ 请求定制化推送
|
|
|
+ @param customized_push_id:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ data = {'customized_push_id': customized_push_id}
|
|
|
+ print(data)
|
|
|
+ url = DETECT_PUSH_DOMAINS + 'customized_push/start'
|
|
|
+ req = requests.post(url=url, data=data, timeout=8)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def getCountryList(response):
|
|
|
+ try:
|
|
|
+ country_qs = CountryModel.objects.filter().values('country_name')
|
|
|
+ if not country_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+
|
|
|
+ country_list = []
|
|
|
+ for country in country_qs:
|
|
|
+ country_list.append(country['country_name'])
|
|
|
+ return response.json(0, {'list': country_list})
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def getDeviceTypeList(response):
|
|
|
+ try:
|
|
|
+ device_type_qs = DeviceTypeModel.objects.filter().values('name')
|
|
|
+ if not device_type_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+
|
|
|
+ device_type_list = []
|
|
|
+ for device_type in device_type_qs:
|
|
|
+ device_type_list.append(device_type['name'])
|
|
|
+ return response.json(0, {'list': device_type_list})
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|