如何在Odoo 11上显示外部信息?

如何在Odoo 11上显示外部信息?,odoo,weather-api,odoo-11,Odoo,Weather Api,Odoo 11,我正在使用Odoo11开发天气应用程序,我有一个Python脚本,可以从这个API获取天气信息: 这个脚本工作得很好,但我不知道如何将它与奥多集成。 您能否提供有关如何实现这一点的指导,例如如何在表单视图、树或看板中显示此信息? 任何例子都会对我很有帮助。如果你只想显示一些经常更新的文本,你可以使用 来自odoo导入api的 天气=字段。文本(#可以是图像或任何其他字段类型 string='Weather', 计算=“”“计算天气” ) @api.depends()#将此字段留空,以便在加载带有

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

如果你只想显示一些经常更新的文本,你可以使用

来自odoo导入api的

天气=字段。文本(#可以是图像或任何其他字段类型
string='Weather',
计算=“”“计算天气”
)
@api.depends()#将此字段留空,以便在加载带有此字段的视图时始终执行此字段
定义计算天气(自身):
请自行记录:
#在这里检索天气信息
record.weather=天气信息#将天气信息分配给变量
在窗体视图中将其显示为任何其他字段


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


注2:检查Cybrosis模块的源代码,如果您只想显示一些始终更新的文本,则可以使用

来自odoo导入api的

天气=字段。文本(#可以是图像或任何其他字段类型
string='Weather',
计算=“”“计算天气”
)
@api.depends()#将此字段留空,以便在加载带有此字段的视图时始终执行此字段
定义计算天气(自身):
请自行记录:
#在这里检索天气信息
record.weather=天气信息#将天气信息分配给变量
在窗体视图中将其显示为任何其他字段


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

注2:检查Cybrosis中模块的源代码,可能会有所帮助

您可以使用该模块。 此模块使用外部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

谢谢。

您可以使用该模块。 此模块使用外部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


谢谢。

您想显示哪些信息?图像?文本?您想显示什么信息?图像?文本?