BO 1.23 controlling, cron: sync customers
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from .helper import MauticHelper
|
||||
@@ -0,0 +1,71 @@
|
||||
import requests
|
||||
import logging
|
||||
from django.db import connections
|
||||
|
||||
from ..models.customer import Customer
|
||||
|
||||
|
||||
class MauticHelper:
|
||||
|
||||
|
||||
def sync(self):
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.info("Syncronising...")
|
||||
|
||||
last_synced_date = self.get_last_synced_date()
|
||||
|
||||
if len(last_synced_date) != 0:
|
||||
qs = Customer.objects.raw(
|
||||
'SELECT * from customer WHERE date_add > "' + last_synced_date + '" and synced_date is null')
|
||||
else:
|
||||
qs = Customer.objects.raw(
|
||||
'SELECT * from customer WHERE synced_date is null')
|
||||
|
||||
headers = {
|
||||
'content-type': "application/x-www-form-urlencoded",
|
||||
'cache-control': "no-cache"
|
||||
}
|
||||
index = 0
|
||||
for customer in qs:
|
||||
goal = customer.goal if customer.goal is not None else ""
|
||||
fitness_level = customer.fitness_level if customer.fitness_level is not None else ""
|
||||
data = "mauticform[email]=" + customer.email + \
|
||||
"&mauticform[f_name]=" + customer.name + \
|
||||
"&mauticform[firstname]=" + customer.firstname + \
|
||||
"&mauticform[goal]=" + goal + \
|
||||
"&mauticform[fitness_level]=" + fitness_level + \
|
||||
"&mauticform[subscribed]=" + str(customer.date_add) + \
|
||||
"&mauticform[database_id]=" + str(customer.customer_id) + \
|
||||
"&mauticform[formId]=1" + \
|
||||
"&mauticform[formName]=appsync"
|
||||
|
||||
print(data)
|
||||
|
||||
form_url = 'https://mautic.aitrainer.app/form/submit?formId=1'
|
||||
response = requests.post(form_url, data=data.encode('utf-8'), headers=headers)
|
||||
print(str(response.status_code))
|
||||
|
||||
if response.status_code == 200:
|
||||
with connections["live"].cursor() as cursor:
|
||||
cursor.execute("UPDATE customer SET synced_date = NOW() WHERE customer_id="
|
||||
+ str(customer.customer_id))
|
||||
#if index == 0:
|
||||
# break
|
||||
index = index + 1
|
||||
|
||||
logger.info("Syncronised customer count: " + str(index))
|
||||
|
||||
return True
|
||||
|
||||
def get_last_synced_date(self):
|
||||
qs = Customer.objects.raw('SELECT customer_id, max(synced_date) as synced_date from customer')
|
||||
for c in qs:
|
||||
if c.synced_date is None:
|
||||
return ""
|
||||
synced_date = c.synced_date.strftime('%Y-%m-%d')
|
||||
print(synced_date)
|
||||
return synced_date
|
||||
|
||||
def sync_frequent_users(self):
|
||||
print("SYNC FREQ USERS")
|
||||
return True
|
||||
Reference in New Issue
Block a user