From 4b5015469d605def72e953dd6f98c5735f229f02 Mon Sep 17 00:00:00 2001 From: dhr-odoo Date: Tue, 10 Jun 2014 14:26:08 +0530 Subject: [PATCH] [FIX] survey: missing timezone conversions As the survey hardcode lot's of views and reports in python, we do not beneficiate from automatic timezone conversions in the webclient. Hopefully this is fixed in v8 after refactoring of the survey module. opw 608786 --- addons/survey/report/survey_browse_response.py | 8 +++++++- addons/survey/survey.py | 8 +++++++- addons/survey/wizard/survey_answer.py | 6 +++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/addons/survey/report/survey_browse_response.py b/addons/survey/report/survey_browse_response.py index 91e534877e7..fc9f5e5496d 100644 --- a/addons/survey/report/survey_browse_response.py +++ b/addons/survey/report/survey_browse_response.py @@ -22,6 +22,8 @@ import time +import pytz +from datetime import datetime from openerp import pooler, tools from openerp.report import report_sxw from openerp.report.interface import report_rml @@ -30,6 +32,8 @@ from openerp.tools.translate import _ class survey_browse_response(report_rml): def create(self, cr, uid, ids, datas, context): + if context is None: + context = {} _divide_columns_for_matrix = 0.7 _display_ans_in_rows = 5 _pageSize = ('29.7cm','21.1cm') @@ -204,7 +208,9 @@ class survey_browse_response(report_rml): for survey in surv_obj.browse(cr, uid, [response.survey_id.id]): tbl_width = float(_tbl_widths.replace('cm', '')) colwidth = "2.5cm,4.8cm," + str(tbl_width - 15.0) +"cm,3.2cm,4.5cm" - resp_create = tools.ustr(time.strftime('%d-%m-%Y %I:%M:%S %p', time.strptime(response.date_create.split('.')[0], '%Y-%m-%d %H:%M:%S'))) + timezone = pytz.timezone(context.get('tz') or 'UTC') + create_date = pytz.UTC.localize(datetime.strptime(response.date_create.split('.')[0], tools.DEFAULT_SERVER_DATETIME_FORMAT)) + resp_create = create_date.astimezone(timezone).strftime("%Y-%m-%d %H:%M:%S") #Converting date to user's timezone rml += """ """ + _('Print Date : ') + """ diff --git a/addons/survey/survey.py b/addons/survey/survey.py index 6e7198ae591..e03962487ac 100644 --- a/addons/survey/survey.py +++ b/addons/survey/survey.py @@ -20,6 +20,7 @@ ############################################################################## import copy +import pytz from datetime import datetime from dateutil.relativedelta import relativedelta from time import strftime @@ -674,10 +675,15 @@ class survey_response(osv.osv): def name_get(self, cr, uid, ids, context=None): if not len(ids): return [] + if context is None: + context = {} reads = self.read(cr, uid, ids, ['user_id','date_create'], context=context) res = [] for record in reads: - name = (record['user_id'] and record['user_id'][1] or '' )+ ' (' + record['date_create'].split('.')[0] + ')' + timezone = pytz.timezone(context.get('tz') or 'UTC') + create_date = pytz.UTC.localize(datetime.strptime(record['date_create'].split('.')[0], tools.DEFAULT_SERVER_DATETIME_FORMAT)) + localized_create_date = create_date.astimezone(timezone) + name = (record['user_id'] and record['user_id'][1] or '' )+ ' (' + localized_create_date.strftime("%Y-%m-%d %H:%M:%S") + ')' res.append((record['id'], name)) return res diff --git a/addons/survey/wizard/survey_answer.py b/addons/survey/wizard/survey_answer.py index aa7f199367b..c87a87c3e77 100644 --- a/addons/survey/wizard/survey_answer.py +++ b/addons/survey/wizard/survey_answer.py @@ -20,6 +20,7 @@ ############################################################################## import base64 +import pytz import datetime from lxml import etree import os @@ -171,7 +172,10 @@ class survey_question_wiz(osv.osv_memory): # TODO: l10n, cleanup this code to make it readable. Or template? xml_group = etree.SubElement(xml_form, 'group', {'col': '40', 'colspan': '4'}) record = sur_response_obj.browse(cr, uid, context['response_id'][context['response_no']]) - etree.SubElement(xml_group, 'label', {'string': to_xml(tools.ustr(_('Answer Of :- ') + record.user_id.name + _(', Date :- ') + record.date_create.split('.')[0] )), 'align':"0.0"}) + timezone = pytz.timezone(context.get('tz') or 'UTC') + response_date = pytz.UTC.localize(datetime.datetime.strptime(record['date_create'].split('.')[0], tools.DEFAULT_SERVER_DATETIME_FORMAT)) + localized_response_date = response_date.astimezone(timezone) + etree.SubElement(xml_group, 'label', {'string': to_xml(tools.ustr(_('Answer Of :- ') + record.user_id.name + _(', Date :- ') + localized_response_date.strftime("%Y-%m-%d %H:%M:%S") )), 'align':"0.0"}) etree.SubElement(xml_group, 'label', {'string': to_xml(tools.ustr(_(" Answer :- ") + str(context.get('response_no',0) + 1) +"/" + str(len(context.get('response_id',0))) )), 'align':"0.0"}) if context.get('response_no',0) > 0: etree.SubElement(xml_group, 'button', {'colspan':"1",'icon':"gtk-go-back",'name':"action_forward_previous",'string': tools.ustr("Previous Answer"),'type':"object"})