-
-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[17.0][IMP] server_environment: add possibility of auto creating records #188
base: 17.0
Are you sure you want to change the base?
[17.0][IMP] server_environment: add possibility of auto creating records #188
Conversation
3c8a812
to
69c34fd
Compare
There hasn't been any activity on this pull request in the past 4 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days. |
69c34fd
to
f320932
Compare
@OCA/server-environment-maintainers Reviews welcome |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, thanks for your contrib.
TBH Is not a feature that I would include in our projs but can be interesting for others.
However:
- you should move this feature to a separate module
- you should take care of failure: some create can fail (eg: missing required fields)
- it can be very tricky to handle because you might have models that are not fully loaded yet
for model_name in self.env: | ||
model = self.env[model_name] | ||
if hasattr(model, "_server_env_global_section_name"): | ||
global_section_name = model._server_env_global_section_name() | ||
for section in serv_config: | ||
if section.startswith(f"{global_section_name}."): | ||
if serv_config[section].get("__autocreate", None): | ||
name_value = section[len(global_section_name) + 1 :] | ||
domain = [ | ||
(model._server_env_section_name_field, "=", name_value) | ||
] | ||
count = model.with_context(active_test=False).search_count( | ||
domain | ||
) | ||
if count == 0: | ||
_logger.debug("Automatic creation of %s", section) | ||
values = literal_eval( | ||
serv_config[section].get("__autocreate") | ||
) | ||
values[ | ||
model._server_env_section_name_field | ||
] = name_value | ||
records = model.create([values]) | ||
self.env["ir.model.data"].create( | ||
[ | ||
{ | ||
"name": section, | ||
"model": model_name, | ||
"module": "__server_environment__", | ||
"res_id": record.id, | ||
} | ||
for record in records | ||
] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would you loop on all the models? This mixin will be inherited by a specific model and that model only should be set up...
for model_name in self.env: | |
model = self.env[model_name] | |
if hasattr(model, "_server_env_global_section_name"): | |
global_section_name = model._server_env_global_section_name() | |
for section in serv_config: | |
if section.startswith(f"{global_section_name}."): | |
if serv_config[section].get("__autocreate", None): | |
name_value = section[len(global_section_name) + 1 :] | |
domain = [ | |
(model._server_env_section_name_field, "=", name_value) | |
] | |
count = model.with_context(active_test=False).search_count( | |
domain | |
) | |
if count == 0: | |
_logger.debug("Automatic creation of %s", section) | |
values = literal_eval( | |
serv_config[section].get("__autocreate") | |
) | |
values[ | |
model._server_env_section_name_field | |
] = name_value | |
records = model.create([values]) | |
self.env["ir.model.data"].create( | |
[ | |
{ | |
"name": section, | |
"model": model_name, | |
"module": "__server_environment__", | |
"res_id": record.id, | |
} | |
for record in records | |
] | |
) | |
global_section_name = self._server_env_global_section_name() | |
for section in serv_config: | |
if not section.startswith(f"{global_section_name}."): | |
continue | |
if not serv_config[section].get("__autocreate", None): | |
continue | |
name_value = section[len(global_section_name) + 1 :] | |
domain = [ | |
(model._server_env_section_name_field, "=", name_value) | |
] | |
count = model.with_context(active_test=False).search_count( | |
domain | |
) | |
if not count: | |
_logger.info("Automatic creation of %s", section) | |
values = literal_eval( | |
serv_config[section].get("__autocreate") | |
) | |
values[ | |
model._server_env_section_name_field | |
] = name_value | |
# TODO: handle failure | |
records = model.create([values]) | |
self.env["ir.model.data"].create( | |
[ | |
{ | |
"name": section, | |
"model": model_name, | |
"module": "__server_environment__", | |
"res_id": record.id, | |
} | |
for record in records | |
] | |
) |
Something like this would be better.
We need to create mail server or fs.storage in the interface to match those configured with server_environment. This enhancement proposes to let server_environment creates it automatically instead.