diff --git a/weather_infor/__init__.py b/weather_infor/__init__.py new file mode 100644 index 0000000..35e7c96 --- /dev/null +++ b/weather_infor/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from . import models +from . import wizard diff --git a/weather_infor/__manifest__.py b/weather_infor/__manifest__.py new file mode 100644 index 0000000..952df3a --- /dev/null +++ b/weather_infor/__manifest__.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +{ + 'name': "weather_infor", + + 'summary': "Short (1 phrase/line) summary of the module's purpose", + + 'author': "NXH107", + 'website': "https://www.yourcompany.com", + + 'category': 'Uncategorized', + 'version': '0.1', + + 'depends': ['base'], + + 'data': [ + 'security/ir.model.access.csv', + 'views/weather_information_views.xml', + 'wizard/res_config_settings_views.xml', + 'data/weather_infor_cron.xml', + ], + # only loaded in demonstration mode + 'demo': [ + ], + 'application': True, + 'installable': True, + 'auto_install': False, +} + diff --git a/weather_infor/data/weather_infor_cron.xml b/weather_infor/data/weather_infor_cron.xml new file mode 100644 index 0000000..629b2c3 --- /dev/null +++ b/weather_infor/data/weather_infor_cron.xml @@ -0,0 +1,14 @@ + + + + + Fetch Weather Data + + 60 + minutes + + code + model.search([]).fetch_weather() + + + diff --git a/weather_infor/models/__init__.py b/weather_infor/models/__init__.py new file mode 100644 index 0000000..6efc0a5 --- /dev/null +++ b/weather_infor/models/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import weather_information diff --git a/weather_infor/models/weather_information.py b/weather_infor/models/weather_information.py new file mode 100644 index 0000000..27bb02c --- /dev/null +++ b/weather_infor/models/weather_information.py @@ -0,0 +1,38 @@ +from odoo import fields, models, api, _ +import requests +from odoo.exceptions import UserError + +class WeatherInformation(models.Model): + _name = 'weather.information' + _description = 'Weather Information' + + city = fields.Char(string='Thành phố', required=True) + temperature = fields.Float(string='Nhiệt độ (°C)', readonly=True) + humidity = fields.Float(string='Độ ẩm (%)', readonly=True) + wind_speed = fields.Float(string='Tốc độ gió', readonly=True) + description = fields.Char(string="Mô tả") + + @api.model + def get_weather_information(self, city): + """Fetch weather data from OpenWeatherMap API""" + api_key = self.env['ir.config_parameter'].sudo().get_param('weather_infor.api_key') + if not api_key: + raise UserError("API Key chưa được cấu hình. Vui lòng vào Settings > Weather API Settings để nhập API Key.") + url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric" + + response = requests.get(url) + if response.status_code == 200: + data = response.json() + return { + 'temperature': data['main']['temp'], + 'humidity': data['main']['humidity'], + 'wind_speed': data['wind']['speed'], + 'description': data['weather'][0]['description'] + } + else: + raise UserError("Could not fetch weather data. Please check the city name or API Key.") + + def fetch_weather(self): + """Fetch weather data and update the record""" + weather_data = self.get_weather_information(self.city) + self.write(weather_data) \ No newline at end of file diff --git a/weather_infor/security/ir.model.access.csv b/weather_infor/security/ir.model.access.csv new file mode 100644 index 0000000..533f1a0 --- /dev/null +++ b/weather_infor/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_weather_information,weather_information,model_weather_information,base.group_user,1,1,1,1 diff --git a/weather_infor/views/weather_information_views.xml b/weather_infor/views/weather_information_views.xml new file mode 100644 index 0000000..47d73ce --- /dev/null +++ b/weather_infor/views/weather_information_views.xml @@ -0,0 +1,50 @@ + + + + + weather.information.form + weather.information + +
+ + + +