From 2c16f10a62a4dfabe78ee9b71c67bd6839a1b55b Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 11 Feb 2013 16:21:14 +0530 Subject: [PATCH] added date in pop3 mails --- core/doctype/communication/communication.py | 4 +++- webnotes/utils/__init__.py | 18 +++++++++--------- webnotes/utils/email_lib/receive.py | 13 +++++++------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/core/doctype/communication/communication.py b/core/doctype/communication/communication.py index f2b846655b..cefec7bba9 100644 --- a/core/doctype/communication/communication.py +++ b/core/doctype/communication/communication.py @@ -41,7 +41,7 @@ def get_customer_supplier(args=None): def make(doctype=None, name=None, content=None, subject=None, sender=None, recipients=None, contact=None, lead=None, communication_medium="Email", send_email=False, print_html=None, - attachments='[]', send_me_a_copy=False, set_lead=True): + attachments='[]', send_me_a_copy=False, set_lead=True, date=None): # add to Communication sent_via = None @@ -53,6 +53,8 @@ def make(doctype=None, name=None, content=None, subject=None, d.recipients = recipients d.lead = lead d.contact = contact + if date: + d.creation = date if doctype: sent_via = webnotes.get_obj(doctype, name) d.fields[doctype.replace(" ", "_").lower()] = name diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index 7fd21d42ce..4c803206d8 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -186,20 +186,20 @@ def time_diff_in_seconds(string_ed_date, string_st_date): return (get_datetime(string_ed_date) - get_datetime(string_st_date)).seconds def now_datetime(): - global user_time_zone from datetime import datetime - from pytz import timezone - - # get localtime + return convert_utc_to_user_timezone(datetime.utcnow()) + +def get_user_time_zone(): + global user_time_zone if not user_time_zone: user_time_zone = webnotes.conn.get_value('Control Panel', None, 'time_zone') \ or 'Asia/Calcutta' + return user_time_zone - # convert to UTC - utcnow = timezone('UTC').localize(datetime.utcnow()) - - # convert to user time zone - return utcnow.astimezone(timezone(user_time_zone)) +def convert_utc_to_user_timezone(utc_timestamp): + from pytz import timezone + utcnow = timezone('UTC').localize(utc_timestamp) + return utcnow.astimezone(timezone(get_user_time_zone())) def now(): """return current datetime as yyyy-mm-dd hh:mm:ss""" diff --git a/webnotes/utils/email_lib/receive.py b/webnotes/utils/email_lib/receive.py index 595d9ea61b..73a6de2edf 100644 --- a/webnotes/utils/email_lib/receive.py +++ b/webnotes/utils/email_lib/receive.py @@ -21,18 +21,17 @@ # from __future__ import unicode_literals -from webnotes.utils import extract_email_id +from webnotes.utils import extract_email_id, convert_utc_to_user_timezone class IncomingMail: """ Single incoming email object. Extracts, text / html and attachments from the email """ def __init__(self, content): - """ - Parse the incoming mail content - """ import email - from email.utils import parseaddr + import time + import datetime + import email.utils self.mail = email.message_from_string(content) @@ -42,7 +41,9 @@ class IncomingMail: self.parse() self.set_content_and_type() self.from_email = extract_email_id(self.mail["From"]) - self.from_real_name = parseaddr(self.mail["From"])[0] + self.from_real_name = email.utils(self.mail["From"])[0] + utc = email.utils.mktime_tz(email.utils.parsedate_tz(self.mail["Date"])) + self.date = convert_utc_to_user_timezone(utc).strftime('%Y-%m-%d %H:%M:%S') def parse(self): for part in self.mail.walk():