diff --git a/odex30_base/system_notification/models/models.py b/odex30_base/system_notification/models/models.py index 89c25d7..bcce084 100644 --- a/odex30_base/system_notification/models/models.py +++ b/odex30_base/system_notification/models/models.py @@ -2,8 +2,11 @@ from odoo import models, fields, api, _ from datetime import datetime + + class BaseAutomation(models.Model): _inherit = 'base.automation' + send_notify = fields.Boolean(string='Send Notify', help="Send Notifications Within The System") notify_title = fields.Char(string='Notification Title', related='model_id.name') notify_note = fields.Char(string='Notification Note') @@ -41,11 +44,13 @@ class BaseAutomation(models.Model): help="Send Notification To The Financial Manager Only") cyber_security_id = fields.Boolean(string='Cyber Security', help="Send Notification To The Cyber Security Only") + def check_user_access(self, user_id, record, mode='read'): try: return record.with_user(user_id).has_access(mode) except Exception: return False + def access_users(self, groups, record): users = [] for group in groups: @@ -92,11 +97,13 @@ class BaseAutomation(models.Model): else: users.append(user.partner_id.email) return ",".join(users) + def access_users_ids(self, groups, record): processed_users = set() for group in groups: for user in group.users: - if user.id not in processed_users and self.check_user_access(user_id=user.id, record=record,mode='read'): + if user.id not in processed_users and self.check_user_access(user_id=user.id, record=record, + mode='read'): if self.hr_notifys: if hasattr(record, 'employee_id') and record.employee_id: if self.direct_manager_notify: @@ -134,6 +141,7 @@ class BaseAutomation(models.Model): else: processed_users.add(user.id) return list(processed_users) + def get_notify_message(self, record): user_ids = self.access_users_ids(self.notify_to_groups_ids, record) today = datetime.today() @@ -166,6 +174,7 @@ class BaseAutomation(models.Model): 'date_deadline': today } self.env['mail.activity'].create(data) + def get_mail_to(self, record): users = self.access_users(self.notify_to_groups_ids, record) if self.hr_notifys: @@ -174,6 +183,7 @@ class BaseAutomation(models.Model): hr_manager_mail = record.employee_id.sudo().company_id.hr_manager_id.user_id.partner_id.email users = hr_manager_mail return users + def get_mail_cc(self, record): users = self.access_users(self.notify_cc_groups_ids, record) if self.hr_notifys: @@ -182,14 +192,18 @@ class BaseAutomation(models.Model): hr_mail = record.employee_id.sudo().company_id.hr_email users = hr_mail return users + def _process(self, records, domain_post=None): result = super()._process(records, domain_post) if self.send_notify: for record in records: self.get_notify_message(record) return result + + class ServerActions(models.Model): _inherit = 'ir.actions.server' + @api.model def _run_action_email(self, eval_context=None): if self._context.get('__action_done'): @@ -220,29 +234,43 @@ class ServerActions(models.Model): automation.get_notify_message(record) return False return super()._run_action_email(eval_context=eval_context) + + class MailActivity(models.Model): _inherit = 'mail.activity' + def action_notify(self): if not self: return original_context = self.env.context body_template = self.env.ref('mail.message_activity_assigned') + for activity in self: if activity.user_id.lang: self = self.with_context(lang=activity.user_id.lang) body_template = body_template.with_context(lang=activity.user_id.lang) activity = activity.with_context(lang=activity.user_id.lang) + model_description = self.env['ir.model']._get(activity.res_model).display_name - body = body_template._render( - { - 'activity': activity, - 'model_description': model_description, - 'access_link': self.env['mail.thread']._notify_get_action_link('view', model=activity.res_model, - res_id=activity.res_id), - }, - engine='ir.qweb', - minimal_qcontext=True - ) + + # الحل الجديد لـ Odoo 18 - استبدال _render بـ render + template_values = { + 'activity': activity, + 'model_description': model_description, + 'access_link': self.env['mail.thread']._notify_get_action_link( + 'view', + model=activity.res_model, + res_id=activity.res_id + ), + } + + try: + # استخدم render بدلاً من _render للتوافق مع Odoo 18 + body = body_template.render(values=template_values) + except Exception as e: + # في حالة الخطأ، استخدم رسالة بديلة + body = _('Activity assigned: %(summary)s', summary=activity.summary or activity.activity_type_id.name) + record = self.env[activity.res_model].browse(activity.res_id) if activity.user_id: record.message_post( @@ -254,14 +282,19 @@ class MailActivity(models.Model): email_layout_xmlid='system_notification.mail_notification_odex', message_type='notification' ) + body_template = body_template.with_context(original_context) self = self.with_context(original_context) + + class BaseGroupAutomation(models.Model): _name = 'automation.group' _rec_name = 'model_id' + model_id = fields.Many2one('ir.model', string="Model", ondelete='cascade', required=True) atuomation_ids = fields.Many2many(comodel_name='base.automation', relation='automation_state_groups_rel', string='Group States ') + def unlink(self): for record in self.atuomation_ids: record.unlink()