[ADD] odoo_referral: Link to odoo referral platform

closes odoo/odoo#48268

Taskid: 2153800
X-original-commit: 24b57a377fec77178aad2b433da8f743be9db747
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
Benjamin Frantzen (bfr)
2020-03-24 13:52:22 +00:00
committed by Yannick Tivisse
parent 6bf343a172
commit a4cce228cf
14 changed files with 216 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import controllers
from . import models
+16
View File
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name': "Odoo referral program",
'summary': """Allow you to refer your friends to Odoo and get rewards""",
'category': 'Hidden',
'version': '1.0',
'depends': ['base', 'web'],
'data': [
'views/templates.xml',
],
'qweb': [
"static/src/xml/systray.xml",
],
'auto_install': True,
}
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import referral
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.http import Controller, request, route
class Referral(Controller):
@route(['/odoo_referral/go'], type='json', auth='user', method='POST', website=True)
def referral_go(self):
return {'link': request.env.user._get_referral_link(reset_count=True)}
+4
View File
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import res_users
+56
View File
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
import uuid
import requests
import hmac
import threading
from hashlib import sha1
from werkzeug import url_encode
from odoo import api, models
DEST_SERVER_URL = 'https://www.odoo.com'
_logger = logging.getLogger(__name__)
class Users(models.Model):
_inherit = 'res.users'
@api.model
def get_referral_updates_count_for_current_user(self):
token = self.env.user._get_or_generate_referral_token()
if not token:
return 0
try:
response = requests.get(DEST_SERVER_URL + '/referral/notifications/' + token, timeout=10)
except Exception as e:
if not getattr(threading.currentThread(), 'testing', False):
_logger.exception("Failed to fetch referral gift notifications")
return 0
if not response.ok:
return 0
return response.json().get('updates_count', 0)
def _get_or_generate_referral_token(self):
self.ensure_one()
db_secret = self.env['ir.config_parameter'].sudo().get_param('database.secret').encode('utf-8')
db_uuid = self.env['ir.config_parameter'].sudo().get_param('database.uuid')
message = (db_uuid + self.env.user.email).encode('utf-8')
return hmac.new(db_secret, message, sha1).hexdigest()
def _get_referral_link(self, reset_count=False):
self.ensure_one()
params = {
'token': self._get_or_generate_referral_token(),
'referrer_email': self.partner_id.email,
}
if reset_count:
params['reset_count'] = 1
return DEST_SERVER_URL + '/referral/register?' + url_encode(params)
@@ -0,0 +1,54 @@
odoo.define('systray.systray_odoo_referral', function (require) {
"use strict";
var localStorage = require('web.local_storage');
var SystrayMenu = require('web.SystrayMenu');
var Widget = require('web.Widget');
var ActionMenu = Widget.extend({
template: 'systray_odoo_referral.gift_icon',
events: {
'click .gift_icon': 'onclick_gifticon',
},
start: function (parent) {
var self = this;
var lastFetch = localStorage.getItem('odoo_referral.updates_last_fetch');
var updatesCount = localStorage.getItem('odoo_referral.updates_count');
var hasClicked = localStorage.getItem('odoo_referral.has_clicked');
if (hasClicked && (!lastFetch || Date(parseInt(lastFetch)) < Date(new Date().getTime() - (24 * 60 * 60 * 1000)))) {
this._rpc({
model: 'res.users',
method: 'get_referral_updates_count_for_current_user'
}, {
shadow: true,
}).then(function (count) {
localStorage.setItem('odoo_referral.updates_last_fetch', Date.now());
localStorage.setItem('odoo_referral.updates_count', count);
if (count > 0) {
self.$('.o_notification_counter').text(count);
}
});
} else {
if (updatesCount && updatesCount > 0) {
self.$('.o_notification_counter').text(updatesCount);
}
}
return this._super.apply(this, arguments);
},
onclick_gifticon: function () {
var self = this;
localStorage.setItem('odoo_referral.updates_count', 0);
this._rpc({
route: '/odoo_referral/go/'
}).then(function (result) {
localStorage.setItem('odoo_referral.has_clicked', 1);
self.$('.o_notification_counter').text('');
window.open(result.link, '_blank', 'noopener noreferrer');
});
},
});
SystrayMenu.Items.push(ActionMenu);
return ActionMenu;
});
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates>
<t t-name="systray_odoo_referral.gift_icon">
<li class="gift_icon o_mail_systray_item">
<a role="button" href="javascript:void(0)" class="gift_icon" title="Odoo Referral Program">
<i class="fa fa-gift" aria-label="Odoo Referral Program" /> <span id="gift_notification_counter" class="o_notification_counter badge badge-pill"/>
</a>
</li>
</t>
</templates>
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<template id="assets_backend" name="systray_odoo_referral_icon" inherit_id="web.assets_backend">
<xpath expr=".">
<script type="text/javascript" src="/odoo_referral/static/src/js/systray.js"/>
</xpath>
</template>
</data>
</odoo>
+3
View File
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import controllers
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name': "Odoo referral program bridge with portal",
'summary': """Allow you to refer your friends to Odoo and get rewards""",
'category': 'Hidden',
'version': '0.1',
'depends': ['website', 'odoo_referral'],
'data': [
'views/referral_template.xml',
],
'auto_install': True,
}
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import portal
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.portal.controllers.portal import CustomerPortal
from odoo.http import request
class CustomerPortal(CustomerPortal):
def _prepare_portal_layout_values(self):
values = super(CustomerPortal, self)._prepare_portal_layout_values()
count = request.env['res.users'].get_referral_updates_count_for_current_user()
values['referral_updates_count'] = count or ''
values['referral_link'] = request.env.user._get_referral_link()
return values
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template id="portal_my_home_odoo_referral" name="Portal My Home : Odoo Referral" inherit_id="portal.portal_my_home" priority="90">
<xpath expr="//div[hasclass('o_portal_docs')]" position="inside">
<t t-call="portal.portal_docs_entry">
<t t-set="title">Odoo Referral Program</t>
<t t-set="url" t-value="referral_link"/>
<t t-set="count" t-value="referral_updates_count"/>
</t>
</xpath>
</template>
</odoo>