|
@@ -69,62 +69,99 @@ class UserSubscriptionControllerView(View):
|
|
|
user_qs = Device_User.objects.filter(userID=user_id)
|
|
|
if not user_qs.exists():
|
|
|
return response.json(104)
|
|
|
- user_sub = UserEmailSubscriptions.objects.filter(user_id=user_id).values('status').first()
|
|
|
+ user_sub = UserEmailSubscriptions.objects.filter(user_id=user_id).values('status', 'push_sub_status').first()
|
|
|
if not user_sub:
|
|
|
- return response.json(0, {"status": 0})
|
|
|
- user_sub = {"status": user_sub['status']}
|
|
|
+ UserEmailSubscriptions.objects.create(user_id=user_id, status=0, push_sub_status=1,
|
|
|
+ updated_time=int(time.time()), created_time=int(time.time()))
|
|
|
+ return response.json(0, {"emailSubStatus": 0, 'pushSubStatus': 1})
|
|
|
+ email_sub_status = 1 if user_sub["status"] == 1 else 0
|
|
|
+ push_sub_status = 0 if user_sub["push_sub_status"] == 0 else 1
|
|
|
+ user_sub = {"emailSubStatus": email_sub_status, 'pushSubStatus': push_sub_status}
|
|
|
return response.json(0, user_sub)
|
|
|
|
|
|
def switch_subscription(self, user_id, request_dict, response):
|
|
|
"""
|
|
|
- 订阅开关
|
|
|
+ 邮件订阅开关
|
|
|
@param user_id: str
|
|
|
@param request_dict: dict
|
|
|
@param response
|
|
|
"""
|
|
|
user_qs = Device_User.objects.filter(userID=user_id)
|
|
|
- status = request_dict.get('status', None)
|
|
|
- if not status:
|
|
|
- return response.json(444, "status")
|
|
|
+ email_sub_status = request_dict.get('emailSubStatus', None)
|
|
|
+ push_sub_status = request_dict.get('pushSubStatus', None)
|
|
|
+ if not email_sub_status and not push_sub_status:
|
|
|
+ return response.json(444, "emailSubStatus or pushSubStatus")
|
|
|
if not user_qs.exists():
|
|
|
return response.json(104)
|
|
|
|
|
|
- # 修改数据库中订阅状态
|
|
|
- status = int(status)
|
|
|
- user_sub_qs = UserEmailSubscriptions.objects.filter(user_id=user_id)
|
|
|
- # 订阅
|
|
|
- if status == 1:
|
|
|
- subscribers = Device_User.objects.filter(userID=user_id).values('NickName', 'userEmail',
|
|
|
- 'region_country').first()
|
|
|
- if not subscribers["userEmail"]:
|
|
|
- LOGGER.info(f'subscribers{user_id}邮箱为空,无法订阅')
|
|
|
- return response.json(183)
|
|
|
+ # 用户推送订阅
|
|
|
+ if push_sub_status:
|
|
|
+ push_sub_status = int(push_sub_status)
|
|
|
+ user_sub_qs = UserEmailSubscriptions.objects.filter(user_id=user_id)
|
|
|
if user_sub_qs.exists():
|
|
|
- user_sub_qs.update(status=1, updated_time=int(time.time()))
|
|
|
+ user_sub_qs.update(push_sub_status=push_sub_status)
|
|
|
else:
|
|
|
- UserEmailSubscriptions.objects.create(user_id=user_id, status=1, email=subscribers["userEmail"],
|
|
|
- created_time=int(time.time()), updated_time=int(time.time()))
|
|
|
- subscription_thread = threading.Thread(target=self.subscription, args=(subscribers,))
|
|
|
- subscription_thread.start()
|
|
|
- # 取消订阅
|
|
|
- else:
|
|
|
- device_user = Device_User.objects.filter(userID=user_id).values('userEmail').first()
|
|
|
- if device_user:
|
|
|
- customer = {
|
|
|
- "email": device_user["userEmail"],
|
|
|
- "first_name": device_user["userEmail"],
|
|
|
- "last_name": "APP",
|
|
|
+ user = user_qs.values('userEmail', 'phone').first()
|
|
|
+ push_sub = {
|
|
|
+ "user_id": user_id,
|
|
|
+ "push_sub_status": push_sub_status,
|
|
|
+ "created_time": int(time.time()),
|
|
|
+ "updated_time": int(time.time())
|
|
|
}
|
|
|
+ if user["userEmail"]:
|
|
|
+ push_sub["email"] = user["userEmail"]
|
|
|
+ if user["phone"]:
|
|
|
+ push_sub["phone"] = user["phone"]
|
|
|
+ UserEmailSubscriptions.objects.create(**push_sub)
|
|
|
+ else:
|
|
|
+ user_sub = UserEmailSubscriptions.objects.filter(user_id=user_id).values('status',
|
|
|
+ 'push_sub_status').first()
|
|
|
+ if not user_sub:
|
|
|
+ push_sub_status = 1
|
|
|
else:
|
|
|
- return response.json(0, {"status": status})
|
|
|
- yotpo = YotpoCoreObject()
|
|
|
- list_id = 8589406
|
|
|
- subscription_thread = threading.Thread(target=yotpo.close_subscribers, args=(customer, list_id))
|
|
|
- subscription_thread.start()
|
|
|
- if user_sub_qs.exists():
|
|
|
- customer["status"] = "unsubscription"
|
|
|
- user_sub_qs.update(status=0, sub_result=customer)
|
|
|
- return response.json(0, {"status": status})
|
|
|
+ push_sub_status = user_sub["push_sub_status"]
|
|
|
+
|
|
|
+ if email_sub_status:
|
|
|
+ # 修改数据库中订阅状态
|
|
|
+ email_sub_status = int(email_sub_status)
|
|
|
+ user_sub_qs = UserEmailSubscriptions.objects.filter(user_id=user_id)
|
|
|
+ # 邮件订阅
|
|
|
+ if email_sub_status == 1:
|
|
|
+ subscribers = user_qs.values('NickName', 'userEmail', 'region_country').first()
|
|
|
+ if not subscribers["userEmail"]:
|
|
|
+ LOGGER.info(f'subscribers{user_id}邮箱为空,无法订阅')
|
|
|
+ return response.json(183)
|
|
|
+ if user_sub_qs.exists():
|
|
|
+ user_sub_qs.update(email=subscribers["userEmail"], status=1, updated_time=int(time.time()))
|
|
|
+ else:
|
|
|
+ UserEmailSubscriptions.objects.create(user_id=user_id, status=1, email=subscribers["userEmail"],
|
|
|
+ created_time=int(time.time()), updated_time=int(time.time()))
|
|
|
+ subscription_thread = threading.Thread(target=self.subscription, args=(subscribers,))
|
|
|
+ subscription_thread.start()
|
|
|
+ # 取消订阅
|
|
|
+ elif email_sub_status == 0:
|
|
|
+ device_user = Device_User.objects.filter(userID=user_id).values('userEmail').first()
|
|
|
+ if device_user:
|
|
|
+ customer = {
|
|
|
+ "email": device_user["userEmail"],
|
|
|
+ "first_name": device_user["userEmail"],
|
|
|
+ "last_name": "APP",
|
|
|
+ }
|
|
|
+ yotpo = YotpoCoreObject()
|
|
|
+ list_id = 8589406
|
|
|
+ subscription_thread = threading.Thread(target=yotpo.close_subscribers, args=(customer, list_id))
|
|
|
+ subscription_thread.start()
|
|
|
+ if user_sub_qs.exists():
|
|
|
+ customer["status"] = "unsubscription"
|
|
|
+ user_sub_qs.update(email=device_user["userEmail"], status=0, sub_result=customer)
|
|
|
+ else:
|
|
|
+ user_sub = UserEmailSubscriptions.objects.filter(user_id=user_id).values('status').first()
|
|
|
+ if not user_sub:
|
|
|
+ email_sub_status = 0
|
|
|
+ else:
|
|
|
+ email_sub_status = user_sub["status"]
|
|
|
+
|
|
|
+ return response.json(0, {"emailSubStatus": email_sub_status, "pushSubStatus": push_sub_status})
|
|
|
|
|
|
@staticmethod
|
|
|
def subscription(subscribers):
|