weather information

This commit is contained in:
XuanHuyen
2025-03-20 15:10:25 +07:00
parent abac2eda4f
commit 37590ce057
10 changed files with 182 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
from . import models
from . import wizard
+28
View File
@@ -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,
}
+14
View File
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="cron_fetch_weather_data" model="ir.cron">
<field name="name">Fetch Weather Data</field>
<field name="model_id" ref="model_weather_information"/>
<field name="interval_number">60</field>
<field name="interval_type">minutes</field>
<!-- <field name="numbercall">-1</field> -->
<field name="state">code</field>
<field name="code">model.search([]).fetch_weather()</field>
</record>
</odoo>
+3
View File
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import weather_information
@@ -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)
@@ -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
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_weather_information weather_information model_weather_information base.group_user 1 1 1 1
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_weather_information_form" model="ir.ui.view">
<field name="name">weather.information.form</field>
<field name="model">weather.information</field>
<field name="arch" type="xml">
<form string="">
<sheet>
<group>
<field name="city" />
<button name="fetch_weather" type="object" string="Fetch Weather" class="oe_highlight"/>
</group>
<group>
<field name="temperature" />
<field name="humidity" />
<field name="description" />
</group>
</sheet>
</form>
</field>
</record>
<record id="view_weather_information_tree" model="ir.ui.view">
<field name="name">weather.information.tree</field>
<field name="model">weather.information</field>
<field name="arch" type="xml">
<list string="Weather Information">
<field name="city"/>
<field name="temperature"/>
<field name="humidity"/>
<field name="description"/>
</list>
</field>
</record>
<menuitem id="weather_menu" name="Weather" sequence="1"/>
<menuitem id="weather_info_menu" name="Weather Info" parent="weather_menu"/>
<record id="action_weather_info" model="ir.actions.act_window">
<field name="name">Weather Information</field>
<field name="res_model">weather.information</field>
<field name="view_mode">list,form</field>
</record>
<menuitem id="menu_weather_info" name="Weather Information"
parent="weather_info_menu" action="action_weather_info"/>
</odoo>
+1
View File
@@ -0,0 +1 @@
from . import res_config_settings
@@ -0,0 +1,6 @@
from odoo import fields, models, api, _
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
weather_api_key = fields.Char(string="OpenWeatherMap API Key", config_parameter="weather_infor.api_key")
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.view.form.inherit.weather.infor</field>
<field name="model">res.config.settings</field>
<field name="priority" eval="70" />
<field name="inherit_id" ref="base.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//form" position="inside">
<app data-string="Weather API" string="Weather API" name="weather_infor" groups="">
<setting id="weather_api" title="Weather API Key"
string="Weather API Key">
<div class="content-group" name="weather_api_key">
<div class="d-flex">
<field name="weather_api_key" class="ml16" />
</div>
</div>
</setting>
</app>
</xpath>
</field>
</record>
<record id="weather_settings_action" model="ir.actions.act_window">
<field name="name">Weather API Settings</field>
<field name="res_model">res.config.settings</field>
<field name="view_mode">form</field>
<field name="target">inline</field>
<field name="context">{'module' : 'weather_infor', 'bin_size': False}</field>
</record>
<menuitem id="weather_settings_menu" name="Weather API Settings"
parent="weather_menu" action="weather_settings_action" />
</odoo>