Skip to content

[17.0][ADD] server_environment_autocreate: add possibility of auto creating records#188

Merged
OCA-git-bot merged 1 commit intoOCA:17.0from
xcgd:17.0-server_environment_autocreate
Jun 4, 2025
Merged

[17.0][ADD] server_environment_autocreate: add possibility of auto creating records#188
OCA-git-bot merged 1 commit intoOCA:17.0from
xcgd:17.0-server_environment_autocreate

Conversation

@vincent-hatakeyama
Copy link
Contributor

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
@github-actions
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 vincent-hatakeyama force-pushed the 17.0-server_environment_autocreate branch from 69c34fd to f320932 Compare November 13, 2024 09:53
@vincent-hatakeyama
Copy link
Contributor 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.

@github-actions github-actions bot removed the stale PR/Issue without recent activity, it'll be soon closed automatically. label Nov 17, 2024
@github-actions
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 Mar 23, 2025
@vincent-hatakeyama
Copy link
Contributor Author

@simahawk

you should move this feature to a separate module

This is a feature that is optional, so I did not see the need for a separate module. Have you any reason for doing it in a separate module?

Concerning your other point, I do not expect all cases to work out of the box. Basic cases (outgoing mail servers, fs storage), it works and the current version is already in production.

@github-actions github-actions bot removed the stale PR/Issue without recent activity, it'll be soon closed automatically. label Mar 30, 2025
@simahawk
Copy link
Contributor

This is a feature that is optional

That's exactly the main reason to move it to its own module. This is not something we want to maintain OOTB ;)

@vincent-hatakeyama vincent-hatakeyama force-pushed the 17.0-server_environment_autocreate branch 2 times, most recently from a4493d7 to 869a0f2 Compare April 3, 2025 14:26
@vincent-hatakeyama vincent-hatakeyama changed the title [17.0][IMP] server_environment: add possibility of auto creating records [17.0][ADD] server_environment_autocreate: add possibility of auto creating records Apr 3, 2025
@vincent-hatakeyama
Copy link
Contributor Author

I changed the feature to a new module as suggested.
I also added more tests and documentation of the feature.

@vincent-hatakeyama vincent-hatakeyama force-pushed the 17.0-server_environment_autocreate branch from 869a0f2 to 27b37b2 Compare April 3, 2025 15:41
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.

LG overall, minor comments

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.

Almost there :)

@vincent-hatakeyama vincent-hatakeyama force-pushed the 17.0-server_environment_autocreate branch 2 times, most recently from e243177 to 1f5006f Compare May 16, 2025 10:04
@vincent-hatakeyama vincent-hatakeyama force-pushed the 17.0-server_environment_autocreate branch from 1f5006f to f3c31a4 Compare June 4, 2025 07:15
@simahawk
Copy link
Contributor

simahawk commented Jun 4, 2025

/ocabot merge nobump

@OCA-git-bot
Copy link
Contributor

This PR looks fantastic, let's merge it!
Prepared branch 17.0-ocabot-merge-pr-188-by-simahawk-bump-nobump, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit 6eb71c7 into OCA:17.0 Jun 4, 2025
7 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at b31f61e. Thanks a lot for contributing to OCA. ❤️

@vincent-hatakeyama vincent-hatakeyama deleted the 17.0-server_environment_autocreate branch June 5, 2025 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants