Merge lp:~omar7r/openobject-addons/extra-5.0-pxgo-openoffice-reports-imp into lp:openobject-addons/extra-5.0

Proposed by Omar (Pexego)
Status: Merged
Merged at revision: 4623
Proposed branch: lp:~omar7r/openobject-addons/extra-5.0-pxgo-openoffice-reports-imp
Merge into: lp:openobject-addons/extra-5.0
Diff against target: 196 lines (+85/-21)
1 file modified
pxgo_openoffice_reports/openoffice_report.py (+85/-21)
To merge this branch: bzr merge lp:~omar7r/openobject-addons/extra-5.0-pxgo-openoffice-reports-imp
Reviewer Review Type Date Requested Status
Borja López Soilán (NeoPolus) Approve
Review via email: mp+39259@code.launchpad.net

Description of the change

Add to pxgo_openoffice_reports the context lang translation possibility (formatLang(),format()) and auto="False" possibility to define the parser.

To post a comment you must log in.
Revision history for this message
Borja López Soilán (NeoPolus) (borjals) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pxgo_openoffice_reports/openoffice_report.py'
2--- pxgo_openoffice_reports/openoffice_report.py 2010-08-19 13:35:39 +0000
3+++ pxgo_openoffice_reports/openoffice_report.py 2010-10-25 10:51:06 +0000
4@@ -35,7 +35,12 @@
5 from os.path import splitext
6 from tools.translate import _
7 from oo_template import OOTemplate, OOTemplateException
8+import mx.DateTime
9+import time
10
11+DT_FORMAT = '%Y-%m-%d'
12+DHM_FORMAT = '%Y-%m-%d %H:%M:%S'
13+HM_FORMAT = '%H:%M:%S'
14
15
16 class OOReportException(Exception):
17@@ -49,7 +54,7 @@
18 return self.message
19
20
21-class OOReport:
22+class OOReport(object):
23 """
24 OpenOffice/Relatorio based report.
25 """
26@@ -69,10 +74,36 @@
27 self.data = data
28 self.model = self.data['model']
29 self.context = context or {}
30- self.dbname = self.cr.dbname
31- self.pool = pooler.get_pool( self.cr.dbname )
32+ self.dbname = cr.dbname
33+ self.pool = pooler.get_pool( cr.dbname )
34 self.openoffice_port = 8100
35 self.autostart_openoffice = True
36+ self.lang_dict_called = False
37+ self.lang_dict = {}
38+ self.default_lang = {}
39+
40+
41+ def get_report_context(self):
42+ """
43+ Returns the context for the report
44+ (Template method pattern)
45+ """
46+ return {}
47+
48+
49+ def _get_lang_dict(self):
50+ """
51+ Helper function that returns a function that
52+ sets the language for one object using the current database
53+ connection (cr) and user (uid).
54+ """
55+ pool_lang = self.pool.get('res.lang')
56+ lang = self.context.get('lang', False) or 'en_US'
57+ lang_ids = pool_lang.search(self.cr, self.uid, [('code','=',lang)])[0]
58+ lang_obj = pool_lang.browse(self.cr, self.uid, lang_ids)
59+ self.lang_dict.update({'lang_obj':lang_obj,'date_format':lang_obj.date_format,'time_format':lang_obj.time_format})
60+ self.default_lang[lang] = self.lang_dict.copy()
61+ return True
62
63
64 def execute(self, output_format='pdf', report_file_name=None):
65@@ -179,6 +210,47 @@
66 return (data, 'image/png')
67
68
69+ def _formatLang():
70+ """
71+ Helper function that returns a function that
72+ formats received value to the language format.
73+ """
74+ def _format_lang(value, digits=2, date=False, date_time=False, grouping=True, monetary=False):
75+ """
76+ formats received value to the language format
77+ """
78+ if isinstance(value, (str, unicode)) and not value:
79+ return ''
80+ if not self.lang_dict_called:
81+ self._get_lang_dict()
82+ self.lang_dict_called = True
83+
84+ if date or date_time:
85+ if not str(value):
86+ return ''
87+ date_format = self.lang_dict['date_format']
88+ parse_format = DT_FORMAT
89+ if date_time:
90+ date_format = date_format + " " + self.lang_dict['time_format']
91+ parse_format = DHM_FORMAT
92+
93+ if not isinstance(value, time.struct_time):
94+ try:
95+ date = mx.DateTime.strptime(str(value),parse_format)
96+ except:# sometimes it takes converted values into value, so we dont need conversion.
97+ return str(value)
98+ else:
99+ date = mx.DateTime.DateTime(*(value.timetuple()[:6]))
100+ return date.strftime(date_format)
101+ return self.lang_dict['lang_obj'].format('%.' + str(digits) + 'f', value, grouping=grouping, monetary=monetary)
102+ return _format_lang
103+
104+
105+ def _format(text, oldtag=None):
106+ """
107+ Removes white spaces
108+ """
109+ return text.strip()
110
111
112 def _gettext(lang):
113@@ -190,8 +262,6 @@
114 """
115 Returns the translation of one string
116 """
117- cr = pooler.get_db(self.dbname).cursor()
118- context = { 'lang': lang }
119 return _(text)
120 return gettext
121
122@@ -226,8 +296,6 @@
123 context['logo'] = user.company_id and user.company_id.logo
124 context['lang'] = context.get('lang') or (user.company_id and user.company_id.partner_id and user.company_id.partner_id.lang)
125
126- # TODO: add a translate helper like the one rml reports use,
127-
128 #
129 # Add some helper function aliases
130 #
131@@ -240,6 +308,11 @@
132 context['chart'] = context.get('chart') or _chart_template_to_image
133 context['gettext'] = context.get('gettext') or _gettext(context.get('lang'))
134 context['_'] = context.get('_') or _gettext(context.get('lang'))
135+ context['formatLang'] = context.get('formatLang') or _formatLang()
136+ context['format'] = _format
137+
138+ # Update the context with the custom report context
139+ context.update(self.get_report_context())
140
141 #
142 # Process the template using the OpenOffice/Relatorio engine
143@@ -248,7 +321,6 @@
144
145 return data
146
147-
148 def process_template(self, template_file, output_format, context=None):
149 """
150 Will process a relatorio template and return the name
151@@ -306,13 +378,8 @@
152 """
153 name = self.name
154
155- if self.parser:
156- options = self.parser(cr, uid, ids, datas, context)
157- ids = options.get('ids', ids)
158- name = options.get('name', self.name)
159- # Use model defined in openoffice_report definition. Necesary for menu entries.
160- datas['model'] = options.get('model', self.model)
161- datas['records'] = options.get('records', [])
162+ reportClass = self.parser or OOReport
163+
164
165 self._context.update(context)
166
167@@ -330,14 +397,11 @@
168 #
169 # Get the report
170 #
171- rpt = OOReport( name, cr, uid, ids, datas, self._context )
172+ rpt = reportClass( name, cr, uid, ids, datas, self._context )
173 return (rpt.execute(output_format=output_format), output_format )
174
175
176
177-
178-
179-
180 #
181 # Ugly hack to avoid developers the need to register reports. ------------------
182 # Based on NaN jasper_reports module.
183@@ -371,11 +435,11 @@
184 value = OLD_REGISTER_ALL(db)
185
186 #
187- # Search for reports with 'oo-<something>' type
188+ # Search for reports with 'oo-<something>' type and auto set to True
189 # and register them
190 #
191 cr = db.cursor()
192- cr.execute("SELECT * FROM ir_act_report_xml WHERE report_type LIKE 'oo-%' ORDER BY id")
193+ cr.execute("SELECT * FROM ir_act_report_xml WHERE report_type LIKE 'oo-%' and auto=True ORDER BY id")
194 records = cr.dictfetchall()
195 cr.close()
196 for record in records: