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
=== modified file 'pxgo_openoffice_reports/openoffice_report.py'
--- pxgo_openoffice_reports/openoffice_report.py 2010-08-19 13:35:39 +0000
+++ pxgo_openoffice_reports/openoffice_report.py 2010-10-25 10:51:06 +0000
@@ -35,7 +35,12 @@
35from os.path import splitext35from os.path import splitext
36from tools.translate import _36from tools.translate import _
37from oo_template import OOTemplate, OOTemplateException37from oo_template import OOTemplate, OOTemplateException
38import mx.DateTime
39import time
3840
41DT_FORMAT = '%Y-%m-%d'
42DHM_FORMAT = '%Y-%m-%d %H:%M:%S'
43HM_FORMAT = '%H:%M:%S'
3944
4045
41class OOReportException(Exception):46class OOReportException(Exception):
@@ -49,7 +54,7 @@
49 return self.message54 return self.message
5055
5156
52class OOReport:57class OOReport(object):
53 """58 """
54 OpenOffice/Relatorio based report.59 OpenOffice/Relatorio based report.
55 """60 """
@@ -69,10 +74,36 @@
69 self.data = data74 self.data = data
70 self.model = self.data['model']75 self.model = self.data['model']
71 self.context = context or {}76 self.context = context or {}
72 self.dbname = self.cr.dbname77 self.dbname = cr.dbname
73 self.pool = pooler.get_pool( self.cr.dbname )78 self.pool = pooler.get_pool( cr.dbname )
74 self.openoffice_port = 810079 self.openoffice_port = 8100
75 self.autostart_openoffice = True80 self.autostart_openoffice = True
81 self.lang_dict_called = False
82 self.lang_dict = {}
83 self.default_lang = {}
84
85
86 def get_report_context(self):
87 """
88 Returns the context for the report
89 (Template method pattern)
90 """
91 return {}
92
93
94 def _get_lang_dict(self):
95 """
96 Helper function that returns a function that
97 sets the language for one object using the current database
98 connection (cr) and user (uid).
99 """
100 pool_lang = self.pool.get('res.lang')
101 lang = self.context.get('lang', False) or 'en_US'
102 lang_ids = pool_lang.search(self.cr, self.uid, [('code','=',lang)])[0]
103 lang_obj = pool_lang.browse(self.cr, self.uid, lang_ids)
104 self.lang_dict.update({'lang_obj':lang_obj,'date_format':lang_obj.date_format,'time_format':lang_obj.time_format})
105 self.default_lang[lang] = self.lang_dict.copy()
106 return True
76107
77108
78 def execute(self, output_format='pdf', report_file_name=None):109 def execute(self, output_format='pdf', report_file_name=None):
@@ -179,6 +210,47 @@
179 return (data, 'image/png')210 return (data, 'image/png')
180211
181212
213 def _formatLang():
214 """
215 Helper function that returns a function that
216 formats received value to the language format.
217 """
218 def _format_lang(value, digits=2, date=False, date_time=False, grouping=True, monetary=False):
219 """
220 formats received value to the language format
221 """
222 if isinstance(value, (str, unicode)) and not value:
223 return ''
224 if not self.lang_dict_called:
225 self._get_lang_dict()
226 self.lang_dict_called = True
227
228 if date or date_time:
229 if not str(value):
230 return ''
231 date_format = self.lang_dict['date_format']
232 parse_format = DT_FORMAT
233 if date_time:
234 date_format = date_format + " " + self.lang_dict['time_format']
235 parse_format = DHM_FORMAT
236
237 if not isinstance(value, time.struct_time):
238 try:
239 date = mx.DateTime.strptime(str(value),parse_format)
240 except:# sometimes it takes converted values into value, so we dont need conversion.
241 return str(value)
242 else:
243 date = mx.DateTime.DateTime(*(value.timetuple()[:6]))
244 return date.strftime(date_format)
245 return self.lang_dict['lang_obj'].format('%.' + str(digits) + 'f', value, grouping=grouping, monetary=monetary)
246 return _format_lang
247
248
249 def _format(text, oldtag=None):
250 """
251 Removes white spaces
252 """
253 return text.strip()
182254
183255
184 def _gettext(lang):256 def _gettext(lang):
@@ -190,8 +262,6 @@
190 """262 """
191 Returns the translation of one string263 Returns the translation of one string
192 """264 """
193 cr = pooler.get_db(self.dbname).cursor()
194 context = { 'lang': lang }
195 return _(text)265 return _(text)
196 return gettext266 return gettext
197267
@@ -226,8 +296,6 @@
226 context['logo'] = user.company_id and user.company_id.logo296 context['logo'] = user.company_id and user.company_id.logo
227 context['lang'] = context.get('lang') or (user.company_id and user.company_id.partner_id and user.company_id.partner_id.lang)297 context['lang'] = context.get('lang') or (user.company_id and user.company_id.partner_id and user.company_id.partner_id.lang)
228298
229 # TODO: add a translate helper like the one rml reports use,
230
231 #299 #
232 # Add some helper function aliases300 # Add some helper function aliases
233 #301 #
@@ -240,6 +308,11 @@
240 context['chart'] = context.get('chart') or _chart_template_to_image308 context['chart'] = context.get('chart') or _chart_template_to_image
241 context['gettext'] = context.get('gettext') or _gettext(context.get('lang'))309 context['gettext'] = context.get('gettext') or _gettext(context.get('lang'))
242 context['_'] = context.get('_') or _gettext(context.get('lang'))310 context['_'] = context.get('_') or _gettext(context.get('lang'))
311 context['formatLang'] = context.get('formatLang') or _formatLang()
312 context['format'] = _format
313
314 # Update the context with the custom report context
315 context.update(self.get_report_context())
243316
244 #317 #
245 # Process the template using the OpenOffice/Relatorio engine318 # Process the template using the OpenOffice/Relatorio engine
@@ -248,7 +321,6 @@
248321
249 return data322 return data
250323
251
252 def process_template(self, template_file, output_format, context=None):324 def process_template(self, template_file, output_format, context=None):
253 """325 """
254 Will process a relatorio template and return the name326 Will process a relatorio template and return the name
@@ -306,13 +378,8 @@
306 """378 """
307 name = self.name379 name = self.name
308380
309 if self.parser:381 reportClass = self.parser or OOReport
310 options = self.parser(cr, uid, ids, datas, context)382
311 ids = options.get('ids', ids)
312 name = options.get('name', self.name)
313 # Use model defined in openoffice_report definition. Necesary for menu entries.
314 datas['model'] = options.get('model', self.model)
315 datas['records'] = options.get('records', [])
316383
317 self._context.update(context)384 self._context.update(context)
318385
@@ -330,14 +397,11 @@
330 #397 #
331 # Get the report398 # Get the report
332 #399 #
333 rpt = OOReport( name, cr, uid, ids, datas, self._context )400 rpt = reportClass( name, cr, uid, ids, datas, self._context )
334 return (rpt.execute(output_format=output_format), output_format )401 return (rpt.execute(output_format=output_format), output_format )
335402
336403
337404
338
339
340
341#405#
342# Ugly hack to avoid developers the need to register reports. ------------------406# Ugly hack to avoid developers the need to register reports. ------------------
343# Based on NaN jasper_reports module.407# Based on NaN jasper_reports module.
@@ -371,11 +435,11 @@
371 value = OLD_REGISTER_ALL(db)435 value = OLD_REGISTER_ALL(db)
372436
373 #437 #
374 # Search for reports with 'oo-<something>' type438 # Search for reports with 'oo-<something>' type and auto set to True
375 # and register them439 # and register them
376 #440 #
377 cr = db.cursor()441 cr = db.cursor()
378 cr.execute("SELECT * FROM ir_act_report_xml WHERE report_type LIKE 'oo-%' ORDER BY id")442 cr.execute("SELECT * FROM ir_act_report_xml WHERE report_type LIKE 'oo-%' and auto=True ORDER BY id")
379 records = cr.dictfetchall()443 records = cr.dictfetchall()
380 cr.close()444 cr.close()
381 for record in records:445 for record in records: