|
|
@@ -8,7 +8,9 @@
|
|
|
"""
|
|
|
import threading
|
|
|
import time
|
|
|
+from datetime import datetime
|
|
|
|
|
|
+import requests
|
|
|
from django.http import QueryDict
|
|
|
from django.views import View
|
|
|
|
|
|
@@ -17,6 +19,9 @@ from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Object.YotpoCoreObject import YotpoCoreObject
|
|
|
from Ansjer.config import LOGGER
|
|
|
+from django.conf import settings
|
|
|
+OMNI_API_KEY = settings.OMNI_API_KEY
|
|
|
+API_URL = "https://api.omnisend.com/v5/"
|
|
|
|
|
|
|
|
|
class UserSubscriptionControllerView(View):
|
|
|
@@ -99,7 +104,7 @@ class UserSubscriptionControllerView(View):
|
|
|
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(push_sub_status=push_sub_status)
|
|
|
+ user_sub_qs.update(push_sub_status=push_sub_status, updated_time=int(time.time()))
|
|
|
else:
|
|
|
user = user_qs.values('userEmail', 'phone').first()
|
|
|
push_sub = {
|
|
|
@@ -124,6 +129,7 @@ class UserSubscriptionControllerView(View):
|
|
|
if email_sub_status:
|
|
|
# 修改数据库中订阅状态
|
|
|
email_sub_status = int(email_sub_status)
|
|
|
+ email_scription = email_sub_status
|
|
|
user_sub_qs = UserEmailSubscriptions.objects.filter(user_id=user_id)
|
|
|
# 邮件订阅
|
|
|
if email_sub_status == 1:
|
|
|
@@ -136,24 +142,32 @@ class UserSubscriptionControllerView(View):
|
|
|
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 = threading.Thread(target=self.subscription, args=(subscribers,email_scription))
|
|
|
subscription_thread.start()
|
|
|
# 取消订阅
|
|
|
elif email_sub_status == 0:
|
|
|
device_user = Device_User.objects.filter(userID=user_id).values('userEmail').first()
|
|
|
+ email = device_user["userEmail"]
|
|
|
+ omni_unsubscription_thread = threading.Thread(target=UserSubscriptionControllerView.close_omni_subscribers, args=(email,))
|
|
|
+ omni_unsubscription_thread.start()
|
|
|
+
|
|
|
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)
|
|
|
+ try:
|
|
|
+ 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,
|
|
|
+ updated_time=int(time.time()))
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500,'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
else:
|
|
|
user_sub = UserEmailSubscriptions.objects.filter(user_id=user_id).values('status').first()
|
|
|
if not user_sub:
|
|
|
@@ -164,7 +178,7 @@ class UserSubscriptionControllerView(View):
|
|
|
return response.json(0, {"emailSubStatus": email_sub_status, "pushSubStatus": push_sub_status})
|
|
|
|
|
|
@staticmethod
|
|
|
- def subscription(subscribers):
|
|
|
+ def subscription(subscribers, email_scription):
|
|
|
"""
|
|
|
订阅
|
|
|
@param subscribers: dict
|
|
|
@@ -172,6 +186,7 @@ class UserSubscriptionControllerView(View):
|
|
|
"""
|
|
|
|
|
|
yotpo = YotpoCoreObject()
|
|
|
+ email = subscribers.get("userEmail")
|
|
|
|
|
|
try:
|
|
|
# 查询顾客所在地区
|
|
|
@@ -203,21 +218,121 @@ class UserSubscriptionControllerView(View):
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- result = yotpo.creat_and_update_customers(customer)
|
|
|
- list_id = 8589406
|
|
|
- sub_status, sub_result = yotpo.create_subscribers(customer, list_id)
|
|
|
+ result = UserSubscriptionControllerView.sync_on_register(email, email_scription, country_code)
|
|
|
+ if result:
|
|
|
+ LOGGER.info(f'{email}{result}')
|
|
|
+
|
|
|
+ try:
|
|
|
+ result = yotpo.creat_and_update_customers(customer)
|
|
|
+ list_id = 8589406
|
|
|
+ sub_status, sub_result = yotpo.create_subscribers(customer, list_id)
|
|
|
+
|
|
|
+ if result and sub_status:
|
|
|
+ # 创建结果写入数据库
|
|
|
+ user_sub_qs = UserEmailSubscriptions.objects.filter(email=subscribers["userEmail"])
|
|
|
+ if user_sub_qs.exists():
|
|
|
+ user_sub_qs.update(email=subscribers["userEmail"], status=1, sub_result=sub_result,list_id=list_id,
|
|
|
+ updated_time=int(time.time()))
|
|
|
+ LOGGER.info(f'在yotpo创建客户并订阅成功,customer:{customer}')
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ LOGGER.info(f'在yotpo创建客户并订阅失败,customer:{customer}')
|
|
|
+ except Exception as yotpo_err:
|
|
|
+ LOGGER.warning(f'Yotpo接口异常,已跳过:{yotpo_err}')
|
|
|
|
|
|
- if result and sub_status:
|
|
|
- # 创建结果写入数据库
|
|
|
- user_sub_qs = UserEmailSubscriptions.objects.filter(email=subscribers["userEmail"])
|
|
|
- if user_sub_qs.exists():
|
|
|
- user_sub_qs.update(email=subscribers["userEmail"], status=1, sub_result=sub_result, list_id=list_id,
|
|
|
- updated_time=int(time.time()))
|
|
|
- LOGGER.info(f'在yotpo创建客户并订阅成功,customer:{customer}')
|
|
|
- return True
|
|
|
- else:
|
|
|
- LOGGER.info(f'在yotpo创建客户并订阅失败,customer:{customer}')
|
|
|
return False
|
|
|
except Exception as e:
|
|
|
LOGGER.error(f'{subscribers["userEmail"]}订阅失败:{e}')
|
|
|
return False
|
|
|
+
|
|
|
+ def sync_on_register(email, email_scription, country_code):
|
|
|
+ """
|
|
|
+ 注册时调用,写入 Omnisend。当email_scription==1为订阅打app subscribed标签
|
|
|
+ """
|
|
|
+ email_status = "subscribed" if str(email_scription) == '1' else "unsubscribed"
|
|
|
+
|
|
|
+ payload = {
|
|
|
+ "updateEnabled": True,
|
|
|
+ "country": country_code,
|
|
|
+ "identifiers": [
|
|
|
+ {
|
|
|
+ "type": "email",
|
|
|
+ "id": email,
|
|
|
+ "channels": {
|
|
|
+ "email": {
|
|
|
+ "status": email_status,
|
|
|
+ "statusDate": datetime.utcnow().isoformat() + "Z",
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "customProperties": {
|
|
|
+ "source": "User Registration",
|
|
|
+ "createdAt": datetime.utcnow().isoformat() + "Z",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if str(email_scription) == "1":
|
|
|
+ payload["tags"] = ["app subscribed"]
|
|
|
+
|
|
|
+ headers = {
|
|
|
+ "X-API-KEY": OMNI_API_KEY,
|
|
|
+ "accept": "application/json",
|
|
|
+ "content-type": "application/json"
|
|
|
+ }
|
|
|
+
|
|
|
+ response = requests.post(f"{API_URL}/contacts", headers=headers, json=payload)
|
|
|
+ LOGGER.info(f"{email} 注册同步到omni平台: ({response.status_code})")
|
|
|
+ return response.status_code, response.json()
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def close_omni_subscribers(email):
|
|
|
+ """
|
|
|
+ 关闭 Omnisend 订阅
|
|
|
+ @param email: 用户邮箱
|
|
|
+ @return: 成功返回 True,失败返回 False
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ payload = {
|
|
|
+ "updateEnabled": True,
|
|
|
+ "identifiers": [
|
|
|
+ {
|
|
|
+ "type": "email",
|
|
|
+ "id": email,
|
|
|
+ "channels": {
|
|
|
+ "email": {
|
|
|
+ "status": "unsubscribed",
|
|
|
+ "statusDate": datetime.utcnow().isoformat() + "Z",
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "customProperties": {
|
|
|
+ "source": "User Unsubscription",
|
|
|
+ "updatedAt": datetime.utcnow().isoformat() + "Z",
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ headers = {
|
|
|
+ "X-API-KEY": OMNI_API_KEY,
|
|
|
+ "accept": "application/json",
|
|
|
+ "content-type": "application/json"
|
|
|
+ }
|
|
|
+
|
|
|
+ response = requests.patch(
|
|
|
+ f"{API_URL}/contacts?email={email}",
|
|
|
+ headers=headers,
|
|
|
+ json=payload
|
|
|
+ )
|
|
|
+
|
|
|
+ LOGGER.info(f"Omnisend 取消订阅 {email}: {response.status_code}")
|
|
|
+
|
|
|
+ if response.status_code in [200, 201]:
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ LOGGER.error(f"Omnisend API 失败: {response.text}")
|
|
|
+ return False
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error(f"关闭订阅异常 {email}: {str(e)}")
|
|
|
+ return False
|