first commit
This commit is contained in:
commit
7ecc47983d
3
__init__.py
Normal file
3
__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from . import models
|
||||
from . import controllers
|
||||
from . import wizard
|
||||
20
__manifest__.py
Normal file
20
__manifest__.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
{
|
||||
'name': "Mapan Loyalty Push Notifications",
|
||||
'summary': "Send push notifications to the Loyalty Flutter App",
|
||||
'description': """
|
||||
Integrates Odoo with Firebase Cloud Messaging (FCM) to send push notifications directly
|
||||
to customers' Android devices. Maps FCM tokens to res_partner records.
|
||||
""",
|
||||
'author': "Mapan Group",
|
||||
'category': 'Marketing',
|
||||
'version': '1.0',
|
||||
'depends': ['base', 'loyalty'],
|
||||
'data': [
|
||||
'security/ir.model.access.csv',
|
||||
'wizard/push_wizard_views.xml',
|
||||
'views/res_partner_views.xml',
|
||||
],
|
||||
'installable': True,
|
||||
'application': False,
|
||||
}
|
||||
1
controllers/__init__.py
Normal file
1
controllers/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from . import main
|
||||
34
controllers/main.py
Normal file
34
controllers/main.py
Normal file
@ -0,0 +1,34 @@
|
||||
from odoo import http
|
||||
from odoo.http import request
|
||||
|
||||
class AppNotificationController(http.Controller):
|
||||
|
||||
@http.route('/api/loyalty/fetch_notifications', type='json', auth='user', methods=['POST'], csrf=False)
|
||||
def fetch_notifications(self, **kw):
|
||||
"""
|
||||
Endpoint for the Flutter app Background Task and In-App notification center.
|
||||
"""
|
||||
user = request.env.user
|
||||
partner = user.partner_id
|
||||
|
||||
last_id = kw.get('last_id', 0)
|
||||
|
||||
# Give them global notifications OR notifications tagged explicitly to them
|
||||
domain = [
|
||||
('id', '>', last_id),
|
||||
'|',
|
||||
('partner_ids', 'in', [partner.id]),
|
||||
('is_global', '=', True)
|
||||
]
|
||||
|
||||
notifications = request.env['mapan.app.notification'].sudo().search_read(
|
||||
domain,
|
||||
['id', 'title', 'body', 'create_date'],
|
||||
order='create_date desc',
|
||||
limit=50
|
||||
)
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
'data': notifications
|
||||
}
|
||||
2
models/__init__.py
Normal file
2
models/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from . import res_partner
|
||||
from . import app_notification
|
||||
26
models/app_notification.py
Normal file
26
models/app_notification.py
Normal file
@ -0,0 +1,26 @@
|
||||
from odoo import models, fields
|
||||
|
||||
class AppNotification(models.Model):
|
||||
_name = 'mapan.app.notification'
|
||||
_description = 'Mobile App Promotional Notification'
|
||||
_order = 'create_date desc'
|
||||
|
||||
title = fields.Char(string='Notification Title', required=True)
|
||||
body = fields.Text(string='Notification Body', required=True)
|
||||
|
||||
# If no partner_ids are selected, it is broadcast to everyone logically
|
||||
partner_ids = fields.Many2many(
|
||||
'res.partner',
|
||||
string='Selected Customers',
|
||||
help='Leave empty to broadcast to all app users.'
|
||||
)
|
||||
|
||||
is_global = fields.Boolean(
|
||||
string='Broadcast to All?',
|
||||
compute='_compute_is_global',
|
||||
store=True
|
||||
)
|
||||
|
||||
def _compute_is_global(self):
|
||||
for rec in self:
|
||||
rec.is_global = not bool(rec.partner_ids)
|
||||
9
models/res_partner.py
Normal file
9
models/res_partner.py
Normal file
@ -0,0 +1,9 @@
|
||||
from odoo import models, fields
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
|
||||
fcm_token = fields.Char(
|
||||
string='FCM Device Token',
|
||||
help='Firebase Cloud Messaging token for sending push notifications via mobile app.'
|
||||
)
|
||||
3
security/ir.model.access.csv
Normal file
3
security/ir.model.access.csv
Normal file
@ -0,0 +1,3 @@
|
||||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||
access_mapan_push_wizard,mapan_push_wizard,model_mapan_push_wizard,base.group_user,1,1,1,1
|
||||
access_mapan_app_notification,mapan_app_notification,model_mapan_app_notification,base.group_user,1,1,1,1
|
||||
|
17
views/res_partner_views.xml
Normal file
17
views/res_partner_views.xml
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<record id="view_partner_form_inherit_fcm" model="ir.ui.view">
|
||||
<field name="name">res.partner.form.fcm</field>
|
||||
<field name="model">res.partner</field>
|
||||
<field name="inherit_id" ref="base.view_partner_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//notebook" position="inside">
|
||||
<page string="Mobile App Settings">
|
||||
<group>
|
||||
<field name="fcm_token" readonly="1" string="Device Token"/>
|
||||
</group>
|
||||
</page>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
||||
1
wizard/__init__.py
Normal file
1
wizard/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from . import push_wizard
|
||||
35
wizard/push_wizard.py
Normal file
35
wizard/push_wizard.py
Normal file
@ -0,0 +1,35 @@
|
||||
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)
|
||||
partner_ids = fields.Many2many(
|
||||
'res.partner',
|
||||
string='Recipients'
|
||||
)
|
||||
|
||||
def action_send_push(self):
|
||||
# Create the notification record in the DB instead of calling Firebase
|
||||
self.env['mapan.app.notification'].create({
|
||||
'title': self.title,
|
||||
'body': self.body,
|
||||
'partner_ids': [(6, 0, self.partner_ids.ids)]
|
||||
})
|
||||
|
||||
recipient_count = len(self.partner_ids)
|
||||
target_msg = f"{recipient_count} selected partners" if recipient_count > 0 else "all app users"
|
||||
|
||||
return {
|
||||
'type': 'ir.actions.client',
|
||||
'tag': 'display_notification',
|
||||
'params': {
|
||||
'title': 'App Notification Dispatched',
|
||||
'message': f'Message successfully staged for {target_msg}!',
|
||||
'type': 'success',
|
||||
'sticky': False,
|
||||
}
|
||||
}
|
||||
35
wizard/push_wizard_views.xml
Normal file
35
wizard/push_wizard_views.xml
Normal file
@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<record id="view_mapan_push_wizard_form" model="ir.ui.view">
|
||||
<field name="name">mapan.push.wizard.form</field>
|
||||
<field name="model">mapan.push.wizard</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Send Push Notification">
|
||||
<group>
|
||||
<field name="title"/>
|
||||
<field name="body"/>
|
||||
</group>
|
||||
<group string="Recipients">
|
||||
<field name="partner_ids" widget="many2many_tags" placeholder="Select customers with app installed..."/>
|
||||
</group>
|
||||
<footer>
|
||||
<button name="action_send_push" type="object" string="Send Notification" class="btn-primary" icon="fa-paper-plane"/>
|
||||
<button string="Cancel" class="btn-secondary" special="cancel"/>
|
||||
</footer>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="action_mapan_push_wizard" model="ir.actions.act_window">
|
||||
<field name="name">Send Push Notification</field>
|
||||
<field name="res_model">mapan.push.wizard</field>
|
||||
<field name="view_mode">form</field>
|
||||
<field name="target">new</field>
|
||||
</record>
|
||||
|
||||
<menuitem id="menu_mapan_push_wizard"
|
||||
name="Mobile App Push"
|
||||
parent="base.menu_custom"
|
||||
action="action_mapan_push_wizard"
|
||||
sequence="150"/>
|
||||
</odoo>
|
||||
Loading…
Reference in New Issue
Block a user