-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchange_request_conflict.py
More file actions
299 lines (249 loc) · 12.9 KB
/
change_request_conflict.py
File metadata and controls
299 lines (249 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import logging
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
_logger = logging.getLogger(__name__)
class SPPChangeRequestConflict(models.Model):
"""Extension of spp.change.request with conflict detection capabilities."""
_name = "spp.change.request"
_inherit = ["spp.change.request", "spp.cr.conflict.mixin"]
is_conflict_detection_enabled = fields.Boolean(
related="request_type_id.enable_conflict_detection",
)
# ══════════════════════════════════════════════════════════════════════════
# OVERRIDE CRUD TO INTEGRATE CONFLICT DETECTION
# ══════════════════════════════════════════════════════════════════════════
@api.model_create_multi
def create(self, vals_list):
"""Run conflict detection on creation."""
records = super().create(vals_list)
for record in records:
# Run conflict checks if enabled for this CR type
if record.request_type_id and (
record.request_type_id.enable_conflict_detection or record.request_type_id.enable_duplicate_detection
):
try:
record._run_conflict_checks()
except Exception as e:
# Log at error level and notify via message
_logger.exception("Conflict detection failed for CR %s", record.name)
# Post message so user is aware of the issue
record.message_post(
body=_("Warning: Conflict detection failed: %s. " "Manual review recommended.") % str(e),
message_type="notification",
)
return records
def write(self, vals):
"""Re-run conflict detection if relevant fields change."""
result = super().write(vals)
# Re-check conflicts if registrant or type changed
if "registrant_id" in vals or "request_type_id" in vals:
for record in self:
if (
record.request_type_id
and record.approval_state == "draft"
and (
record.request_type_id.enable_conflict_detection
or record.request_type_id.enable_duplicate_detection
)
):
try:
record._run_conflict_checks()
except Exception as e:
# Log at error level and notify via message
_logger.exception("Conflict re-check failed for CR %s", record.name)
record.message_post(
body=_("Warning: Conflict re-check failed: %s. " "Manual review recommended.") % str(e),
message_type="notification",
)
return result
# ══════════════════════════════════════════════════════════════════════════
# OVERRIDE APPROVAL HOOKS
# ══════════════════════════════════════════════════════════════════════════
def _on_submit(self):
"""Check for blocking conflicts before submission."""
# Run conflict check first
for record in self:
if record.request_type_id and record.request_type_id.enable_conflict_detection:
result = record._run_conflict_checks()
if result["needs_override"]:
# Check if already overridden
if record.conflict_status != "overridden":
raise ValidationError(
_("Cannot submit: blocking conflicts detected.\n\n%s") % "\n".join(result["messages"])
)
# Log warnings even if proceeding
if result["messages"] and result["can_proceed"]:
_logger.info(
"CR %s submitted with warnings: %s",
record.name,
"; ".join(result["messages"]),
)
# Continue with normal submission
super()._on_submit()
def _on_approve(self):
"""Re-check conflicts at approval time."""
for record in self:
if record.request_type_id and record.request_type_id.enable_conflict_detection:
# Re-run conflict check with fresh data
result = record._run_conflict_checks()
# If new blocking conflicts appeared since submission
if result["needs_override"] and record.conflict_status != "overridden":
raise ValidationError(
_("Cannot approve: new conflicts detected since submission.\n\n%s")
% "\n".join(result["messages"])
)
super()._on_approve()
# ══════════════════════════════════════════════════════════════════════════
# CONFLICT-AWARE HELPERS
# ══════════════════════════════════════════════════════════════════════════
def get_conflict_summary(self):
"""Get a summary of conflicts for display."""
self.ensure_one()
summary = {
"has_conflicts": self.conflict_count > 0,
"has_duplicates": self.duplicate_count > 0,
"conflict_status": self.conflict_status,
"duplicate_status": self.duplicate_status,
"is_blocked": self.conflict_status == "blocked",
"is_overridden": self.conflict_status == "overridden",
"conflict_count": self.conflict_count,
"duplicate_count": self.duplicate_count,
"similarity_score": self.duplicate_similarity_score,
}
if self.conflict_messages:
summary["messages"] = self.conflict_messages.split("\n")
else:
summary["messages"] = []
return summary
def can_be_submitted(self):
"""Check if this CR can be submitted (considering conflicts)."""
self.ensure_one()
if self.approval_state != "draft":
return False, _("CR is not in draft state.")
if self.conflict_status == "blocked":
if not self.env.user.has_group("spp_change_request_v2.group_cr_conflict_approver"):
return False, _("Blocking conflicts exist and cannot be overridden.")
return True, ""
def action_quick_cancel_this(self):
"""Quick action to cancel this CR due to conflict.
This provides a one-click solution for users who want to cancel
their request when they see it conflicts with another one.
"""
self.ensure_one()
# Pre-translate messages before any deletions (avoid cursor invalidation issues)
title_cancelled = _("Request Cancelled")
message_cancelled = _("Your change request has been cancelled.")
title_list = _("Change Requests")
title_cannot = _("Cannot Cancel")
message_cannot = _("This request cannot be cancelled in its current state.")
if self.approval_state == "pending":
self._do_reject(_("Cancelled by user due to conflict"))
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"title": title_cancelled,
"message": message_cancelled,
"type": "info",
"next": {"type": "ir.actions.act_window_close"},
},
}
elif self.approval_state == "draft":
# Delete detail record first to avoid FK violation
detail = self.get_detail()
if detail:
detail.unlink()
self.unlink()
return {
"type": "ir.actions.act_window",
"name": title_list,
"res_model": "spp.change.request",
"view_mode": "list,form",
"target": "current",
}
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"title": title_cannot,
"message": message_cannot,
"type": "warning",
},
}
class SPPChangeRequestTypeConflict(models.Model):
"""Extension of spp.change.request.type with conflict configuration."""
_inherit = "spp.change.request.type"
# ══════════════════════════════════════════════════════════════════════════
# CONFLICT DETECTION CONFIGURATION
# ══════════════════════════════════════════════════════════════════════════
enable_conflict_detection = fields.Boolean(
default=False,
help="Enable conflict checking for this CR type",
)
conflict_rule_ids = fields.One2many(
"spp.cr.conflict.rule",
"cr_type_id",
string="Conflict Rules",
)
conflict_rule_count = fields.Integer(
compute="_compute_conflict_rule_count",
string="Conflict Rules",
)
@api.depends("conflict_rule_ids")
def _compute_conflict_rule_count(self):
for rec in self:
rec.conflict_rule_count = len(rec.conflict_rule_ids)
# ══════════════════════════════════════════════════════════════════════════
# DUPLICATE DETECTION CONFIGURATION
# ══════════════════════════════════════════════════════════════════════════
enable_duplicate_detection = fields.Boolean(
default=False,
help="Enable duplicate checking for this CR type",
)
duplicate_detection_config_id = fields.Many2one(
"spp.cr.duplicate.config",
string="Duplicate Detection Config",
ondelete="set null",
)
# ══════════════════════════════════════════════════════════════════════════
# ACTIONS
# ══════════════════════════════════════════════════════════════════════════
def action_view_conflict_rules(self):
"""View conflict rules for this CR type."""
self.ensure_one()
return {
"type": "ir.actions.act_window",
"name": _("Conflict Rules - %s") % self.name,
"res_model": "spp.cr.conflict.rule",
"view_mode": "list,form",
"domain": [("cr_type_id", "=", self.id)],
"context": {"default_cr_type_id": self.id},
}
def action_configure_duplicate_detection(self):
"""Open or create duplicate detection configuration."""
self.ensure_one()
if self.duplicate_detection_config_id:
return {
"type": "ir.actions.act_window",
"name": _("Duplicate Detection - %s") % self.name,
"res_model": "spp.cr.duplicate.config",
"res_id": self.duplicate_detection_config_id.id,
"view_mode": "form",
"target": "new",
}
else:
# Create new config
config = self.env["spp.cr.duplicate.config"].create(
{
"cr_type_id": self.id,
}
)
self.duplicate_detection_config_id = config.id
return {
"type": "ir.actions.act_window",
"name": _("Duplicate Detection - %s") % self.name,
"res_model": "spp.cr.duplicate.config",
"res_id": config.id,
"view_mode": "form",
"target": "new",
}