-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbg_job.py
124 lines (100 loc) · 4.03 KB
/
bg_job.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "SmartForm.settings")
# initialize Django
django.setup()
from django.db.models import Q
# import your Django models and components here
from smart_emailApp.models import Emails, EmailTask, ErrorReport
from smart_emailApp.models import Emails, EmailTask, MyJobModel, Link
from datetime import datetime
import pytz
from scheduler.send_email import buildEmail
# def check_for_task():
#
# # Get the current time in UTC
# now_utc = datetime.now(pytz.utc)
#
# # Convert UTC time to Eastern Standard Time (EST)
# eastern = pytz.timezone('US/Eastern')
# now = now_utc.astimezone(eastern)
#
# # Print the EST time in a specific format
# print(now.strftime('%Y-%m-%d %H:%M:%S %Z'))
# print(now)
#
#
# emailtasks = EmailTask.objects.filter(Q(status='Not Scheduled') | Q(status='Scheduled'))
#
#
# for emailtask in emailtasks:
# if emailtask.status in ["Not Scheduled", "Scheduled"]:
# try:
# #email = EmailTask.objects.filter()
# print(emailtask)
# task_name =emailtask.task_name
# occurence = emailtask.task_occurence
# run_from = emailtask.date_from
# run_to = emailtask.date_to_sending
# print(f"########### Email ID of the email to send "+str(emailtask.emailToSend))
#
# buildEmail(emailtask.id, emailtask.emailToSend.id, )
#
# emailtasks.update(status="Scheduled")
# print("Changed to Scheduled")
# return emailtask.id
# except:
# print("Error in 'check_for_task'function ")
# pass
#
# if emailtask.date_to_sending <= now:
# print("Expired")
# emailtasks.update(status="Expired")
# print("Changed to Expired")
# return emailtask.id
# else:
# print("Nothing to run###")
def check_for_task():
# Get the current time in UTC
now_utc = datetime.now(pytz.utc)
# Convert UTC time to Eastern Standard Time (EST)
eastern = pytz.timezone('US/Eastern')
now = now_utc.astimezone(eastern)
# Print the EST time in a specific format
print(now.strftime('%Y-%m-%d %H:%M:%S %Z'))
print(now)
emailtasks = EmailTask.objects.filter(Q(status='Not Scheduled') | Q(status='Scheduled'))
for emailtask in emailtasks:
if emailtask.status in ["Not Scheduled", "Scheduled"]:
try:
# check if the email has already been sent today
if emailtask.last_sent_date and emailtask.last_sent_date == now.date():
print("Email has already been sent today, skipping...")
continue
print(emailtask)
task_name = emailtask.task_name
# occurence = emailtask.task_occurence
# run_from = emailtask.date_from
# run_to = emailtask.date_to_sending
print(f"########### Email ID of the email to send "+str(emailtask.emailToSend))
buildEmail(emailtask.id, emailtask.emailToSend.id)
# Save the job details to the database
my_job = MyJobModel(name=task_name, decription = "ran on"+str(now))
my_job.save()
print("Task saved")
# update the last sent date to today's date
emailtask.last_sent_date = now.date()
emailtask.save()
except:
print("Error in 'check_for_task'function ")
err = ErrorReport(name ="check_for_task",decription=" Error in check_for_task() in the bg_job.py")
err.save()
print("Error saved")
if emailtask.date_to_sending <= now:
print("Expired")
emailtasks.update(status="Expired")
print("Changed to Expired")
return emailtask.id
else:
print("Nothing to run###")
check_for_task()