Search code examples
odoo-8

AttributeError: 'datetime.date' object has no attribute 'hour' when creating an option - group by hour


I´m using odoo8 and want to create an option group by hour, I know that I should not mess with core odoo code but it is just add to lines (I hope so), I check an "pivot by hour" module V10 and there are added the same two lines.

in addons\point_of_sale\report\pos_order_report_view.xml I added:

<filter string="Order Hour" context="{'group_by':'date:hour'}" help="Hour of order date"/> 

and in server\openerp\modules.py in _read_group_process_groupby() method I added two lines:

def _read_group_process_groupby(self, gb, query, context):

   split = gb.split(':')
   field_type = self._fields[split[0]].type
   gb_function = split[1] if len(split) == 2 else None
   temporal = field_type in ('date', 'datetime')
   tz_convert = field_type == 'datetime' and context.get('tz') in pytz.all_timezones
   qualified_field = self._inherits_join_calc(self._table, split[0], query)
   if temporal:
       display_formats = {
           'hour': 'HH:mm dd MMM yyyy',                        ###### added by me
           'day': 'dd MMM yyyy',
           'week': "'W'w YYYY", 
           'month': 'MMMM yyyy',
           'quarter': 'QQQ yyyy',
           'year': 'yyyy',
       }
   time_intervals = {
       'hour': dateutil.relativedelta.relativedelta(hours=1),  ###### added by me
       'day': dateutil.relativedelta.relativedelta(days=1),
       'week': datetime.timedelta(days=7),
       'month': dateutil.relativedelta.relativedelta(months=1),
       'quarter': dateutil.relativedelta.relativedelta(months=3),
       'year': dateutil.relativedelta.relativedelta(years=1)
   }
   if tz_convert:
           qualified_field = "timezone('%s', timezone('UTC',%s))" % (context.get('tz', 'UTC'), 
qualified_field)
   qualified_field = "date_trunc('%s', %s)" % (gb_function or 'month', qualified_field)
   if field_type == 'boolean':
       qualified_field = "coalesce(%s,false)" % qualified_field
   return {
       'field': split[0],
       'groupby': gb,
       'type': field_type,
       'display_format': display_formats[gb_function or 'month'] if temporal else None,
       'interval': time_intervals[gb_function or 'month'] if temporal else None,
       'tz_convert': tz_convert,
       'qualified_field': qualified_field
   }

the whole traceback is the following:

enter image description here


Solution

  • In models.py (with path: server/openerp) in _read_group_format_result method

     .format_date(
    

    need to be changed to

     .format_datetime(