This commit is contained in:
Tibor Bossanyi (Freelancer)
2021-12-01 00:24:31 +01:00
parent 3a429e714b
commit a18112cf5e
11 changed files with 500 additions and 58 deletions
@@ -16,6 +16,7 @@ class Customer(models.Model):
synced_date = models.DateTimeField(blank=True,null=True)
firebase_reg_token = models.CharField(max_length=255, blank=True, null=True)
lang = models.CharField(max_length=5, blank=True, null=True)
trial_date = models.DateTimeField(blank=True,null=True)
def has_add_permission(self, request):
return False
@@ -1,7 +1,7 @@
import os
from firebase_admin import initialize_app
BACKOFFICE_VERSION = "1.33"
BACKOFFICE_VERSION = "1.34"
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -1,7 +1,7 @@
import os
from firebase_admin import initialize_app
BACKOFFICE_VERSION = "1.33"
BACKOFFICE_VERSION = "1.34"
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -1,7 +1,7 @@
import os
from firebase_admin import initialize_app
BACKOFFICE_VERSION = "1.33"
BACKOFFICE_VERSION = "1.34"
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -2,9 +2,15 @@
{% block object-tools %}
<div>
<form action="mautic/" method="POST">
<form action="mautic_lang/" method="POST">
{% csrf_token %}
<button type="submit">Mautic Sync</button>
<button type="submit">Mautic Lang Sync</button>
</form>
</div>
<div>
<form action="mautic_trial/" method="POST">
{% csrf_token %}
<button type="submit">Mautic Trial Sync</button>
</form>
</div>
<div>
@@ -77,7 +77,8 @@ class CustomerAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path('mautic/', self.run_mautic),
path('mautic_lang/', self.run_mautic),
path('mautic_trial/', self.run_mautic_trial),
path('notif/', self.run_notif),
]
return my_urls + urls
@@ -89,7 +90,12 @@ class CustomerAdmin(admin.ModelAdmin):
def run_mautic(self, request):
mautic = Mautic()
mautic.syncLang()
return HttpResponseRedirect("../")
return HttpResponseRedirect("../")
def run_mautic_trial(self, request):
mautic = Mautic()
mautic.syncTrial()
return HttpResponseRedirect("../")
admin.site.register(Customer, CustomerAdmin)
@@ -52,6 +52,40 @@ class Mautic:
print( "Syncronised customer count: " + str(index))
return True
def syncTrial(self):
qs = Customer.objects.raw('SELECT * from customer WHERE trial_date is not null and trial_date_sync is null')
logger = logging.getLogger(__name__)
logger.info("Syncronising trial date...")
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[trialdate]=" + str(customer.trial_date) + \
"&mauticform[formId]=3" + \
"&mauticform[formName]=appdatachange"
print(data)
form_url = 'https://mautic.aitrainer.app/form/submit?formId=3'
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 trial_date_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 sync(self):
@@ -1,4 +1,5 @@
from ..automation.notification import NotificationExec
from ..automation.mautic import Mautic
from django_cron import CronJobBase, Schedule
import datetime
@@ -25,3 +26,16 @@ class NotificationJob(CronJobBase):
print(datetime.datetime.now(), " *** END notification ")
class TrialJob(CronJobBase):
RUN_EVERY_MINS = 60
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'aitrainer_backoffice.controlling.trial_sync' # a unique code
def do(self):
print(datetime.datetime.now(), " *** START trial sync ")
mautic = Mautic()
mautic.syncTrial()
print(datetime.datetime.now(), " *** END trial sync ")