Skip to content
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

Open
wants to merge 1 commit into
base: 17.0
Choose a base branch
from

Conversation

vincent-hatakeyama
Copy link

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.

@vincent-hatakeyama vincent-hatakeyama force-pushed the 17.0-server_environment_autocreate branch 2 times, most recently from 3c8a812 to 69c34fd Compare June 21, 2024 13:05
Copy link

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.
If you want this PR to never become stale, please ask a PSC member to apply the "no stale" label.

@github-actions github-actions bot added the stale PR/Issue without recent activity, it'll be soon closed automatically. label Oct 20, 2024
@vincent-hatakeyama
Copy link
Author

@OCA/server-environment-maintainers Reviews welcome

Copy link
Contributor

@simahawk simahawk left a 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

Comment on lines +431 to +464
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
]
)
Copy link
Contributor

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...

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale PR/Issue without recent activity, it'll be soon closed automatically.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants