Files
maintenance-tools/maintenance_service_http_monitoring/models/maintenance_equipment.py
Stéphan Sainléger 51d3d42491
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m33s
[NEW] maintenance_service_http_monitoring: create add-on
2026-02-26 15:49:48 +01:00

98 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import timedelta
from odoo import models, fields, api
class MaintenanceEquipment(models.Model):
_inherit = 'maintenance.equipment'
maintenance_mode = fields.Boolean(
string="Maintenance Mode",
default=False,
tracking=True,
)
maintenance_mode_start = fields.Datetime(
string="Maintenance Mode Start",
readonly=True,
)
maintenance_mode_end = fields.Datetime(
string="Maintenance Mode End",
readonly=True,
help="Computed from start + configured duration",
)
http_maintenance_request = fields.Many2one(
'maintenance.request',
string="HTTP Maintenance Request",
readonly=True,
)
def action_activate_maintenance_mode(self):
for rec in self:
duration = int(self.env['ir.config_parameter'].sudo().get_param(
'maintenance_service_http_monitoring.maintenance_mode_duration', 4))
now = fields.Datetime.now()
rec.write({
'maintenance_mode': True,
'maintenance_mode_start': now,
'maintenance_mode_end': now + timedelta(hours=duration),
})
def action_deactivate_maintenance_mode(self):
for rec in self:
rec.write({
'maintenance_mode': False,
'maintenance_mode_start': False,
'maintenance_mode_end': False,
})
@api.model
def cron_deactivate_expired_maintenance_mode(self):
now = fields.Datetime.now()
expired = self.search([
('maintenance_mode', '=', True),
('maintenance_mode_end', '<=', now),
])
expired.action_deactivate_maintenance_mode()
def create_http_maintenance_request(self, ko_services):
self.ensure_one()
today = fields.Date.context_today(self)
name = f"[HTTP KO] {self.name}"
domain = [
('name', '=', name),
('equipment_id', '=', self.id),
('maintenance_type', '=', 'corrective'),
('create_date', '>=', f"{today} 00:00:00"),
('create_date', '<=', f"{today} 23:59:59"),
]
existing = self.env['maintenance.request'].search(domain, limit=1)
# Check if a task with same name already exist for the day, if its the case : skip
if existing:
self.http_maintenance_request = existing.id
return existing
request = self.http_maintenance_request
if request and not request.stage_id.done:
return request
vals = {
'name': name,
'equipment_id': self.id,
'priority': '2',
'maintenance_type': 'corrective',
'description': self._build_ko_services_description(ko_services),
}
if self.employee_id:
vals['employee_id'] = self.employee_id.id
if self.technician_user_id:
vals['user_id'] = self.technician_user_id.id
if self.maintenance_team_id:
vals['maintenance_team_id'] = self.maintenance_team_id.id
else:
team = self.env['maintenance.team'].search([], limit=1)
if team:
vals['maintenance_team_id'] = team.id
request = self.env['maintenance.request'].create(vals)
self.http_maintenance_request = request.id
return request
def _build_ko_services_description(self, ko_services):
lines = [f"Service KO: {s.service_url or s.name}" for s in ko_services]
return '\n'.join(lines)