You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.5 KiB

7 years ago
  1. # -*- coding: utf-8 -*-
  2. ###################################################################################
  3. #
  4. # Copyright (C) 2018 MuK IT GmbH
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ###################################################################################
  20. import re
  21. import pytz
  22. import hashlib
  23. import logging
  24. import psycopg2
  25. from odoo import _
  26. from odoo import models, api, fields
  27. from odoo.tools import ustr, pycompat, human_size
  28. _logger = logging.getLogger(__name__)
  29. _read_group_process_groupby = models.BaseModel._read_group_process_groupby
  30. def _read_group_process_groupby_hour(self, gb, query):
  31. split = gb.split(':')
  32. field_type = self._fields[split[0]].type
  33. gb_function = split[1] if len(split) == 2 else None
  34. temporal = field_type in ('date', 'datetime')
  35. tz_convert = field_type == 'datetime' and self._context.get('tz') in pytz.all_timezones
  36. qualified_field = self._inherits_join_calc(self._table, split[0], query)
  37. if temporal and gb_function in ['hour']:
  38. if tz_convert:
  39. qualified_field = "timezone('%s', timezone('UTC',%s))" % (self._context.get('tz', 'UTC'), qualified_field)
  40. qualified_field = "date_trunc('%s', %s)" % (gb_function or 'month', qualified_field)
  41. if field_type == 'boolean':
  42. qualified_field = "coalesce(%s,false)" % qualified_field
  43. return {
  44. 'field': split[0],
  45. 'groupby': gb,
  46. 'type': field_type,
  47. 'display_format': 'hh:00 dd MMM',
  48. 'interval': dateutil.relativedelta.relativedelta(hours=1),
  49. 'tz_convert': tz_convert,
  50. 'qualified_field': qualified_field
  51. }
  52. return _read_group_process_groupby(self, gb, query)
  53. models.BaseModel._read_group_process_groupby = _read_group_process_groupby_hour