[IMP] affiliate_management: set affkey cookie in all pages

This commit is contained in:
Abdurrahman Saber 2025-07-16 13:10:09 +03:00
parent b180458d24
commit 21af8089d4
3 changed files with 32 additions and 21 deletions

View File

@ -48,7 +48,6 @@ class WebsiteSale(WebsiteSale):
@http.route()
def shop(self, page=0, category=None, search='', ppg=False, **post):
enable_ppc = request.env['res.config.settings'].sudo().website_constant().get('enable_ppc')
expire = self.calc_cookie_expire_date()
result = super(WebsiteSale, self).shop(page=page, category=category, search=search, ppg=ppg, **post)
aff_key = request.httprequest.args.get('aff_key')
if category and aff_key:
@ -57,14 +56,8 @@ class WebsiteSale(WebsiteSale):
vals.update({'affiliate_type': 'category'})
if (len(partner_id) == 1):
self.create_aff_visit_entry(vals) if enable_ppc else False
result.set_cookie(key='affkey', value=aff_key, expires=expire)
else:
_logger.info("=====affiliate_visit not created by category===========")
else:
if aff_key:
partner_id = request.env['res.partner'].sudo().search([('res_affiliate_key', '=', aff_key), ('is_affiliate', '=', True)])
if partner_id:
result.set_cookie(key='affkey', value=aff_key, expires=expire)
return result
@http.route()
@ -72,7 +65,6 @@ class WebsiteSale(WebsiteSale):
_logger.info("=====product page=========")
_logger.info("=====product page aff_key==%r=========", request.httprequest.args)
enable_ppc = request.env['res.config.settings'].sudo().website_constant().get('enable_ppc')
expire = self.calc_cookie_expire_date()
result = super(WebsiteSale, self).product(product=product, category=category, search=search, **kwargs)
if request.httprequest.args.get('aff_key'):
aff_key = request.httprequest.args.get('aff_key')
@ -81,7 +73,6 @@ class WebsiteSale(WebsiteSale):
vals.update({'affiliate_type': 'product'})
if (len(partner_id) == 1):
affiliate_visit = self.create_aff_visit_entry(vals) if enable_ppc else False
result.set_cookie(key='affkey', value=aff_key, expires=expire)
_logger.info("============affiliate_visit created by product==%r=======", affiliate_visit)
else:
_logger.info("=====affiliate_visit not created by product===========%s %s" % (aff_key, partner_id))
@ -133,14 +124,3 @@ class WebsiteSale(WebsiteSale):
'is_converted': True
})
return result
def calc_cookie_expire_date(self):
ConfigValues = request.env['res.config.settings'].sudo().website_constant()
cookie_expire = ConfigValues.get('cookie_expire')
cookie_expire_period = ConfigValues.get('cookie_expire_period')
time_dict = {
'hours': cookie_expire,
'days': cookie_expire * 24,
'months': cookie_expire * 24 * 30,
}
return datetime.datetime.now() + datetime.timedelta(hours=time_dict[cookie_expire_period])

View File

@ -25,4 +25,5 @@ from . import affiliate_request
from . import affiliate_image
from . import advance_commision
from . import affiliate_product_pricelist_item
from . import ir_http
# from . import odoo_http

View File

@ -0,0 +1,30 @@
from odoo import models
from odoo.http import request
from datetime import datetime, timedelta
class IrHttp(models.AbstractModel):
_inherit = 'ir.http'
@classmethod
def _dispatch(cls):
result = super(IrHttp, cls)._dispatch()
aff_key = request.httprequest.args.get('aff_key')
if aff_key and request.is_frontend and hasattr(result, 'set_cookie'):
expire = cls.calc_cookie_expire_date()
result.set_cookie(key='affkey', value=aff_key, expires=expire)
return result
@classmethod
def calc_cookie_expire_date(cls):
ConfigValues = request.env['res.config.settings'].sudo().website_constant()
cookie_expire = ConfigValues.get('cookie_expire')
cookie_expire_period = ConfigValues.get('cookie_expire_period')
time_dict = {
'hours': cookie_expire,
'days': cookie_expire * 24,
'months': cookie_expire * 24 * 30,
}
return datetime.now() + timedelta(hours=time_dict[cookie_expire_period])