email digest fix

This commit is contained in:
Anand Doshi 2012-03-06 11:42:32 +05:30
parent 4e154d2233
commit bb64d302df

View File

@ -282,7 +282,8 @@ class DocType:
Returns start and end date depending on the frequency of email digest Returns start and end date depending on the frequency of email digest
""" """
from datetime import datetime, date, timedelta from datetime import datetime, date, timedelta
today = datetime.now().date() from webnotes.utils import now_datetime
today = now_datetime().date()
year, month, day = today.year, today.month, today.day year, month, day = today.year, today.month, today.day
if self.doc.frequency == 'Daily': if self.doc.frequency == 'Daily':
@ -409,6 +410,7 @@ class DocType:
from webnotes.utils.email_lib import sendmail from webnotes.utils.email_lib import sendmail
try: try:
#webnotes.msgprint('in send')
sendmail( sendmail(
recipients=recipient_list, recipients=recipient_list,
sender='notifications+email_digest@erpnext.com', sender='notifications+email_digest@erpnext.com',
@ -419,64 +421,22 @@ class DocType:
) )
except Exception, e: except Exception, e:
webnotes.msgprint('There was a problem in sending your email. Please contact support@erpnext.com') webnotes.msgprint('There was a problem in sending your email. Please contact support@erpnext.com')
#webnotes.errprint(webnotes.getTraceback()) webnotes.errprint(webnotes.getTraceback())
def get_next_sending(self): def get_next_sending(self):
"""
"""
# Get TimeZone
# Get System TimeZone
import time
from pytz import timezone
import datetime import datetime
import webnotes.defs
cp = webnotes.model.doc.Document('Control Panel','Control Panel')
app_tz = timezone(cp.time_zone)
server_tz = timezone(getattr(webnotes.defs, 'system_timezone'))
start_date, end_date = self.get_start_end_dates() start_date, end_date = self.get_start_end_dates()
new_date = end_date + datetime.timedelta(days=1) send_date = end_date + datetime.timedelta(days=1)
new_time = datetime.time(hour=6)
naive_dt = datetime.datetime.combine(new_date, new_time)
app_dt = app_tz.localize(naive_dt)
server_dt = server_tz.normalize(app_dt.astimezone(server_tz))
res = {
'app_dt': app_dt.replace(tzinfo=None),
'app_tz': app_tz,
'server_dt': server_dt.replace(tzinfo=None),
'server_tz': server_tz
}
from webnotes.utils import formatdate from webnotes.utils import formatdate
str_date = formatdate(str(res['app_dt'].date())) str_date = formatdate(str(send_date))
str_time = res['app_dt'].time().strftime('%I:%M')
self.doc.next_send = str_date + " at about " + str_time self.doc.next_send = str_date + " at midnight"
return res return send_date
def get_next_execution(self):
"""
"""
from datetime import datetime, timedelta
dt_args = self.get_next_sending()
server_dt = dt_args['server_dt']
now_dt = datetime.now(dt_args['server_tz'])
if now_dt.time() <= server_dt.time():
next_date = now_dt.date()
else:
next_date = now_dt.date() + timedelta(days=1)
next_time = server_dt.time()
return datetime.combine(next_date, next_time)
def onload(self): def onload(self):
@ -743,21 +703,16 @@ def send():
""", as_list=1) """, as_list=1)
from webnotes.model.code import get_obj from webnotes.model.code import get_obj
from datetime import datetime, timedelta from webnotes.utils import now_datetime
now = datetime.now()
now_date = now.date()
now_time = (now + timedelta(hours=2)).time()
now_date = now_datetime().date()
for ed in edigest_list: for ed in edigest_list:
if ed[0]: if ed[0]:
ed_obj = get_obj('Email Digest', ed[0]) ed_obj = get_obj('Email Digest', ed[0])
ed_obj.sending = True ed_obj.sending = True
dt_dict = ed_obj.get_next_sending() send_date = ed_obj.get_next_sending()
send_date = dt_dict['server_dt'].date() #webnotes.msgprint([ed[0], now_date, send_date])
send_time = dt_dict['server_dt'].time()
if (now_date == send_date): if (now_date == send_date):
#webnotes.msgprint('sending ' + ed_obj.doc.name)
ed_obj.send() ed_obj.send()
#else:
# webnotes.msgprint('not sending ' + ed_obj.doc.name)