예제 #1
0
    def attributes(self, record, field_name, options, values):
        attrs = super(DateTime, self).attributes(record, field_name, options,
                                                 values)
        value = record[field_name]

        lg = self.env['res.lang']._lang_get(self.env.user.lang)
        locale = babel.Locale.parse(lg.code)
        babel_format = value_format = posix_to_ldml(
            '%s %s' % (lg.date_format, lg.time_format), locale=locale)
        tz = record.env.context.get('tz') or self.env.user.tz

        if isinstance(value, pycompat.string_types):
            value = fields.Datetime.from_string(value)

        if value:
            # convert from UTC (server timezone) to user timezone
            value = fields.Datetime.context_timestamp(self.with_context(tz=tz),
                                                      timestamp=value)
            value_format = pycompat.to_text(
                babel.dates.format_datetime(value,
                                            format=babel_format,
                                            locale=locale))
            value = fields.Datetime.to_string(value)

        attrs['data-oe-original'] = value
        attrs['data-oe-original-with-format'] = value_format
        attrs['data-oe-original-tz'] = tz
        return attrs
예제 #2
0
    def value_to_html(self, value, options):
        if not value:
            return ''
        lang = self.user_lang()
        locale = babel.Locale.parse(lang.code)

        if isinstance(value, pycompat.string_types):
            value = fields.Datetime.from_string(value)

        value = fields.Datetime.context_timestamp(self, value)

        if options and 'format' in options:
            pattern = options['format']
        else:
            if options and options.get('time_only'):
                strftime_pattern = (u"%s" % (lang.time_format))
            else:
                strftime_pattern = (u"%s %s" % (lang.date_format, lang.time_format))

            pattern = posix_to_ldml(strftime_pattern, locale=locale)

        if options and options.get('hide_seconds'):
            pattern = pattern.replace(":ss", "").replace(":s", "")

        return pycompat.to_text(babel.dates.format_datetime(value, format=pattern, locale=locale))
예제 #3
0
    def attributes(self, record, field_name, options, values):
        attrs = super(Date, self).attributes(record, field_name, options,
                                             values)
        attrs['data-oe-original'] = record[field_name]

        if record._fields[field_name].type == 'datetime':
            attrs = self.env['ir.qweb.field.datetime'].attributes(
                record, field_name, options, values)
            attrs['data-oe-type'] = 'datetime'
            return attrs

        lg = self.env['res.lang']._lang_get(self.env.user.lang)
        locale = babel.Locale.parse(lg.code)
        babel_format = value_format = posix_to_ldml(lg.date_format,
                                                    locale=locale)

        if record[field_name]:
            date = fields.Date.from_string(record[field_name])
            value_format = pycompat.to_text(
                babel.dates.format_date(date,
                                        format=babel_format,
                                        locale=locale))

        attrs['data-oe-original-with-format'] = value_format
        return attrs
예제 #4
0
    def value_to_html(self, value, options):
        if not value:
            return ''
        options = options or {}

        lang = self.user_lang()
        locale = babel_locale_parse(lang.code)
        format_func = babel.dates.format_datetime
        if isinstance(value, str):
            value = fields.Datetime.from_string(value)

        value = fields.Datetime.context_timestamp(self, value)

        if options.get('tz_name'):
            tzinfo = babel.dates.get_timezone(options['tz_name'])
        else:
            tzinfo = None

        if 'format' in options:
            pattern = options['format']
        else:
            if options.get('time_only'):
                strftime_pattern = (u"%s" % (lang.time_format))
            elif options.get('date_only'):
                strftime_pattern = (u"%s" % (lang.date_format))
            else:
                strftime_pattern = (u"%s %s" % (lang.date_format, lang.time_format))

            pattern = posix_to_ldml(strftime_pattern, locale=locale)

        if options.get('hide_seconds'):
            pattern = pattern.replace(":ss", "").replace(":s", "")

        if options.get('time_only'):
            format_func = babel.dates.format_time
            return pycompat.to_text(format_func(value, format=pattern, locale=locale))
        if options.get('date_only'):
            format_func = babel.dates.format_date
            return pycompat.to_text(format_func(value, format=pattern, locale=locale))

        return pycompat.to_text(format_func(value, format=pattern, tzinfo=tzinfo, locale=locale))