workouttest_backoffice/aitrainer_backoffice/controlling/automation/mautic.py
Tibor Bossanyi (Freelancer) 696ce5a368 V1.30 notification
2021-10-01 15:53:20 +02:00

77 lines
2.8 KiB
Python

import requests
import logging
from django.db import connections
import datetime
from ..models.customer import Customer
class Mautic:
def syncTrial(self):
tenDays = datetime.datetime - datetime.timedelta(days=10)
qs = Customer.objects.raw(
'SELECT * from customer WHERE trial_date < "' + tenDays + '" and trial_date is not null')
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 + '" or 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