Browse Source

allow getting the language format from the user + only offset datetimes, not dates

pull/42/head
Vincent Vinet 10 years ago
parent
commit
27e444b5c2
  1. 50
      email_template_dateutil/email_template.py

50
email_template_dateutil/email_template.py

@ -35,33 +35,49 @@ from openerp.addons.email_template import email_template
@contextfilter @contextfilter
def format_date(context, dtstr, new_format, tz=None):
def format_date(context, dtstr, new_format=None, tz=None):
if not dtstr: if not dtstr:
return dtstr return dtstr
if not new_format:
if context.get("user") and context["user"].lang:
user = context["user"]
lang_obj = user.pool["res.lang"]
cr, uid = user._cr, user._uid
lang = lang_obj.search(cr, uid, [
('code', '=', user.lang)
])
if lang:
new_format = lang_obj.browse(cr, uid, lang[0]).date_format
new_format = new_format or DFMT
try: try:
date = datetime.strptime(dtstr, DTFMT) date = datetime.strptime(dtstr, DTFMT)
except ValueError: except ValueError:
# Maybe this is a date, not datetime # Maybe this is a date, not datetime
date = datetime.strptime(dtstr, DFMT) date = datetime.strptime(dtstr, DFMT)
if tz:
tz_name = tz
elif context.get("user") and context["user"].tz:
tz_name = context["user"].tz
else: else:
tz_name = context.get("ctx", {}).get("tz")
# If this was a date, calculating timezones will give unexpected
# results. Any timezone with a negative offset will be the day
# before, since (midnight - anything) is the previous day
if tz:
tz_name = tz
elif context.get("user") and context["user"].tz:
tz_name = context["user"].tz
else:
tz_name = context.get("ctx", {}).get("tz")
if tz_name:
try:
utc = pytz.timezone('UTC')
context_tz = pytz.timezone(tz_name)
utc_timestamp = utc.localize(date, is_dst=False) # UTC = no DST
date = utc_timestamp.astimezone(context_tz)
except Exception:
_logger.debug("failed to compute context/client-specific "
"timestamp, using the UTC value",
exc_info=True)
if tz_name:
try:
utc = pytz.timezone('UTC')
context_tz = pytz.timezone(tz_name)
utc_timestamp = utc.localize(date, is_dst=False) # UTC = no DST
date = utc_timestamp.astimezone(context_tz)
except Exception:
_logger.debug("failed to compute context/client-specific "
"timestamp, using the UTC value",
exc_info=True)
return date.strftime(new_format) return date.strftime(new_format)

Loading…
Cancel
Save