mapan_loyalty_push/wizard/push_wizard.py

59 lines
2.2 KiB
Python

from odoo import models, fields, api
from odoo.exceptions import UserError
class PushNotificationWizard(models.TransientModel):
_name = 'mapan.push.wizard'
_description = 'Send Mobile App Notification'
title = fields.Char(string='Notification Title', required=True)
body = fields.Text(string='Notification Body', required=True)
image = fields.Image(
string='Notification Image (optional)',
max_width=1920,
max_height=1920,
help='Upload a promotional image to display in the notification detail.'
)
recipient_type = fields.Selection([
('all', 'All Activated Members'),
('specific', 'Specific Members')
], string='Send To', default='all', required=True)
partner_ids = fields.Many2many(
'res.partner',
string='Recipients',
domain=[('user_ids.share', '=', True), ('user_ids.active', '=', True)],
)
def action_send_push(self):
if self.recipient_type == 'specific' and not self.partner_ids:
raise UserError('Please select at least one recipient, or switch to "All Activated Members".')
if self.recipient_type == 'all':
# Leave partner_ids empty → is_global=True → visible to all app users
self.env['mapan.app.notification'].create({
'title': self.title,
'body': self.body,
'image': self.image,
})
target_msg = 'all activated app members'
else:
# Specific recipients only
self.env['mapan.app.notification'].create({
'title': self.title,
'body': self.body,
'image': self.image,
'partner_ids': [(6, 0, self.partner_ids.ids)],
})
target_msg = f'{len(self.partner_ids)} selected member(s)'
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': 'Notification Sent',
'message': f'Notification staged for {target_msg}.',
'type': 'success',
'sticky': False,
'next': {'type': 'ir.actions.client', 'tag': 'reload'},
}
}