From c98528d5f0dc8be8f8534bf2051bebf6426831df Mon Sep 17 00:00:00 2001 From: "Tibor Bossanyi (Freelancer)" Date: Thu, 2 Dec 2021 17:59:28 +0100 Subject: [PATCH] BO 1.34+6 no data sync --- Dockerfile | 2 + .../aitrainer_backoffice/settings/deploy.py | 2 +- .../controlling/automation/mautic.py | 69 +++++++++++++++++++ aitrainer_backoffice/controlling/cron/cron.py | 12 ++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 480cec8..bfc5d62 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,8 @@ RUN chmod 0644 /etc/cron.d/aitrainer-cron # Apply cron job RUN crontab /etc/cron.d/aitrainer-cron +RUN chmod +x /aitrainer_backoffice/cron.sh + RUN pip3 install uwsgi COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt diff --git a/aitrainer_backoffice/aitrainer_backoffice/settings/deploy.py b/aitrainer_backoffice/aitrainer_backoffice/settings/deploy.py index 71b98b1..bcbbc30 100644 --- a/aitrainer_backoffice/aitrainer_backoffice/settings/deploy.py +++ b/aitrainer_backoffice/aitrainer_backoffice/settings/deploy.py @@ -182,6 +182,6 @@ LOGGING = { CRON_CLASSES = [ 'controlling.cron.cron.NotificationJob', - 'controlling.cron.cron.TrialJob', 'controlling.cron.cron.LangJob', + 'controlling.cron.cron.NoDataJob', ] diff --git a/aitrainer_backoffice/controlling/automation/mautic.py b/aitrainer_backoffice/controlling/automation/mautic.py index 4b07505..0b1493a 100644 --- a/aitrainer_backoffice/controlling/automation/mautic.py +++ b/aitrainer_backoffice/controlling/automation/mautic.py @@ -13,6 +13,75 @@ else: class Mautic: + + def syncNoDataOldDownloaders(self): + qs = Customer.objects.raw('SELECT * FROM customer c left join exercises e on c.customer_id = e.customer_id WHERE trial_date_sync <= NOW() - INTERVAL 2 DAY AND e.exercise_id is null AND c.last_exercise_sync is null') + logger = logging.getLogger(__name__) + logger.info("Syncronising lang...") + + headers = { + 'content-type': "application/x-www-form-urlencoded", + 'cache-control': "no-cache" + } + index = 0 + for customer in qs: + data = "mauticform[email]=" + customer.email + \ + "&mauticform[database_id]=" + str(customer.customer_id) + \ + "&mauticform[lastexercise]=1900-01-01" + \ + "&mauticform[formId]=7" + \ + "&mauticform[formName]=appdatasync" + + print(data) + + form_url = 'https://mautic.aitrainer.app/form/submit?formId=7' + response = requests.post(form_url, data=data.encode('utf-8'), headers=headers) + print(customer.email + " " +str(response.status_code)) + + if response.status_code == 200: + with connections["live"].cursor() as cursor: + cursor.execute("UPDATE customer SET last_exercise_sync = NOW() WHERE customer_id=" + + str(customer.customer_id)) + print(f'customer {customer.customer_id} is updated') + + index = index + 1 + + print( "Syncronised customer count: " + str(index)) + return True + + + def syncNoDataNewSubscribes(self): + qs = Customer.objects.raw('SELECT * FROM customer c left join exercises e on c.customer_id = e.customer_id WHERE c.date_add <= NOW() - INTERVAL 2 DAY AND e.exercise_id is null AND c.last_exercise_sync is null') + logger = logging.getLogger(__name__) + logger.info("Syncronising lang...") + + headers = { + 'content-type': "application/x-www-form-urlencoded", + 'cache-control': "no-cache" + } + index = 0 + for customer in qs: + data = "mauticform[email]=" + customer.email + \ + "&mauticform[database_id]=" + str(customer.customer_id) + \ + "&mauticform[lastexercise]=1900-01-01" + \ + "&mauticform[formId]=7" + \ + "&mauticform[formName]=appdatasync" + + print(data) + + form_url = 'https://mautic.aitrainer.app/form/submit?formId=7' + response = requests.post(form_url, data=data.encode('utf-8'), headers=headers) + print(customer.email + " " +str(response.status_code)) + + if response.status_code == 200: + with connections["live"].cursor() as cursor: + cursor.execute("UPDATE customer SET last_exercise_sync = NOW() WHERE customer_id=" + + str(customer.customer_id)) + print(f'customer {customer.customer_id} is updated') + + index = index + 1 + + print( "Syncronised customer count: " + str(index)) + return True def syncLang(self): qs = Customer.objects.raw('SELECT * from customer WHERE lang is not null and lang_sync is null') diff --git a/aitrainer_backoffice/controlling/cron/cron.py b/aitrainer_backoffice/controlling/cron/cron.py index 959f367..a8350fc 100644 --- a/aitrainer_backoffice/controlling/cron/cron.py +++ b/aitrainer_backoffice/controlling/cron/cron.py @@ -39,3 +39,15 @@ class LangJob(CronJobBase): print(datetime.datetime.now(), " *** END Lang sync ") +class NoDataJob(CronJobBase): + RUN_EVERY_MINS = 60 + schedule = Schedule(run_every_mins=RUN_EVERY_MINS) + code = 'aitrainer_backoffice.controlling.nodata' # a unique code + + def do(self): + print(datetime.datetime.now(), " *** START NoData sync ") + mautic = Mautic() + mautic.syncNoDataNewSubscribes() + mautic.syncNoDataOldDownloaders() + print(datetime.datetime.now(), " *** END NoData sync ") +