0

I'm working on Weather application using Odoo11, I have a Python script that fetches weather information from this API: https://openweathermap.org/api The script works fine but I have no idea how to integrate it with Odoo. Can you give guidelines about how to achieve this, for example how to show this information in a form view, tree or Kanban? Any example will be very helpful for me.

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
M.Lamine Lalmi
  • 78
  • 1
  • 2
  • 12

2 Answers2

1

If you only want to show some text that´s always updated you can use a 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

Show it in a form view as any other field

<field name="weather" />

Note: If you want to store the information on the database you can just create a button or a atomate task, for instance, to store or update the values in the fields (without compute method).

Note2: Check the source code of the user_weather_map module from Cybrosis, it may be helpful

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
1

You can use the module User Weather Notification. This module uses external 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'
        }

This is the code that I use in that module. you can just convert it into odoo11.

Thank you.