【问题标题】:How to display external information on Odoo 11?如何在 Odoo 11 上显示外部信息?
【发布时间】:2018-02-23 18:30:09
【问题描述】:

我正在使用 Odoo11 开发天气应用程序,我有一个 Python 脚本可以从此 API 获取天气信息:https://openweathermap.org/api 该脚本运行良好,但我不知道如何将它与 Odoo 集成。 您能否提供有关如何实现这一点的指南,例如如何在表单视图、树或看板中显示此信息? 任何例子都会对我很有帮助。

【问题讨论】:

  • 您要显示什么信息?图片?文字?

标签: odoo weather-api odoo-11


【解决方案1】:

如果您只想显示一些始终更新的文本,您可以使用computed field

from odoo import api
weather = fields.Text(          # this can be an image or any other field type
    string='Weather',
    compute='_compute_weather'
)

@api.depends()          # leave this empty, so this is executed always when the view with this field is loaded
def _compute_weather(self):
    for record in self:

        # retrieve the weather information here

        record.weather = weather_information        # assign the weather information to the variable

在表单视图中显示为任何其他字段

<field name="weather" />

注意:如果您想将信息存储在数据库中,您可以只创建一个按钮或原子任务,例如,存储或更新字段中的值(不带compute方法)。

注意2:查看Cybrosis的user_weather_map模块的源代码,可能会有帮助

【讨论】:

    【解决方案2】:

    您可以使用模块User Weather Notification。 该模块使用外部 API。

        def get_weather(self, user_id):
        rec = self.env['user.weather.map.config'].search([('user_id', '=', user_id)], limit=1)
        if rec:
            weather_path = 'http://api.openweathermap.org/data/2.5/weather?'
            if rec.u_longitude and rec.u_latitude:
                    params = urllib.urlencode(
                        {'lat': rec.u_latitude, 'lon': rec.u_longitude, 'APPID': rec.appid})
            elif rec.city:
                params = urllib.urlencode(
                    {'q': rec.city, 'APPID': rec.appid})
            else:
                return {
                            'issue': 'localization'
                        }
    
            url = weather_path + params
            try:
                f = urllib.urlopen(url)
            except Exception:
                f = False
            if f:
                ret = f.read().decode('utf-8')
                result = json.loads(ret)
                if result:
                    if "cod" in result.keys():
                        if result['cod'] == 200:
                            city = False
                            city2 = False
                            if "name" in result.keys():
                                city = result['name']
                            if not city:
                                if rec.method == 'address':
                                    city = rec.city
                            if rec.method == 'address':
                                    city2 = rec.city
    
                            temp = pytemperature.k2c(result['main']['temp'])
                            min_temp = pytemperature.k2c(result['main']['temp_min'])
                            max_temp = pytemperature.k2c(result['main']['temp_max'])
                            weather_rec = self.search([('user_id', '=', rec.user_id.id)])
                            now_utc = datetime.now(timezone('UTC'))
                            user_list = self.env['res.users'].search([('id', '=', user_id)])
                            if user_list.partner_id.tz:
                                tz = pytz.timezone(user_list.partner_id.tz)
                                now_pacific = now_utc.astimezone(timezone(str(tz)))
                                current_time = now_pacific.strftime('%d %B %Y, %I:%M%p')
                                vals = {
                                    'date_weather_update': current_time,
                                    'name': city,
                                    'city': city2,
                                    'user_id': user_id,
                                    'weather': result['weather'][0]['main'],
                                    'description': result['weather'][0]['description'],
                                    'temp': temp,
                                    'pressure': result['main']['pressure'],
                                    'humidity': result['main']['humidity'],
                                    'min_temp': min_temp,
                                    'max_temp': max_temp,
                                }
                                if weather_rec:
                                    weather_rec.write(vals)
                                    return {
                                        'issue': ''
                                    }
                                else:
                                    weather_rec.create(vals)
                                    return {
                                        'issue': ''
                                    }
                            else:
                                return {
                                    'issue': 'timezone'
                                }
                        else:
                            return {
                                'issue': 'localization'
                            }
                else:
                    return {
                        'issue': 'bad_request'
                    }
            else:
                return {
                    'issue': 'internet'
                }
        else:
            return {
                'issue': 'config'
            }
    

    这是我在该模块中使用的代码。你可以把它转换成odoo11。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      相关资源
      最近更新 更多