-
-
Notifications
You must be signed in to change notification settings - Fork 120
[18.0][IMP] mail_activity_team: Optional notify for team members #148
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
Open
CRogos
wants to merge
2
commits into
OCA:18.0
Choose a base branch
from
c4a8-odoo:18.0-mail_activity_team-notify
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+433
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| # Copyright 2018-22 ForgeFlow S.L. | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
|
||
| from odoo import SUPERUSER_ID, _, api, fields, models | ||
| from odoo import SUPERUSER_ID, api, fields, models | ||
| from odoo.exceptions import ValidationError | ||
| from odoo.tools.misc import get_lang | ||
|
|
||
|
|
||
| class MailActivity(models.Model): | ||
|
|
@@ -47,7 +48,26 @@ def create(self, vals_list): | |
| # so if we don't have a user_team_id we don't want user_id too | ||
| if "user_id" in vals and not vals.get("team_user_id", False): | ||
| del vals["user_id"] | ||
| return super().create(vals_list) | ||
|
|
||
| activities = super().create(vals_list) | ||
|
|
||
| # when creating activities for other: send a notification to assigned user; | ||
| if self.env.context.get("mail_activity_quick_update"): | ||
| activities.action_notify_team() | ||
| return activities | ||
|
|
||
| def write(self, values): | ||
| new_team_activities = self.env["mail.activity"] | ||
| if values.get("team_id"): | ||
| new_team_activities = self.filtered( | ||
| lambda activity: activity.user_id.id != values.get("team_id") | ||
| ) | ||
| res = super().write(values) | ||
| # notify new responsibles | ||
| if "team_id" in values: | ||
| if self.env.context.get("mail_activity_quick_update", False): | ||
| new_team_activities.action_notify_team() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be removed since you are already calling
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment above. |
||
| return res | ||
|
|
||
| @api.onchange("user_id") | ||
| def _onchange_user_id(self): | ||
|
|
@@ -88,7 +108,7 @@ def _check_team_and_user(self): | |
| not in activity.team_id.with_context(active_test=False).member_ids | ||
| ): | ||
| raise ValidationError( | ||
| _( | ||
| self.env._( | ||
| "The assigned user %(user_name)s is " | ||
| "not member of the team %(team_name)s.", | ||
| user_name=activity.user_id.name, | ||
|
|
@@ -105,3 +125,77 @@ def _onchange_activity_type_id(self): | |
| if self.user_id not in members and members: | ||
| self.user_id = members[:1] | ||
| return res | ||
|
|
||
| def action_notify_team(self): | ||
| # Check if any activity has a team with notify_members enabled | ||
| classified = self._classify_by_model() | ||
| for model, activity_data in classified.items(): | ||
| records_sudo = self.env[model].sudo().browse(activity_data["record_ids"]) | ||
| activity_data["record_ids"] = ( | ||
| records_sudo.exists().ids | ||
| ) # in case record was cascade-deleted in DB, skipping unlink override | ||
| for activity in self: | ||
| if activity.res_id not in classified[activity.res_model]["record_ids"]: | ||
| continue | ||
| if activity.team_id and activity.team_id.notify_members: | ||
| # Get the record and model description | ||
| model_description = ( | ||
| activity.env["ir.model"]._get(activity.res_model).display_name | ||
| ) | ||
| record = activity.env[activity.res_model].browse(activity.res_id) | ||
| # Notify each team member except the assigned user and the current user | ||
| for member in activity.team_id.member_ids.filtered( | ||
| lambda m, assigned_user_id=activity.user_id: self.env.uid | ||
| not in m.user_ids.ids | ||
| and ( | ||
| not assigned_user_id | ||
| or (assigned_user_id and assigned_user_id not in m.user_ids) | ||
| ) | ||
| ): | ||
| if member.lang: | ||
| # Send notification in member's language | ||
| activity_ctx = activity.with_context(lang=member.lang) | ||
| else: | ||
| activity_ctx = activity | ||
| body = activity_ctx.env["ir.qweb"]._render( | ||
| "mail.message_activity_assigned", | ||
| { | ||
| "activity": activity_ctx, | ||
| "model_description": model_description, | ||
| "is_html_empty": lambda x: not x or x == "<p><br></p>", | ||
| }, | ||
| minimal_qcontext=True, | ||
| ) | ||
| record.message_notify( | ||
| partner_ids=member.sudo().partner_id.ids, | ||
| body=body, | ||
| record_name=activity_ctx.res_name, | ||
| model_description=model_description, | ||
| email_layout_xmlid="mail.mail_notification_layout", | ||
| subject=self.env._( | ||
| "%(activity_name)s: %(summary)s (Team Activity)", | ||
| activity_name=activity_ctx.res_name, | ||
| summary=activity_ctx.summary | ||
| or activity_ctx.activity_type_id.name, | ||
| ), | ||
| subtitles=[ | ||
| self.env._( | ||
| "Activity: %s", activity_ctx.activity_type_id.name | ||
| ), | ||
| self.env._("Team: %s", activity_ctx.team_id.name), | ||
| self.env._( | ||
| "Deadline: %s", | ||
| activity_ctx.date_deadline.strftime( | ||
| get_lang(activity_ctx.env).date_format | ||
| ) | ||
| if hasattr(activity_ctx.date_deadline, "strftime") | ||
| else str(activity_ctx.date_deadline), | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| def action_notify(self): | ||
| """Override to notify team members when notify_members is enabled.""" | ||
| result = super().action_notify() | ||
| self.action_notify_team() | ||
| return result | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should be removed since you are already calling
action_notify_teaminsideaction_notify, I am getting double notifications.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.
As mentioned above action_notify is not called, when user_id is the current user. Therefore it needs to be added.
Uh oh!
There was an error while loading. Please reload this page.
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.
If I as User A assign an activity to user B that is in a team with user C and D, 6 notifications are sent (2 for B, 2 for C and 2 for D).
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.
I am still struggling with the duplicate notifications. I've added a test for this case, which is currently failing. The problem is that the write method is called during the create process, which triggers the action_notify from the mail code... I have to postpone this to next week, but feel free to send an PR.
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.
I am currently using a custom module I made that contains the changes from both of your PRs. I'll leave you my current
mail_activity.pythat contains both but should fix the duplication mail problem (your test passes).