Skip to content

Commit 7be614d

Browse files
mmahroussfda-odoo
authored andcommitted
[IMP] validate inverse_name
1 parent 97ff9c4 commit 7be614d

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

server/src/core/diagnostic_codes_list.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ OLS03019, DiagnosticSetting::Error, "Compute method not set to modify this field
155155
* Hence, this model is shadowing an existing model.
156156
*/
157157
OLS03020, DiagnosticSetting::Warning, "Model {0} is shadowing an existing model in dependencies",
158+
/**
159+
* On a One2many field, the inverse_name should be a field on the comodel that is a Many2one to the current model.
160+
*/
161+
OLS03021, DiagnosticSetting::Error, "Inverse field {0} does not exist on comodel {1}",
162+
/**
163+
* On a One2many field, the inverse_name should be a field on the comodel that is a Many2one to the current model.
164+
*/
165+
OLS03022, DiagnosticSetting::Error, "Inverse field is not a Many2one field",
158166
/**
159167
* A __manifest__.py file should be evaluated with a literal_eval to a single dictionary.
160168
* Do not store any other information in it.

server/src/core/python_arch_eval_hooks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ impl PythonArchEvalHooks {
10311031
("comodel_name", "str"),
10321032
("related", "str"),
10331033
("compute", "str"),
1034+
("inverse_name", "str"),
10341035
("delegate", "bool"),
10351036
("required", "bool"),
10361037
("default", "bool"),

server/src/core/python_validator.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::PathBuf;
77
use lsp_types::{Diagnostic, Position, Range};
88
use crate::core::diagnostics::{create_diagnostic, DiagnosticCode};
99
use crate::core::evaluation::ContextValue;
10-
use crate::{constants::*, Sy};
10+
use crate::{constants::*, oyarn, Sy};
1111
use crate::core::symbols::symbol::Symbol;
1212
use crate::core::odoo::SyncOdoo;
1313
use crate::core::symbols::module_symbol::ModuleSymbol;
@@ -538,6 +538,45 @@ impl PythonValidator {
538538

539539
}
540540
}
541+
if let Some(inverse_name) = eval_weak.as_weak().context.get(&S!("inverse_name")).map(|ctx_val| ctx_val.as_string()) {
542+
let Some(model_name) = eval_weak.as_weak().context.get(&S!("comodel_name")).map(|ctx_val| ctx_val.as_string()) else {
543+
continue;
544+
};
545+
let Some(model) = session.sync_odoo.models.get(&oyarn!("{}", model_name)).cloned() else {
546+
continue;
547+
};
548+
let Some(module) = class_ref.find_module() else {
549+
continue;
550+
};
551+
let main_syms = model.borrow().get_main_symbols(session, Some(module.clone()));
552+
let symbols: Vec<_> = main_syms.iter().flat_map(|main_sym|
553+
main_sym.clone().borrow().get_member_symbol(session, &inverse_name, Some(module.clone()), false, true, false, true, false).0
554+
).collect();
555+
let method_found = !symbols.is_empty();
556+
if !method_found{
557+
let Some(arg_range) = eval_weak.as_weak().context.get(&format!("inverse_name_arg_range")).map(|ctx_val| ctx_val.as_text_range()) else {
558+
continue;
559+
};
560+
if let Some(diagnostic_base) = create_diagnostic(&session, DiagnosticCode::OLS03021, &[&inverse_name, &model_name]) {
561+
self.diagnostics.push(Diagnostic {
562+
range: Range::new(Position::new(arg_range.start().to_u32(), 0), Position::new(arg_range.end().to_u32(), 0)),
563+
..diagnostic_base.clone()
564+
});
565+
}
566+
}
567+
if symbols.iter().any(|sym| !sym.borrow().is_specific_field(session, &["Many2one"])) {
568+
let Some(arg_range) = eval_weak.as_weak().context.get(&format!("inverse_name_arg_range")).map(|ctx_val| ctx_val.as_text_range()) else {
569+
continue;
570+
};
571+
if let Some(diagnostic_base) = create_diagnostic(&session, DiagnosticCode::OLS03022, &[]) {
572+
self.diagnostics.push(Diagnostic {
573+
range: Range::new(Position::new(arg_range.start().to_u32(), 0), Position::new(arg_range.end().to_u32(), 0)),
574+
..diagnostic_base.clone()
575+
});
576+
}
577+
578+
}
579+
}
541580
}
542581
}
543582
}

0 commit comments

Comments
 (0)