首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Odoo 11上显示外部信息?

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

Stack Overflow用户
提问于 2018-02-24 02:30:09
回答 2查看 685关注 0票数 0

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

EN

回答 2

Stack Overflow用户

发布于 2018-02-26 08:35:44

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

代码语言:javascript
复制
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

在窗体视图中将其显示为任何其他字段

代码语言:javascript
复制
<field name="weather" />

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

Note2:查看Cybrosis的user_weather_map模块源代码,这可能会有帮助

票数 1
EN

Stack Overflow用户

发布于 2018-05-08 11:13:56

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

代码语言:javascript
复制
    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。

谢谢。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48954177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档